About math operators

Notices and updates
Locked
cavva
Posts: 16
Joined: Sun Nov 11, 2007 2:45 am

About math operators

Post 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?

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

Post 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

cavva
Posts: 16
Joined: Sun Nov 11, 2007 2:45 am

Post 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 ?!

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

Post 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

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

Post 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

cavva
Posts: 16
Joined: Sun Nov 11, 2007 2:45 am

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

Locked