
Javascript rounding error
HI
There is a rounding error problem that I encountered using Java script.
The problem occurs when you do an caculation 4.225 times a integer:
Note ^ means exponent
I.e. 10^3 = 1000 and 10^2 = 100
Here are the output that Java script's caculation result
4.225 * 10 = 42.25
4.225 x 100 = 422.49999999999994
4.225 x 1000 = 4225
4.225 x 10000 = 42250
4.225 x 100000 = 422499.99999999994
4.225 x 10^6 = 4225000
4.225 x 10^7 = 42250000
4.225 x 10^8 = 422499999.99999994
....
4.225 x 10^11 = 422499999999.99993
....
4.225 x 10^14 = 422499999999999.93
...
4.225 x 10^17 = 422499999999999930
...
4.225 x 10^2 = 0.042249999999999996
Note: There is no rounding error when 4.225 times a number that is smaller
than 0.01
It seems that when 4.225 x 10^X when X is a natural number and X is in the
expression of 2+3n (when n is a natural number) or X = 2 it will give you
a rounding error.
I know decimal number in most of the program langauge is represented by a
floating number which is a binary represetation.
I did some investigation on Java Script as well as the number 4.225 and here
is what I found
1. 4.225's hex(base 16) representation is 4.399999999999999999999999999....
which is a infinite decimal represtation (like 1/3)
2. Java script is typless and integer value can be from 2^53 to 2^53 ( I
think that means there are 54 bits are used to store both mantissa and exponent).
I thought the problem is when certain bits been taken away from mantissa
to exponent, there is some minor information that stored in mantissa been
lost, that's why we have a rounding error.
My question is:
1. How do we get around?
2. How do we "detect" those floating point problems give some "known" implemetation
about how the floating point been handled in certain program. ( I think there
is a general problem with floating point in some cases, 4.225 is just one
of those examples)
Thank You for your time
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Development Centers
 Android Development Center
 Cloud Development Project Center
 HTML5 Development Center
 Windows Mobile Development Center
