# trouble with precision adding floats together

M Burke

Ranch Hand

Posts: 406

posted 12 years ago

I have two floats, and I want to add them together and keep a level of precision. How can I do this?

Example:

float a = 180285.61;

float b = 381689.0;

float tot;

tot = a + b;

I get 561974.6, but I want 561974.61

How do I make certain all the answers are in .00 format and round accordingly?

Example:

float a = 180285.61;

float b = 381689.0;

float tot;

tot = a + b;

I get 561974.6, but I want 561974.61

How do I make certain all the answers are in .00 format and round accordingly?

Dmitry Melnik

Ranch Hand

Posts: 328

M Burke

Ranch Hand

Posts: 406

posted 12 years ago

I was wonerding why that works. But I think I understand now.

BTW, I was not trying to be snotty in my respones. I do appreciate your help

Originally posted by Dmitry Melnik:

It will give you more precision. Is not it what you asked for?

[ August 04, 2004: Message edited by: Dmitry Melnik ]

I was wonerding why that works. But I think I understand now.

BTW, I was not trying to be snotty in my respones. I do appreciate your help

M Burke

Ranch Hand

Posts: 406

Dmitry Melnik

Ranch Hand

Posts: 328

Jack Kay

Ranch Hand

Posts: 62

Julian Kennedy

Ranch Hand

Posts: 823

posted 12 years ago

That's the vagaries of floating point representation and arithmetic for you. The reason for the strangeness is that the internal representation is in binary. The sign, mantissa (bit before the 'E') and exponent (bit after the 'E') are split across the 32 bits of a float or the 64 bits of a double.

With only so many bits to work with there is a limit on the precision (number of significant figures, I suppose) that can be achieved. The more bits you have, the better the precision. Think about it. 1.1 in binary is 1.5 in decimal; 1.11 is 1.75; 1.111 is 1.875 and so on. Say you want to represent the decimal number 0.999 in binary: you can never do it exactly as 1.???1 in binary is always going to be 1.???5 in decimal.

Scary huh?! It's official that computers can't count, and they're running the systems that take care of your bank balance and keep the planes in the air!

Hope that's informative.

Jules

With only so many bits to work with there is a limit on the precision (number of significant figures, I suppose) that can be achieved. The more bits you have, the better the precision. Think about it. 1.1 in binary is 1.5 in decimal; 1.11 is 1.75; 1.111 is 1.875 and so on. Say you want to represent the decimal number 0.999 in binary: you can never do it exactly as 1.???1 in binary is always going to be 1.???5 in decimal.

Scary huh?! It's official that computers can't count, and they're running the systems that take care of your bank balance and keep the planes in the air!

Hope that's informative.

Jules

Dmitry Melnik

Ranch Hand

Posts: 328

Joyce Lee

Ranch Hand

Posts: 1392

posted 12 years ago

Julian, good explanation!

There's an assignment Sum in Cattle Drive that addresses this issue. You may want to take a look and try it out.

Have fun!

There's an assignment Sum in Cattle Drive that addresses this issue. You may want to take a look and try it out.

Have fun!

Peter Chase

Ranch Hand

Posts: 1970

posted 12 years ago

Currency calculations generally should be done in integers, not floating-point ("double" or "float"), for the reasons already discussed. Integer arithmetic is exact. For instance, in U.K. money, do your calculations in integer pence (100 pence = 1 pound), not floating-point pounds.

Originally posted by Dmitry Melnik:

Scary huh?! It's official that computers can't count, and they're running the systems that take care of your bank balance and keep the planes in the air!

Regular price: $82.49

Your price: $88.99

Your savings: $8.20Wow! That's fantastic!

Currency calculations generally should be done in integers, not floating-point ("double" or "float"), for the reasons already discussed. Integer arithmetic is exact. For instance, in U.K. money, do your calculations in integer pence (100 pence = 1 pound), not floating-point pounds.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

Nick George

Ranch Hand

Posts: 815