Re: Rounding issue
- From: Jürgen Exner <jurgenex@xxxxxxxxxxx>
- Date: Tue, 18 Aug 2009 13:24:27 -0700
Marten Lehmann <lehmannmapson@xxxxxx> wrote:
But what will do for you (if it's possible at all) depends on
what exactly you want to do.
I want to do financial calculations (nothing special, just simple things
like adding items of an invoice, adding taxes, removing disagio from
creditcard transactions etc.) and I want to be sure of correct results.
Then I _strongly_ suggest to brush up on computer numerics. Writing a
financial application that is sound is anything but trivial. So far you
discovered merely the tip of the iceberg of potential issues.
So if a total with tax is $0.015, then I need it to be rounded to $0.02
and not $0.01.
Just one other tiny glimpse into the peculiarities of financial
applications: If you have 5 of those 0.015$ amounts, should their total
be 0.75 or should their total be 1.00?
And there are numerous other pitfalls....
Therefor, numbers need to be represented as they are and
not the typical internal representation.
With "as they are" I suppose you mean in the decimal system.
No, Perl does not provide BCD natively (Binary Coded Decimals; someone
else already pointed that out). The reason is simple: they not only take
up more space (probably a minor issue on modern hardware) but more
important most hardware does not support BCD arithmetic, therefore all
calculations would have to be handcoded in software, making them very
slow.
I don't want to use cents as
integers. Even cents will get odd numbers after the decimal point if you
have to add e.g. 19% value added taxes. And even worser: The whole code
would get bloated and the original intention of the code would be harder
to understand if the whole source is full of "* 100" and "/ 100". In
I think you still don't recognize the basic issue: what you are asking
for cannot be done. Not because of a limitation in the binary system,
but because of a fundamantal mathematical limitation: if you have a tax
of 1/3 of one dollar, then how do you suggest to represent that value in
your favoured decimal system. You can not as we all know from 3rd grade
math. Therefore you have to deal with those rounding imperfections, no
matter which system (binary, decimal, ...) you are using. Just saying "I
want to use decimal instead of binary" doesn't absolve you from that
requirement.
How to deal with them is being tought in Computer Numerics, and that's
why I suggest several times already to brush up on that.
databases it is easy to define a decimal like (10,2): 10 digits before
the point, 2 after it.
And you will still run into the same fundamental limitations.
It would be great to have this directly in Perl
so that sprintf and other functions can still work with such values and
variables, not only specific modules like Math::Decimal.
Well, you got a few pointers to maybe helpful modules. If they don't
suit you needs then you are always free to write your own BCD module.
But as I said, even BCD won't solve your rounding problem, they "bad"
numbers will just be different.
jue
.
- Follow-Ups:
- Re: Rounding issue
- From: Alan Curry
- Re: Rounding issue
- References:
- Rounding issue
- From: Marten Lehmann
- Re: Rounding issue
- From: Ben Morrow
- Re: Rounding issue
- From: Marten Lehmann
- Re: Rounding issue
- From: Jens Thoms Toerring
- Re: Rounding issue
- From: Marten Lehmann
- Re: Rounding issue
- From: Jens Thoms Toerring
- Re: Rounding issue
- From: Marten Lehmann
- Rounding issue
- Prev by Date: Re: search for hex characters in a binary file and remove them
- Next by Date: Re: revisiting web development in Perl...where to start?
- Previous by thread: Re: Rounding issue
- Next by thread: Re: Rounding issue
- Index(es):
Relevant Pages
|