2009/7/19 Octavian Râşniţă <orasn...@gmail.com>:
> From: "Chas. Owens" <chas.ow...@gmail.com>
>>
>> What would be the problem if the way of doing calculations by using bignum
>> would be the default?
>> Does it give errors? It would be helpful to know.
>>
>> Octavian
>
>> Because it is slower.
>
> Well, PHP does it faster than Perl, even if it also hide the inaccuracy.
>
snip

You still don't get it.  PHP is not faster than Perl for floating
point operations.  It runs at the same speed because both use the same
hardware.  PHP just lies to you about what the result is.  Here is
proof that PHP has the same problem:

<?php
system("perl -le 'print 0.79 - 0.798 + .008'");
$x = 0.79 - 0.798 + .008;
print "$x\n";
?>

>> If you want to use bignums by default you can use the
>> [bignum][0] pragma, but be warned, there is a massive performance penalty:
>
> You are right, and that's why I try to make a module that just uses
> sprintf() or that just first multiplies the numbers with say.. a million, do
> the operation and then divide by a million, but I need to make that module
> work like bignum, without needing to define objects in my application, but
> just do:
>
> use MyModule;
> print 0.079 - 0.085;
>
> This should be clean enough but I couldn't find yet how I can overload some
> operators with float numbers.
snip

This really shows that you don't get it.  If you round at every step
you will compound the error rounding adds.  All you need to do is
round the number at display time.  Just switch your print statements
to printf statements and you will be fine.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to