## rounding

I want to round a number to the nearest integer.

Calculation is 41/104 * 52

This results in 20.5

If i do round(41/104) *52 which results
in 20

if i divide 41/104 and then take the result
0.3942307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 30769230769230769

and put this into the ttcalc and multiply by 52 i get 20.499999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999639 999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999 99999999999999988

so why does (41/104)*52 display 20.5 not 20.499999....etc ?

Added by: ~pabslabs, 2010 X 13

Hi, just reread my post and its a bit confusing.

1. If i calculate (41/104)*52 the calculator rounds up to 20.5

2. If i calculate 41/104 and then paste that result into the calc box and multiply it by 52 i get 20.4999999..

why is there a difference between calculation result in 1 and 2 ?

thanks, Paul

TTMath is a binary library. When you calculating decimal "41/104" then:
• decimal 41 is converted to binary 101001
• decimal 104 is converted to binary 1101000
• now division is made and the result is (96 bits mantissa): 0.0110010011101100010011101100010011101100010011101100010011 101100010011101100010011101100010011101 (the result is an approximation of the real value - we took only first 96 bits)
• the binary result is converted back to decimal: 0.3942307692307692307692307692
And here is the 'problem'. The decimal representation is not *exactly* the same as the binary. It is rounded. It should be rounded because the real decimal value is not finite, it would be something like:
0.3942307692307692307692307692307692307692307692307692307692 307692307692307692307692307692307692307692307692307692307692 30769230769230769230769230769230769

So if you put 0.3942307692307692307... instead of 41/104 the result is a little different.