Closed Bug 538209 Opened 15 years ago Closed 15 years ago

Large number division is wrong

Categories

(Core :: JavaScript Engine, defect)

x86
Windows 7
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 5856

People

(Reporter: pvnick, Unassigned)

Details

I'm running 64bit Windows 7 with 32bit versions of various webbrowsers. The following statement:

alert(Math.pow(0xFFFFFFFF,2).toString() + " / 2 = " + Math.pow(0xFFFFFFFF, 2)/2).toString()

displays 18446744065119617000 / 2 = 9223372032559809000 which is clearly wrong. 9 * 2 != 17. Am I missing something here or is this a bug? I tried it in Firefox, Chrome, and Safari with the same results.

I'm running Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7
Welcome to IEEE 754 double precision -- used by C, C++, Java, JavaScript, etc.

$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
ibase=16
obase=2
FFFFFFFF^2
1111111111111111111111111111111000000000000000000000000000000001
./2
111111111111111111111111111111100000000000000000000000000000000
^D
$ wc
111111111111111111111111111111100000000000000000000000000000000
       1       1      64

Math.pow(0xFFFFFFFF,2) (FFFFFFFF^2 in bc terms) does not fit in IEEE 754 double precision, which as 53 bits for the mantissa (including implied leading 1). So the result is truncated, and then half that fits in a double without loss.

When you see the same behavior in all top JS implementations, consider checking the specs -- not filing a bugzilla.mozilla.org bug.

/be
Status: NEW → RESOLVED
Closed: 15 years ago
Resolution: --- → INVALID
Could dup to bug 5856 but perhaps this is different enough -- INVALID in any case.

/be
On second thought, dup.

/be
Resolution: INVALID → DUPLICATE
You need to log in before you can comment on or make changes to this bug.