# Math Issues

• 01-10-2005
Berserker
Math Issues
Umm... Hi.

Whenever...

33.3 + 33.3 + 33.3 = 99.89999

or

33.3 * 3 = 99.8999

Why doesn't it equal 99.9?

-Berserker
• 01-10-2005
misplaced
umm..

and

cannot help

Why not post code?
• 01-10-2005
Thantos
Because floating point numbers can not respresent every number accurately. However 99.9 does = 99.89999999999999999999999999999999999999999999999 9999999999(to infinity)
• 01-10-2005
Berserker
Sorry :p Calculating a triangle's perimeter.

void perimeter(void)
{

float perimeter, a, b, c;

printf("Please input side A > ");
scanf("%f", &a);
printf("Please input side B > ");
scanf("%f", &b);
printf("Please input side C > ");
scanf("%f", &c);
perimeter = a + b + c;
printf("\nThe final solution is... > %.5f\n\n", perimeter);
}
• 01-10-2005
Berserker
Quote:

Originally Posted by Thantos
Because floating point numbers can not respresent every number accurately. However 99.9 does = 99.89999999999999999999999999999999999999999999999 9999999999(to infinity)

Is there any way I can accurately depcit all numbers?
• 01-11-2005
Thantos
Nope. Due to memory limitations and per fact that some numbers just never end it's impossible to accurately store all numbers. What you have to do is decide how close is close enough.

Edit: you can also try using double instead of float but I don't know how much difference it'll make in this case.
• 01-11-2005
Berserker
Quote:

Originally Posted by Thantos
Nope. Due to memory limitations and per fact that some numbers just never end it's impossible to accurately store all numbers. What you have to do is decide how close is close enough.

Edit: you can also try using double instead of float but I don't know how much difference it'll make in this case.

Perhaps that is what I don't understand.
To me, 3.33 + 3.33 + 3.33 should equal 99.9. Not 99.8999...
I know it can't store all numbers, but it should accurately depict the numbers displayed.
It screws up Pi too, but that is more understandable.
Oh well, I guess I'll never understand it.
Thanks anyway.

PS: Double did the same as float.
• 01-11-2005
jim mcnamara
Here is an explanation of fixed-point math, which may also help explain what flaoting point numbers are doing to confuse you.

http://www.wwnet.net/~stevelim/fixed.html
• 01-11-2005
DonFiasco
You could also have a look at arbitrary-precision math libraries, like http://www.tc.umn.edu/~ringx004/mapm-main.html
• 01-11-2005
Codeplug