Strange predicate behavior?

Q&A's, tips, howto's
Locked
bfulgham
Posts: 1
Joined: Mon Sep 13, 2004 6:15 am
Location: California
Contact:

Strange predicate behavior?

Post by bfulgham »

If I do this:

Code: Select all

> (integer? 25)
true
So far, so good. but what about:

Code: Select all

> (integer? (sqrt 25))
nil
Huh? Am I doing something wrong? It's almost as though integer?
is evaluateing the sqrt function, rather than the result of the function.

Am I misunderstanding how newLisp evaluates data?

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

Post by Lutz »

All math functions return floats. Only +, -, * , /, % and the bit manipulating functions return integers.

This is one area where newLISP is different from most scripting languages which do all math in (double) floats internally. You can have the same in newLISP redefining +,-,/* this way (see chapter "Customization ..." in the manual):

(constant '+ add)
(constant '- sub)
(constant '* mul)
(constant '/ div)
(constant '% mod)

preserving pure integer arithmetic is useful when interfacing with some 'C' library functions or compiling newLISP for CPUs incapable of floating point operations and when doing extensive bit-fiddleing.

'C' library imports in newLISP are very short (think 'scripting') because they don't have to declare types of parameters passed. This makes it necessary to distinguish between integers and floats in newLISP. If library imports are of little concern overload +,-,*,/,% as shown above.

Functions which require integers in newLISP, i.e. indices into lists/arrays etc. automatically convert floats into integers. All functions requiring floats convert integers to floats as well.

Lutz

Locked