min/max return always float?

For the Compleat Fan
Locked
HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

min/max return always float?

Post by HPW »

Is this correct that min/max return always a float type?

Code: Select all

> (min 100 200 300)
100
> (integer?(min 100 200 300))
nil
> (float?(min 100 200 300))
true
Maybe it should be made more clear in the doc.
Hans-Peter

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

Post by Lutz »

As a rule of thumb: if the the operator/function is made up of letters it returns a floating point else an integer:

+, -, *, / ,%,$,~,|,^,<<,>> ; are all integer operators, everything else is float. All integer operators can take bith floating point and integer as parameters but the result will always be integers.

All floating point operators/functions can take both types as parameters, but the result will always be floating point.

I will include this in the manual.

Lutz
Last edited by Lutz on Wed Jan 26, 2005 6:36 pm, edited 1 time in total.

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

I think you mean:

+, -, *, / ,%,$,~,|,^,<<,>> ; are all integer operators, everything else is float. All integer operators can take both floating point and integer as parameters but the result will always be integer.
Hans-Peter

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

Post by Lutz »

yes, of course thanks, will edit the original post

Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Hi Lutz, HPW,

Just out of curiosity: why is there such a strict difference between integers and floats anyway?

I understand that they have a different format in memory and all that, but why has this difference impact on the actual mathematical commands in newLisp? Why not use the same mathematical functions for both integers and floats?

Actually, is there any other language with this difference - (except for additional commands like 'mod', to retrieve a rest after a division)? NewLisp is the only language I know so far with this strict definition.

Peter

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

Post by Lutz »

The difference are in the type of the return value and the way arithmetik is performed. When using +,-,*,/ you will have truncation of fractional values and roll over. It is also useful for maximum speed of imported functions handling integers. When 64bit is desktop mainstream we will have 64bit integers.

I am not aware of any other scripting language doing it this way, but I find it useful to have pure integer arithmetik available in some instances and with the typical behaviour described.

You can just do the (constant '+ add) - trick as desribed in in the manual and put it in you init.lsp file. newLISP will then behave exactly like any other scripting language keeping everything as double float and converting internally as required (i.e. bit shifts hex prints etc.).

Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Thanks Lutz, I see why it works like this now. Again, it was just curiosity of mine, not meant to be criticism. I already guessed there must be reason which I did not see.

Your trick with the (constant '+ add) is a good tip! Again thanks.

Locked