Page 1 of 1
About math operators
Posted: Mon Nov 12, 2007 1:11 pm
by cavva
Hi all,
i'm movig my first step with newLISP and Lisp in general,
I'm just curious about the +,-,*,/, ecc...
why they return only integer number?
Posted: Mon Nov 12, 2007 1:42 pm
by Lutz
The operators +,-,*,/ do only integer (64 bit) arithmetic.
Use add,sub,mul,div for float (IEE 754 double float) and mixed arithmetic.
You can make +,-,*,/ act like add.sub,mul,div doing:
Code: Select all
(constant '+ add)
(constant '- sub)
(constant '* mul)
(constant '/ div)
but you will lose the precision of 64 bit integers. Most newLISP users rapidly get accustomed using both +,-,*,/ and add,sub,mul,div.
See also here:
http://newlisp.org/newlisp_manual.html#int_float
Lutz
Posted: Mon Nov 12, 2007 2:06 pm
by cavva
umm...
but if i want integer precision then i can "cast" to integer right?
someting like this
(int (add 1 1.5 2))
probably it works only on 32 bit integer ?!
Posted: Mon Nov 12, 2007 5:03 pm
by Lutz
cavva wrote:(int (add 1 1.5 2))
it would give you back an integer but everything coming out of floating point has only about 15 digits of precision, while with int yuu get to about 18 digit. Casting to an int doesn't give you back precision and will cut off all decimals.
Also: all functions in newLISP "know" if they need an integer or float and will automatically convert, so casting normally is only necessary when calling routines imported from C libraries.
Lutz
Posted: Mon Nov 12, 2007 5:07 pm
by cormullion
I've written a little about working with numbers, and some of the things to be aware of. I hope it might be of some help:
http://newlisp.org/introduction-to-newl ... ithnumbers
Posted: Mon Nov 12, 2007 5:54 pm
by cavva
@Lutz
Lutz wrote:
it would give you back an integer but everything coming out of floating point has only about 15 digits of precision, while with int yuu get to about 18 digit. Casting to an int doesn't give you back precision and will cut off all decimals.
thanks Lutz,
didn't know about the difference in the digit precision,
i think i must get some acknowledgement.
What about this?
Also: all functions in newLISP "know" if they need an integer or float and will automatically convert, so casting normally is only necessary when calling routines imported from C libraries.
is something simple as "call" 'integer? or 'float? on the function arguments or something else?
@cormullion
Thanks, your document was my starting point, but i don't read well the chapter 9 :)