aspose file tools*
The moose likes Beginning Java and the fly likes trouble with precision adding floats together Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "trouble with precision adding floats together " Watch "trouble with precision adding floats together " New topic
Author

trouble with precision adding floats together

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
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?
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
How about using double rather than float?
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
how will that make a difference?
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
It will give you more precision. Is not it what you asked for?
[ August 04, 2004: Message edited by: Dmitry Melnik ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Try out the following code:

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
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

Joined: Jun 25, 2004
Posts: 388
Originally posted by Joyce Lee:
Try out the following code:



Thank you, Joyce. I will give it a try
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
BTW, I was not trying to be snotty in my respones.

I have never thought about it this way. Just wanted to find out (without being specific though) if you were concern more about fixed point computations, rather than adding precision.

I do appreciate your help

Good to know that
Jack Kay
Ranch Hand

Joined: Aug 01, 2004
Posts: 62
Howdy!:


Output: 561974.609375
What the heck is going on!?

----------------

Aaahhh...


Output: 180285.609375
Strange strange.....
[ August 05, 2004: Message edited by: Jack Kay ]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
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
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
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.20 Wow! That's fantastic!

Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
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!
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.20 Wow! 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

Joined: Apr 04, 2004
Posts: 815
How is it, then, that my $2.50 TI 4 calculator can add some numbers, but my brand new computer can't quite manage it?


I've heard it takes forever to grow a woman from the ground
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
How is it, then, that my $2.50 TI 4 calculator can add some numbers, but my brand new computer can't quite manage it?

May be your computer can do it too, but it's not allowed to?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble with precision adding floats together