Click to See Complete Forum and Search --> : Problems By Numbers
David Harrison
12-23-2002, 01:58 PM
I have noticed that when multiplying decimals or subtrating a decimal from another ect. that it doesn't give the right answer, on any computer I use (all have some version of IE).
For example 11 * 11 = 121 and 1.1 * 1.1 = 1.21
However when the computer works out 1.1 * 1.1 it gives the answer 1.2100000...000001
Also everyone should agree that 0.8 - 1 = -0.2, fair enough, however the computer gets -0.199999...9999995
Why is this?
I am currently writing a bit of a monster of a script that will incorporate everything I know about javascript and it will work various things out depending on what information it is given and because it includes decimals at some oint or another some of my results are a little bit out, this is frustrating so can anyone help me solve this problem?
Zach Elfers
12-23-2002, 02:07 PM
hmmmm........ I don't know why it is doing that. The answers aren't that far off.
jeffmott
12-23-2002, 03:37 PM
hmmmm........ I don't know why it is doing that
Not a very productive post...
The infinite set that a mathematician thinks of as the real numbers can only be approximated on a computer, since the computer only has a finite number of bits to store an infinite number of numbers.
Internally, your computer represents floating-point numbers in binary. Floating-point numbers read in from a file or appearing as literals in your program are converted from their decimal floating-point representation (eg, 19.95) to an internal binary representation.
However, 19.95 can't be precisely represented as a binary floating-point number, just like 1/3 can't be exactly represented as a decimal floating-point number. The computer's binary representation of 19.95, therefore, isn't exactly 19.95.
This affects all computer languages that represent decimal floating-point numbers in binary.
If precision is important, such as when dealing with money, it's good to work with integers and then divide at the last possible moment. For example, work in pennies (1995) instead of dollars and cents (19.95) and divide by 100 at the end.
To get rid of the superfluous digits, just round to get the required precision.
Charles
12-23-2002, 03:42 PM
Be aware though, that JavaScript doesn't have anything like integers. All numbers are 8 byte floating-point values.
Charles
12-24-2002, 04:38 AM
Originally posted by Dave Clark
...or use the toFixed() method:
http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthtofixed.asp
Dave But that's a link to the JScript documentation. JScript is a Microsoft specific version of JavaScript. Also, a great many people don't use JavaScript so we're talking about making your page broken for just a few more people.
However:From the ECMA-262 Specification (http://www.ecma.ch/ecma1/STAND/ECMA-262.HTM), page 111
15.7.4.5 Number.prototype.toFixed (fractionDigits)
Return a string containing the number represented in fixed-point notation with fractionDigits digits
after the decimal point. If fractionDigits is undefined, 0 is assumed. Specifically, perform the
following steps:
1. Let f be ToInteger(fractionDigits). (If fractionDigits is undefined, this step produces the value
0).
2. If f < 0 or f > 20, throw a RangeError exception.
3. Let x be this number value.
4. If x is NaN, return the string "NaN".
5. Let s be the empty string.
6. If x = 0, go to step 9.
7. Let s be "-".
8. Let x = –x.
9. If x = 1021, let m = ToString(x) and go to step 20.
10. Let n be an integer for which the exact mathematical value of n ÷ 10f – x is as close to zero as
possible. If there are two such n, pick the larger n.
11. If n = 0, let m be the string "0". Otherwise, let m be the string consisting of the digits of the
decimal representation of n (in order, with no leading zeroes).
12. If f = 0, go to step 20.
13. Let k be the number of characters in m.
14. If k > f, go to step 18.
15. Let z be the string consisting of f+1–k occurrences of the character ‘0’.
16. Let m be the concatenation of strings z and m.
17. Let k = f + 1.
18. Let a be the first k–f characters of m, and let b be the remaining f characters of m.
19. Let m be the concatenation of the three strings a, ".", and b.
20. Return the concatenation of the strings s and m.
The length property of the toFixed method is 1.
If the toFixed method is called with more than one argument, then the behaviour is undefined (see
clause 15).
An implementation is permitted to extend the behaviour of toFixed for values of fractionDigits less
than 0 or greater than 20. In this case toFixed would not necessarily throw RangeError for such
values.
NOTE
The output of toFixed may be more precise than toString for some values because toString only
prints enough significant digits to distinguish the number from adjacent number values. For example,
(1000000000000000128).toString() returns "1000000000000000100", while
(1000000000000000128).toFixed(0) returns "1000000000000000128".You'll find that Number.prototype.toFixed is implemented in Mozila 1.2.1, MSIE 6 and NN 7, but not NN4 or Opera 6.
khalidali63
12-24-2002, 09:06 AM
Ofcource JavaScript does not allow allots of Math specific functionality,but it does support enough,please take a look at the JavaScripts Math object,there functions like
Math.round() (3.2 = 3)
Math.ceil() (3.2 = 4)
Math.floor() (3.6 = 3)
and so on
Khalid