About math operators

Started by cavva, November 12, 2007, 05:11:43 AM

Previous topic - Next topic

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?

Lutz

#1
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">http://newlisp.org/newlisp_manual.html#int_float



Lutz

cavva

#2
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 ?!

Lutz

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

cormullion

#4
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">//http://newlisp.org/introduction-to-newlisp.html#workingwithnumbers

cavva

#5
@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 :)