min/max return always float?

Started by HPW, January 26, 2005, 02:40:32 AM

Previous topic - Next topic

HPW

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



> (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

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

HPW

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

#3
yes, of course thanks, will edit the original post



Lutz

pjot

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

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

#6
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.