Strange math behaviour... could someone explain this ?

Mark Waddingham 36degrees at runrev.com
Sun Oct 9 08:06:38 EDT 2005


I strongly agree that 'trunc' should come with a warning in the
documentation as to its nature. Indeed, the corresponding function in
perl (int) is documented quite clearly:

"You should not use this function for rounding: one because it truncates
towards 0, and two because machine representations of floating point
numbers can sometimes produce counterintuitive results.  For example,
"int(-6.725/0.025)" produces -268 rather than the correct -269; that’s
because it’s really more like -268.99999999999994315658 instead."

Any cross-platform differences with 'trunc' will be down to the
different implementations of:
  - the floating point instructions on the underlying processors
  - the implementation of the fp glue code that applications use
  - the default rounding modes utilised by the various OSes
(All three together actually define the 'machine representation' that
applications have to work with)

Ultimately, trunc is a raw function. It gives you precisely what you ask
for with no 'fluff'. It is just that what you are actually asking for
and what you think you are asking for in this instance are not the same
thing.

Warmest Regards,

Mark.

On Fri, 2005-10-07 at 21:30 +0200, jbv wrote:
> 
> Alex,
> 
> > Users of trunc() (whichever language they use it in :-) should be wary
> > of the dangers.
> >
> 
> Thank you so much for your explanations. As for rev users, I strongly
> suggest that this "potential problem" should be mentioned in the doc,
> at least in the part dedicated to the trunc() function...
> 
> Nevertheless, how do you explain that the following :
> 
>   put 36 into A
>   put 34.2 into B
>   put A-B into C
> 
>   put C*100 into C
>   put trunc(C)
> 
> returns 180 with Rev 2.5 on Mac OS9, but returns 179 on other
> platforms such as Linux or Win2000 ?
> 
> Just curious...
> 
> JB
> 
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution

------------------------------------------------------------------
 Mark Waddingham ~ 36degrees at runrev.com ~ http://www.runrev.com
       Runtime Revolution ~ User-Centric Development Tools




More information about the use-livecode mailing list