integer to float

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

integer to float

Post by newdep »

Hello Lutz,

Its it possible to extend Newlisp with an option in + - / * ... to
return not only integers but also autodetect i.e. floats.. and round
in float format? Currently an extra function is needed that does a
pack/unpack for float.

An examples on how i thought it could be done for float is i.e. to
add an "f" at the end of float numbers. But ofcaurse more elegant would
be an autodetect... (btw i know mul, add... exist ;-)

; a float
>(- 1000.54f 100.31f )
900.23

; a float/double
>(+ 1000.123456789123456 1.1234567789123456)

Or if someting like this would work it would be nice too ->

(float (- 123.34 35.123))


Regards,
Norman.
-- (define? (Cornflakes))

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

Post by Lutz »

Just do at the beginning or in init.lsp:

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

Now +,-,*,/ will work on floats and integers alike. This is also described in the manual in the 'Customization, localization and UTF-8' chapter.


Lutz

Locked