Hi Folk:
    I am working on a project to calculate the average cost of products
which is made of a lot of small parts. In order to find out the cost of
a product, I have to find out the cost of all its parts. However, I am
facing some problem with the the decimal pleace. The range of the parts
values is about $0.00001 - $3.00. When I multiple these price with the
quantity something around 0.001 -10, the result returned by the script
will be inaccurated. Let say, I doing something like:
    $0.05126 * 2  =  $0.10252
supposingly, but the result will become something like
    0.102519999999997986.
    I think this is the problem of my CPU or the complier that they
can't represent the decimal number precisely in binary mode underneath.
Althougth I can round off the result to get a pretty good answer for
each part, the cost of the final product becomes unacceptable after
summing up the price of all litte parts.
    I, then, wrote a small script to tried out this problem with simple
calculation. I did:
    3.03313 - 3.03312
obviously, the answer should be
    0.00001
but the result of the script becomes:
    9.99999999997669e-06
    These several day, I've tried to find something like BCD (Binary
Coded Decimal) for Perl, but nothing had been found. Therefore, I'm
wondering may anybody out there could help me with this problem. I wanna
know is there any third party module that can do the calculation in BCD
mode for perl. Actually, I've tried the Math::BigFloat, and
Math::FixedPrecision, but I find that they mainly limit the number
decimal pleaces to be calculated or the round off problem. This is not
something I am really looking for.
Please, find me some method.

Thank You very Much.

Harry Yau

Reply via email to