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 11 of 11

Thread: IE modulus bug?

  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    IE modulus bug?

    I'm using IE 6.0.2800.1106 and it appears to have a problem with the modulus operator. For example, try entering this into the URL bar:

    javascript:alert(100%10)

    It worked fine in Netscape, but in IE I got an "Invalid character" error.

    For another example, enter this into the URL bar:

    javascript:alert(102%34)

    You should get zero back (as you do in Netscape), but IE returns 1024!

    If anyone has an idea what is going on, I would appreciate feedback. THanks!

    Tom

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    % has a special meaning in URLs - it's the character escape character. You need to escape it with %25 for it to work. (%hh where hh is a two character hexadecimal number representing a US-ASCII character)

    For the record, moz does also alert 1024, while op7 alerts 0.
    Last edited by liorean; 02-08-2004 at 04:37 AM.
    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

  • #3
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    % has a special meaning in URLs - it's the character escape character. You need to escape it with %25 for it to work. (%hh where hh is a two character hexadecimal number representing a US-ASCII character)

    For the record, moz does also alert 1024, while op7 alerts 0.
    liorean, thank you very much for the quick reply! on a related note, i also noticed that this doesn't work as expected in either NS or IE:

    1.02%.34

    1.02/.34 = 3, so how come 1.02%.34 = .33999999999999997 ??

    weird. anyway, this workaround gave me the result that i expected (zero):

    (1.02/.34)%1

    cheers,
    tom

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    "The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result."

    http://msdn.microsoft.com/library/de...eratorsTOC.asp

    Hope that helps some
    Last edited by Antoniohawk; 02-08-2004 at 06:02 AM.

  • #5
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Antoniohawk
    "The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result."

    http://msdn.microsoft.com/library/de...eratorsTOC.asp

    Hope that helps some
    yes, that does help explain why it behaved the way it did -- thanks!

    i've been relying on netscape devedge documents, which didn't reveal this information:

    http://devedge.netscape.com/library/...s.html#1042403

    msdn library pages take too long to load on my system , but maybe it's worth it.

    cheers,
    tom

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Where did you read that? This was all I found:
    <http://msdn.microsoft.com/library/de...56jsoprmod.asp>
    Modulus Operator (%)

    Divides the value of one expression by the value of another, and returns the remainder.

    result = number1 % number2

    Arguments

    result
    &nbsp;&nbsp;&nbsp;&nbsp;Any variable.
    number1
    &nbsp;&nbsp;&nbsp;&nbsp;Any numeric expression.
    number2
    &nbsp;&nbsp;&nbsp;&nbsp;Any numeric expression.

    Remarks

    The modulus, or remainder, operator divides number1 by number2 and returns only the remainder as result. The sign of result is the same as the sign of number1. The value of result is between 0 and the absolute value of number2.

    For example, in the following expression, A (which is result) equals 5.6.

    A = 19 % 6.7

    However, the Devedge documentation states:
    <http://devedge.netscape.com/library/...s.html#1042400>
    % (Modulus)
    The modulus operator is used as follows:

    var1 % var2

    The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2.
    (emphasis mine)

    Anyway, the reason for the 0.33999999999999997 is probably the floatingpoint rounding problem (which doesn't kick in if you're using integers). Why do I say this? Well, because if you test for instance 1.5%.7, the result is 0.10000000000000009 instead of .1. Note that if it rounded the numbers, or returned the integer remainder, it would have returned nothing of the kind.
    Last edited by liorean; 02-08-2004 at 02:23 PM.
    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

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You're right it's not on that page. I found it somewhere else and put in wrong link, oops. The fact that modulus only returns an integer should really be put on MSDN, but we're talking about microsoft here...

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    As I explained in the last post, it doesn't really. Netscape may say so, but the example 1.5%.7 shows clearly that it does not limit itself to integers.

    The error is more likely to be one of the floatingpoint precision problems that arise when doing many multiplicative actions, such as eg. division, modulus and multiplication. (The reason for the problem is that floatingpoint numbers only stay unrounded if the decimal part happens to be evenly divisible by ten.)
    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

  • #9
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    As I explained in the last post, it doesn't really. Netscape may say so, but the example 1.5%.7 shows clearly that it does not limit itself to integers.

    The error is more likely to be one of the floatingpoint precision problems that arise when doing many multiplicative actions, such as eg. division, modulus and multiplication. (The reason for the problem is that floatingpoint numbers only stay unrounded if the decimal part happens to be evenly divisible by ten.)
    thanks, again. sheesh, if a $1 calculator can get it right, you'd think that the millions of dollars put into developing the javascript engine would also get it right .

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The problem lies not in JavaScript. JavaScript uses the standard floatingpoint mathematics that almost all programming languages use. The problem lies rather in the floating point system in general. That's why you either use an integer only system or a fractions capable system when you want to calculate really precise matematics.
    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

  • #11
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by liorean
    The problem lies not in JavaScript. JavaScript uses the standard floatingpoint mathematics that almost all programming languages use. The problem lies rather in the floating point system in general. That's why you either use an integer only system or a fractions capable system when you want to calculate really precise matematics.
    hi, i am trying to understand you....so you are saying the floatingpoint system as used by computer programming languages is not a real fractions capable system that can do true precise calculations? if yes, that concerns me a bit.

    i am just curious, what exactly is happening for you to get the .34 result in this operation:

    1.02%.34 = .34

    thanks,
    tom


  •  

    Posting Permissions

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