Page 1 of 1

int does not like 08 and 09?

Posted: Mon Jun 11, 2012 3:06 am
by joejoe

Code: Select all

> (int 00)
0

> (int 01)
1

> (int 02)
2

> (int 03)
3

> (int 04)
4

> (int 05)
5

> (int 06)
6

> (int 07)
7

> (int 08)
0

> (int 09)
0

> (int 10)
10

> (int 11)
11

ok. nL is playing tricks on me! ;0)

Manual says:
If exp evaluates to a string, the string must start with a digit; one or more spaces; or the + or - sign.

Code: Select all

(set 'my-nums '("00" "08" "40"))

(int (nth 1 my-nums))

;-> 0
I managed to get float to come to my rescue:

Code: Select all

(int (float (nth 1 my-nums)))

;-> 8
What's up w/ int and the 08 and 09 numbers?

Thank you.

Re: int does not like 08 and 09?

Posted: Mon Jun 11, 2012 4:36 am
by saulgoode
Numeric constants that start with a zero are treated as octal numbers, therefore '8' and '9' are not valid digits.

Re: int does not like 08 and 09?

Posted: Mon Jun 11, 2012 4:38 am
by Patrick
See here in the documentation
The string must begin with '0x' for hexadecimal strings or '0' (zero) for octal strings. If exp is invalid, int returns nil as a default value if not otherwise specified.
If you prefix it with a 0, it will be evaluated as octal. There is no 9 or 8 in octal, since 8 dec is 10 oct.

Re: int does not like 08 and 09?

Posted: Mon Jun 11, 2012 5:19 am
by joejoe
Gotcha both on that, saulgoode and Patrick,

Never heard of octals but now I know better. :0)

Thanks again for pointing it out exactly!

Re: int does not like 08 and 09?

Posted: Mon Jun 11, 2012 7:39 am
by cormullion
I learnt that the hard way - an old post http://newlisper.wordpress.com/2006/09/18/my-mistake-2/ describes my experience in tedious detail...

Re: int does not like 08 and 09?

Posted: Mon Jun 11, 2012 2:16 pm
by Patrick
cormullion wrote:I learnt that the hard way - an old post http://newlisper.wordpress.com/2006/09/18/my-mistake-2/ describes my experience in tedious detail...
To be fair I found it a bit weird when I first read about it as well. I think having h and o prefixes would have been clearer, but I guess as long as one knows about it it's not too hard to avoid it as you have shown in your post.