Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post

    Integer Division

    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.
    Last edited by TNO; 05-10-2005 at 05:17 PM.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Math.floor() accomplishes the same thing.

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:
    Code:
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New Coder
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean View Post
    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?

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by TriKri View Post
    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.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    New Coder
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Jaha.

  • #9
    TNO
    TNO is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    213
    Thanks
    2
    Thanked 1 Time in 1 Post
    *necro bump for google's sake*
    Code:
    function div(a, b){
        return a / b - a % b / b;
    }

  • #10
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still simpler: function div(a, b) { return (a - a % b) / b; }

  • #11
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by TNO View Post
    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.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #12
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •