I’m not so sure about correct casts, here especially from unsigned int and #defines (whatever type that is) to double.
In this example
#define SPEEDSENSOR_EDGES_NUM 10
int speed_counter_left;
double result = speed_counter_left / SPEEDSENSOR_EDGES_NUM;
the result always is 0 when speed_counter_left is < 10.
So I put in some casts:
double result = (double) ((double) speed_counter_left / (double) SPEEDSENSOR_EDGES_NUM);
This is correct, but I think there are too many casts. How can I figure out the correct way with least casts?
Having one
doubleoperand should be enough:The cast “sticks” closest to
speed_counter_left. So it’s essentially equivalent to:The standard says:
And (for decimal constants without suffixes) the list goes:
int,long int,long long int.