int does not like 08 and 09?

Started by joejoe, June 10, 2012, 08:06:13 PM

Previous topic - Next topic

joejoe

> (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:


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


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

(int (nth 1 my-nums))

;-> 0


I managed to get float to come to my rescue:



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

;-> 8


What's up w/ int and the 08 and 09 numbers?



Thank you.

saulgoode

#1
Numeric constants that start with a zero are http://en.wikibooks.org/wiki/Introduction_to_newLISP/Working_with_numbers">treated as octal numbers, therefore '8' and '9' are not valid digits.

Patrick

#2
See http://www.newlisp.org/downloads/newlisp_manual.html#int">here in the documentation


QuoteThe 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

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

#4
I learnt that the hard way - an old post http://newlisper.wordpress.com/2006/09/18/my-mistake-2/">//http://newlisper.wordpress.com/2006/09/18/my-mistake-2/ describes my experience in tedious detail...

Patrick

#5
Quote from: "cormullion"I learnt that the hard way - an old post http://newlisper.wordpress.com/2006/09/18/my-mistake-2/">//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.