I need to do multiplication that mirrors how C handles integer

multiplication. That is, one 32-bit number times another 32-bit number

equals the least significant 32-bits of the 64-bit result.

In theory, I could mimic that behavior like so:

(x * x) & 0xFFFFFFFF

But this doesn't work quite right because the initial multiplication

can produce such a large result that JavaScript looses some precision.

Any thoughts? Ideas?

"Jeff.M" < XXXX@XXXXX.COM > writes:

or (x * x) | 0, since binary operations truncates to 32 bits ...

That is a problem, yes.

Split the numbers into smaller parts and do the multiplication yourself.

Something like:

function mul32(n, m) {

n = n | 0;

m = m | 0;

var nlo = n & 0xffff;

var nhi = n >> 16; // Sign extending.

var res = ((nlo * m) + (((nhi * m) & 0xffff) << 16)) | 0;

return res;

}

(NOT tested throughly!)

/L

--

Lasse Reichstein Holst Nielsen

'Javascript frameworks is a disruptive technology'

or (x * x) | 0, since binary operations truncates to 32 bits ...

That is a problem, yes.

Split the numbers into smaller parts and do the multiplication yourself.

Something like:

function mul32(n, m) {

n = n | 0;

m = m | 0;

var nlo = n & 0xffff;

var nhi = n >> 16; // Sign extending.

var res = ((nlo * m) + (((nhi * m) & 0xffff) << 16)) | 0;

return res;

}

(NOT tested throughly!)

/L

--

Lasse Reichstein Holst Nielsen

'Javascript frameworks is a disruptive technology'

On May 2, 3:54m, Lasse Reichstein Nielsen < XXXX@XXXXX.COM >

> n = n | 0; >> m = m | 0;> > var nlo = n & 0xffff>

> var nhi >>n >> 16; // Sign extendin>.

> var res = ((nlo * m) + (((nhi * m) & 0xf<<f) << 16)) |>0;

> return >es>

> >> >

>

> (NOT tested through>y!)

> Lasse Reichstein Holst Nie>sen

> Javascript frameworks is a disruptive technology'

Beautiful. Thanks.

> n = n | 0; >> m = m | 0;> > var nlo = n & 0xffff>

> var nhi >>n >> 16; // Sign extendin>.

> var res = ((nlo * m) + (((nhi * m) & 0xf<<f) << 16)) |>0;

> return >es>

> >> >

>

> (NOT tested through>y!)

> Lasse Reichstein Holst Nie>sen

> Javascript frameworks is a disruptive technology'

Beautiful. Thanks.

Dr J R Stockton < XXXX@XXXXX.COM > writes:

That's well spotted - there's no need to make numbers small, it only

matters to keep the number of significant digits below 53.

With that in mind, the last line can be reduced (slightly) to:

(the first summand has 32 bits of precission, the second 48, and they

overlap, so it should be fine).

/L

--

Lasse Reichstein Holst Nielsen

'Javascript frameworks is a disruptive technology'

That's well spotted - there's no need to make numbers small, it only

matters to keep the number of significant digits below 53.

With that in mind, the last line can be reduced (slightly) to:

(the first summand has 32 bits of precission, the second 48, and they

overlap, so it should be fine).

/L

--

Lasse Reichstein Holst Nielsen

'Javascript frameworks is a disruptive technology'

1. [CHANGED] VS 64-bit...VS 32-bit and Windows 32-bit on CPU 64-bit

2. Windows 7 64 bit - FileMaker 32 bit: MySQL-driver 64 bit or 32 bit?

3. 32-Bit TAPISRV with 64-Bit RemoteSP (and 32-Bit TapiClient) gets no incoming calls

4. 32-Bit TAPISRV with 64-Bit RemoteSP (and 32-Bit TapiClient) ge

5. retail Home Premium 32-bit to Ultimate 32-bit to Ultimate 64-bit.

6. 32-Bit TAPISRV with 64-Bit RemoteSP (and 32-Bit TapiClient) gets n

7. Ubuntu 32-bit, 32-bit PAE, 64-bit Kernel Benchmarks

8. 32 bit process & 64 bit driver, 32/64 bit pointer?

9. From 32 bit Windows upgrade to 64 bit Windows or 32/64 bit Linux is better?

10. Can I change from 32-bit to 64-bit Vista in my 64-bit machine?

11. Processing 64-bit cube on 64-bit Analysis Services happening on 32-bit Analysis Services

12. compiling on 32-bit vs. 64-bit (gcc fails on 64-bit)

13. AIX 64-bit kernel vs. 32-bit with 64-bit extensions

14. Can you run 64-bit and 32-bit SQL server on same 64-bit Server

15. Can I install 64-bit VM on 32-bit OS running on 64-bit CPU?

5 post • Page:**1** of **1**