Topic awaiting preservation: Math.random truncation (Page 1 of 1) |
|
---|---|
Obsessive-Compulsive (I) Inmate From: |
posted 11-12-2005 16:54
I came across this while writing a script for the SpiderMonkey shell, but I've adapted it into a small hypertext example. I've tested this on Gecko/SpiderMonkey, and the KHTML and Presto javascript engines. code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Strict 1.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US"> <head> <title>testing randomness</title> <script type="text/javascript"> //<![CDATA[ function print(x) { document.getElementById('output').appendChild(document.createTextNode(x)); document.getElementById('output').appendChild(document.createElement('br')); } /************************************************/ function makewhole(x) { return x * Math.pow(10,(x.toString().length - 2)); } var rand = Math.random(); for (i=1; makewhole(rand) == Math.floor(makewhole(rand)); i++) { rand = Math.random(); } /************************************************/ //]]> </script> </head> <body> <div id="output"></div> <script type="text/javascript"> //<![CDATA[ print('Test ' + i + ':'); print(rand); print(makewhole(rand)); //]]> </script> </body> </html>
|
Bipolar (III) Inmate From: Umeå, Sweden |
posted 11-12-2005 21:47
The problem is inherent to floating numbers and the limited precision of fixed size numbers. |
Obsessive-Compulsive (I) Inmate From: |
posted 11-13-2005 02:04
Thanks. quote:
|
Bipolar (III) Inmate From: Umeå, Sweden |
posted 11-13-2005 06:51
Gah, had a long reply written on this, but dosbox caused a kernel panic on my mac... code: 2^-n: 0 1 2 3 4 5 1,3,5 1 1/2 1/4 1/8 1/16 1/32 21/32 bin: 1 .1 .01 .001 .0001 .00001 .10101 dec: 1 .5 .25 .125 .0625 .03125 .65625 hex: 1 .8 .4 .2 .1 .08 .a8 Back to the bases, decimal radix is 10, binary is 2. 10 is factorisable into 2 and 5. Now what does this have to do with things? Well, it means the following: code: n/10: 10 9 8 7 6 5 4 3 2 1 dec: 1 .9 .8 .7 .6 .5 .4 .3 .2 .1 bin: 1 - - - - .1 - - - - hex: 1 - - - - .8 - - - - All numbers represented with a dash cannot be precisely represented in binary because they aren't evenly divisible with 5. 5 being a prime, fractions of it can't be precisely represented in base 2. |
Obsessive-Compulsive (I) Inmate From: |
posted 11-13-2005 23:13
I understood. The part I quoted above was the answer I was looking for. I'm sorry you thought differently, but I'm grateful that you would take the time to explain bases and the limits of floating point numbers. |
Bipolar (III) Inmate From: London |
posted 11-15-2005 13:18
You can also use "toFixed" to display a fixed number of DP: code: alert(123.45678.toFixed(2));
|
Bipolar (III) Inmate From: Umeå, Sweden |
posted 11-15-2005 23:00
Ie5 (mac and win) as well as saf doesn't *correctly* support toFixed. (The version I checked this in, at least. They might have added it now, I looked at this before saf 1.3/2.0.) |