newLISP Fan Club

Forum => Anything else we might add? => Topic started by: HPW on January 26, 2005, 02:40:32 AM

Title: min/max return always float?
Post by: HPW on January 26, 2005, 02:40:32 AM
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.
Title:
Post by: Lutz on January 26, 2005, 05:26:23 AM
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
Title:
Post by: HPW on January 26, 2005, 07:29:55 AM
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.
Title:
Post by: Lutz on January 26, 2005, 10:35:59 AM
yes, of course thanks, will edit the original post



Lutz
Title:
Post by: pjot on January 27, 2005, 01:03:35 PM
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
Title:
Post by: Lutz on January 27, 2005, 01:34:10 PM
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
Title:
Post by: pjot on January 27, 2005, 01:40:30 PM
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.