is this a bug?

Q&A's, tips, howto's
Locked
tomtoo
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

is this a bug?

Post by tomtoo »

Code: Select all

> (set 'a "0821")
"0821"
> (int a)
0
shouldn't this return 0821?

Code: Select all

> (integer? 0821)
true

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

Re: is this a bug?

Post by cormullion »

Not really - at least, not in newLISP... You didn't specify a number base, and then you announced an octal number with the initial "0". But then you blew it with the "8", which is not a valid octal digit. Also, you didn't specify a default value in the event of a failed conversion, so you got the default default, which is 0.

Try:

Code: Select all

(int a 0 10)

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: is this a bug?

Post by jopython »

I will sanitise the input before conversion

Code: Select all

(int (trim "0034234230" "0" ""))
=> 34234230

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: is this a bug?

Post by Lutz »

also consider speed:

Code: Select all

> (time (int "0821" 0 10) 1000000)
95.273
> (time (int (trim "0821" "0" "")) 1000000)
530.1
> 

Locked