Closed Thread Icon

Preserved Topic: math error ? Pages that link to <a href="https://ozoneasylum.com/backlink?for=18559" title="Pages that link to Preserved Topic: math error ?" rel="nofollow" >Preserved Topic: math error ?\

 
Author Thread
jstuartj
Bipolar (III) Inmate

From: Mpls, MN
Insane since: Dec 2000

posted posted 09-13-2001 03:18

Could someone tell me why this works out wrong in JavaScript under IE.

alert ( 012 + 1)

When I run it I get 11 not 13. Shouldn't 012 be the same as 12.

I just don't get it. It's part of a script where I am creating padded values up to 3 places. ( IE. 001 or 010 ) . I reworked it to avoid the problem but I would like to know why this dosen't work.

jstuartj

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 09-13-2001 09:14

by adding a '0' in front of the 12 you explicitly specified that this number is an Octal number (base 8)

therefore ocal 12 + 1 = decimal 11,


btw, do alert(012) alone and see what happens.

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 09-13-2001 09:58

lallous is quite right. Here is an exerpt from this page http://developer.netscape.com/docs/manuals/js/client/jsguide/ident.htm

Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8). A decimal integer literal consists of a sequence of digits without a leading 0 (zero). A leading 0 (zero) on an integer literal indicates it is in octal; a leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7.

Some examples of integer literals are: 42, 0xFFF, and -345.

jstuartj
Bipolar (III) Inmate

From: Mpls, MN
Insane since: Dec 2000

posted posted 09-14-2001 04:36

Thanks very much I spent 2 days going over the script manually tracking values and just was not seeing it.

Octal, That never would have occured to me. I haven't dealt with octal in years. That would explain the weird pattern I was seeing when I was parsing the value in a loop.

I just did not make any sense but now I see it.


jstuartj



« BackwardsOnwards »

Show Forum Drop Down Menu