Web Analytics Made Easy -
StatCounter IE modulus bug? - CodingForum

Announcement

Collapse
No announcement yet.

IE modulus bug?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    % 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; Feb 7, 2004, 10:37 PM.
    liorean <[[email protected]]>
    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

    Comment


    • #3
      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

      Comment


      • #4
        "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; Feb 8, 2004, 12:02 AM.

        Comment


        • #5
          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

          Comment


          • #6
            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; Feb 8, 2004, 08:23 AM.
            liorean <[[email protected]]>
            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

            Comment


            • #7
              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...

              Comment


              • #8
                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 <[[email protected]]>
                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

                Comment


                • #9
                  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 .

                  Comment


                  • #10
                    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 <[[email protected]]>
                    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

                    Comment


                    • #11
                      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

                      Comment

                      Working...
                      X