int does not like 08 and 09?

Q&A's, tips, howto's
Locked
joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

int does not like 08 and 09?

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

saulgoode
Posts: 10
Joined: Sat Jul 16, 2011 6:15 am

Re: int does not like 08 and 09?

Post by saulgoode »

Numeric constants that start with a zero are treated as octal numbers, therefore '8' and '9' are not valid digits.

Patrick
Posts: 7
Joined: Fri May 25, 2012 5:15 pm

Re: int does not like 08 and 09?

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

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: int does not like 08 and 09?

Post 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!

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: int does not like 08 and 09?

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

Patrick
Posts: 7
Joined: Fri May 25, 2012 5:15 pm

Re: int does not like 08 and 09?

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

Locked