View Full Version : IE modulus bug?

10basetom

02-08-2004, 02:57 AM

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

liorean

02-08-2004, 03:34 AM

% 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.

10basetom

02-08-2004, 03:52 AM

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

Antoniohawk

02-08-2004, 04:57 AM

"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/default.asp?url=/library/en-us/script56/html/js56jslrfJScriptOperatorsTOC.asp

Hope that helps some :)

10basetom

02-08-2004, 09:41 AM

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/default.asp?url=/library/en-us/script56/html/js56jslrfJScriptOperatorsTOC.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/manuals/2000/javascript/1.3/reference/ops.html#1042403

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

cheers,

tom

liorean

02-08-2004, 01:20 PM

Where did you read that? This was all I found:

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsoprmod.asp>

Modulus Operator (%)

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

result = number1 % number2

Arguments

result

Any variable.

number1

Any numeric expression.

number2

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/manuals/2000/javascript/1.3/reference/ops.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.

Antoniohawk

02-08-2004, 05:24 PM

You're right it's not on that page. I found it somewhere else and put in wrong link, oops. :o The fact that modulus only returns an integer should really be put on MSDN, but we're talking about microsoft here...

liorean

02-08-2004, 05:32 PM

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.)

10basetom

02-08-2004, 07:55 PM

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 ;).

liorean

02-08-2004, 09:47 PM

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.

10basetom

02-08-2004, 09:59 PM

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

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.