"Thomas G. Lockhart" <lockhart@alumni.caltech.edu> writes:
> tgl=> select cash_mul_flt8('$1', '123.77');
> cash_mul_flt8
> -------------
> $123.76
> (1 row)
> That's annoying; it's non-symmetric too.
And hardware-dependent, evidently, because I get the right thing on HP:
play=> select cash_mul_flt8('$1', '123.77');
cash_mul_flt8
-------------
$123.77
(1 row)
> The money type is stored as an
> integer, and the float type is an IEEE double; looks like we have an LSB
> rounding problem. Not sure what to do about it other than remove the
> function, which isn't desirable I'm sure...
What's needed is explicit rounding. Instead of letting the compiler do
the rounding during its implicit float-to-int conversion (which
generally truncates towards 0 or towards -infinity), do
*** cash.c~ Wed Nov 4 10:11:57 1998
--- cash.c Wed Nov 4 10:13:06 1998
***************
*** 17,22 ****
--- 17,23 ---- #include <limits.h> #include <ctype.h> #include <locale.h>
+ #include <math.h> #include "postgres.h" #include "miscadmin.h"
***************
*** 419,425 **** if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed,
can'tmultiply cash", NULL);
! *result = ((*f) * (*c)); return result; } /* cash_mul_flt8() */
--- 420,426 ---- if (!PointerIsValid(result = palloc(sizeof(Cash)))) elog(ERROR, "Memory allocation failed,
can'tmultiply cash", NULL);
! *result = floor((*f) * (*c) + 0.5); return result; } /* cash_mul_flt8() */
(Alternatively you could do it with rint(), although I distrust rint()
because it's context-dependent...)
The other cash functions also need to be looked at for rounding
sloppiness, so I see no point in committing this fix by its lonesome.
Lessee, who's the author of cash.c ... looks like it's D'Arcy ...
D'Arcy, you wanna put this on your to-do list?
regards, tom lane