PDA

View Full Version : Integer Division


TNO
05-10-2005, 04:07 PM
Frustrated by the fact that you don't have the integer division operator in JavaScript?

Here's a surprisingly little known method:

parseInt(19/4) = 4

Now when a VBguru or vbscripter says "ha ha, I can integer divide and you can't!"

You now know better.

jkd
05-10-2005, 07:26 PM
Math.floor() accomplishes the same thing.

PassiveSmoking
05-25-2007, 12:08 PM
After doing some profiling in Firebug it also seems that Math.floor (or Math.round is slightly faster than parseInt. Not by a massive amount but enough to make a difference in loops.

felgall
05-25-2007, 09:34 PM
parseInt is optimized for changing the base that a number is using (anything between binary and base 36) so it would not be as efficient at converting to integer the way that those methods designed for that purpose would be.

Math.floor() Math.round() Math.ceil() are designed specifically to convert a number to an integer and will obviously be the most efficient way to do it.

liorean
05-26-2007, 01:24 AM
Actually the thing with parseInt isn't that it's optimised for base conversions - it isn't. It's slower because it converts the floating point number into a string first, then parses that string as an integer number, so it does a roundtrip through a string instead of doing a purely numerical operation.


Oh, and if a 32-bit signed integer is enough for you, this will be faster in some (most) implementations:var
truncatednumber=19/4>>0;In most implementations it does a truncating double-->int32-->double conversion, which often boils down to only a couple of native operations.

TriKri
08-09-2007, 10:43 PM
It's slower because it converts the floating point number into a string first, then parses that string as an integer number, so it does a roundtrip through a string instead of doing a purely numerical operation.

How comes it does this? It sounds wicked! Surely it's able to convert a float to an integer?

liorean
08-09-2007, 10:58 PM
How comes it does this? It sounds wicked! Surely it's able to convert a float to an integer?ECMAScript is able to do that just fine.


However, parseInt specifically parses an integer number from a string. It's not made for turning floats into integers, it's made for parsing strings into integers. Since ECMAScript has automatic type conversion virtually everywhere, what happens is that since the function expects a string, the engine will serialise the number to a string. After doing that it parses the generated string for an integer number, and returns a that integer, stored in a float. (Since the only *user visible* number type in ECMAScript is double precision floating point, all numbers are stored that way.)

TriKri
08-09-2007, 11:14 PM
Jaha. :thumbsup:

TNO
03-26-2010, 12:46 AM
*necro bump for google's sake*

function div(a, b){
return a / b - a % b / b;
}

huang
03-22-2011, 08:53 AM
Still simpler: function div(a, b) { return (a - a % b) / b; }

Krupski
03-22-2011, 02:19 PM
Frustrated by the fact that you don't have the integer division operator in JavaScript?

Here's a surprisingly little known method:

parseInt(19/4) = 4

Now when a VBguru or vbscripter says "ha ha, I can integer divide and you can't!"

You now know better.

Thanks. I already knew that, but thanks anyway. You have helped someone that didn't know.

Kor
03-22-2011, 02:45 PM
Hey: stop digging out medieval threads!

huang, what do we do here? Arithmetic variations?
Krupski, whenever you are use parseInt(), don't forget to specify the base. If your number comes from decimal base, make sure you set that:

parseInt(number,10)

The base 10 is not the default. There are some cases in which numbers are take as octal.