integer functions on floats

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

integer functions on floats

Post by Dmi »

Lutz, I just got an idea:

Code: Select all

(+ 1.5 2.3) => 3
In my own practice, this implicit conversion from a float to an integer in functions, such as (+) and (-) mostly causes a mistakes and is really useful in rare cases.

Probably, it will be good - to supress the conversion (throw a type error) and force programmer convert it explicitly?
WBR, Dmi

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

Post by Lutz »

I know some using this. Those who don;t like it should put:

Code: Select all

(constant (global '+) add)
   ...
in there init.lsp or ~/.init.lsp

Code: Select all

> (constant (global '+) add)

> (+ 1.2 3.4)
4.6
> 

Then there will be always implicit conversion to float only like in Perl/Python etc., and only when writing library interfaces the (int ...) cast is used. I believe throwing errors will confuse more as all other functions also convert from/to int/float as needed.

Lutz

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Thanx for a trick - now my financial calculations will lose the cents less frequently :-)

Simply I've think about absence of such autoconversion for strings and got an idea, that the integers relyes to the floats like to the strings...
Just an idea... :-)
WBR, Dmi

Locked