[Solved] Addition between Integer and Float…. resulting in a Zero


In the line:

TimeNow = (28220 * 1000000) + (461189000 * 0.001);

the (28220 * 1000000) part is calculated using ints, and (with 32-bit int) overflows giving an incorrect value (likely -1844771072).

The (461189000 * 0.001) part is calculated using doubles, because of the 0.001 double constant, giving 461189.0 as a double.

The two are then added together, giving a negative double value… when that negative double is converted to unsigned long long for the assignment to TimeNow, it’s being converted as 0, probably because that’s the closest value in its range to any negative number. This is different from converting a negative integer type to unsigned, which would “wrap around”.

2

solved Addition between Integer and Float…. resulting in a Zero