Can someone tell me if I am missing anything here?
Needs of a currency class for GnuCash:
==========================
Assumption: Unless otherwise specified, all currencies are the same
denomination (all dollars, all lira, all pounds sterling, etc)
We must be able to add two monetary values with commutivity/associativit
y
We must be able to subtract two monetary values
We must be able to multiply a monetary value with an arbitrary real.
We do not need to be able to multiply two monetary values (i.e., square
dollars are meaningless).
We must be able to handle a large range of values precisely, ranging
from
$0.01 to $20,000,000,000,000.00 or more. For practicality, there must
be a maximum value, but at that maximum value, it -must- maintain
cent-level precision (or pence-level, or yen-level, or whatever the
minimum granularity for that denomination is).
The details of the denomination (such as precision, display
characteristics,
etc) can be kept separate from the actual values, because the
denomination
can be determined from context if needed (such as stored as part of the
account structure, etc).
Conversions between two different denominations will be done explicitly,
and the denomination-specific info and exchange rates can be specified
to any conversion routine.
===========================
I propose the following simple class for currency calculations:
class Currency {
g_int64 value;
public:
Currency(g_int64 v) { value = v};
Currency add(const Currency &addend)
{ return new Currency( value + addend.value)};
Currency sub(const Currency &subend)
{ return new Currency( value - subend.value)};
Currency mul(const double &multend)
{ return new Currency( (g_int64) ((double)value*multend + 0.5))};
int comp (const Currency &compend)
{ if (value < compend.value) return -1;
if (value > compend.value) return 1;
return 0;
}
}
inline Currency operator+ (const Currency &addor,const Currency &addend)
{ return addor.add(addend) };
inline Currency operator- (const Currency &subor,const Currency &subend)
{ return subor.sub(subend) };
inline Currency operator* (const Currency &mulor,const double &mulend)
{ return mulor.mul(mulend) };
inline Currency operator* (const double &mulend,const Currency &mulor)
{ return mulor.mul(mulend) };
inline int operator< (const Currency &compor, const Currency &compend)
{ return compor.comp(compend) < 0; };
etc...
I'll admit, I haven't written much C++ in a while and this is being
written off the cuff into a mail client, but I think I have the basics
down to add the functionality we want.
Please note: As long as we don't do any multiplication, this will
allow us to properly handle dollar values as large as
$92,233,720,368,547,758.07 without loss of precision. Assuming I have
the mantissa right, IEEE 64-bit FP will start to mess up around
$22,517,998,136,852.48 (+/- a factor of two or so). While $22 trillion
is probably "sufficient" for most purposes, it's nice to know that we
can go as high as $92 quadrillion if we need to...
So, what are the objections to this approach?
--
Buddha Buck [EMAIL PROTECTED]
"Just as the strength of the Internet is chaos, so the strength of our
liberty depends upon the chaos and cacophony of the unfettered speech
the First Amendment protects." -- A.L.A. v. U.S. Dept. of Justice
--
Gnucash Developer's List
To unsubscribe send empty email to: [EMAIL PROTECTED]