File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble with precision adding floats together

 
M Burke
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using double rather than float?
 
M Burke
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how will that make a difference?
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try out the following code:

 
M Burke
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joyce Lee:
Try out the following code:



Thank you, Joyce. I will give it a try
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nick George
Ranch Hand
Posts: 815
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic