[ic] Really strange [calc] evaluation

Peter peter at pajamian.dhs.org
Wed Jan 27 03:02:42 UTC 2010


On 27/01/10 15:44, Grant wrote:
>>> The following should evaluate to zero:
>>>
>>> [calc]197.7 - (32.95 * 6)[/calc]
>>>
>>> but instead I get:
>>>
>>> -2.8421709430404e-14
>>>
>>> Everything else is functioning normally.  Does anyone know what's
>>> going on here?  I'm on 5.6.1.
>> Standard floating point arithmetic. Same thing occurs in every programming
>> language that uses FP. Use rounding (printf) or CPAN modules to handle it.
> 
> OK, I'd like to change the code so that it produces the correct
> result.  I tried:
> 
> [calc]sprintf("%.0f",(197.7 - (32.95 * 6)))[/calc]
> 
> but I get -0.  This situation seems pretty strange to me.  How can I
> get [calc] to calculate reliably?

[calc] is just perl, so any floating point errors it shows are just
passed along from perl.

This seems to work:
print sprintf(q{%f}, 197.7 - (32.95 * 6)) + 0


Peter




More information about the interchange-users mailing list