user3101198
user3101198

Reputation: 21

C++: Simple operator precedence issue or something else?

If I have the following code:

long lSecondsSum = 8039;
double dNumDays = lSecondsSum / (24 * 3600);

I expect to get 0.093044, but for some reason I am getting dNumDays = 0.0000000000.

However, if I write the code as follows:

long lSecondsSum = 8039;
double dNumDays = lSecondsSum/24;
dNumDays = dNumDays/3600;

then I get correct dNumDays = 0.092777777777777778.

How do I avoid all these floating point errors?

Upvotes: 1

Views: 72

Answers (2)

Richard Viney
Richard Viney

Reputation: 1007

In your first code snippet you are getting zero because all the math is being done as integers and then converted to double by the assignment. You want to to do all the math in double precision, e.g.

long lSecondsSum = 8039;
double dNumDays = lSecondsSum / (24.0 * 3600.0);

Your second code snippet works because the third line is done in double precision, however the second line is not and you may be expecting it to be, so watch out for that.

The reason for this working like it does is that if you do long * long or long / long then the result will be a long, not a double, even if you assign the resulting long to a double, hence the result of your math was 0. This zero was then assigned to your double. However, long / double will be done in double precision and give you a double back, which is what you want. Essentially, be aware whether your calculations are being done as integer math or in double precision, otherwise you'll get caught out.

Upvotes: 0

billz
billz

Reputation: 45410

lSecondsSum is long, 8039/86400 will be 0

If you convert 24 and 3600 to double, you will get correct result:

double dNumDays = lSecondsSum / (24 * 3600.0);

Or just:

double dNumDays = lSecondsSum / 24.0 / 3600.0;

Upvotes: 3

Related Questions