newLISP Fan Club

Forum => newLISP newS => Topic started by: cavva on November 12, 2007, 05:11:43 AM

Title: About math operators
Post by: cavva on November 12, 2007, 05:11:43 AM
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?
Title:
Post by: Lutz on November 12, 2007, 05:42:06 AM
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:


(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
Title:
Post by: cavva on November 12, 2007, 06:06:50 AM
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 ?!
Title:
Post by: Lutz on November 12, 2007, 09:03:01 AM
Quote from: "cavva"(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
Title:
Post by: cormullion on November 12, 2007, 09:07:25 AM
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-newlisp.html#workingwithnumbers
Title:
Post by: cavva on November 12, 2007, 09:54:49 AM
@Lutz


Quote from: "Lutz"
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?
Quote


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