Re: [HACKERS] Warning!!

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Thomas G(dot) Lockhart" <lockhart(at)alumni(dot)caltech(dot)edu>, darcy(at)druid(dot)net
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] Warning!!
Date: 1998-11-04 15:23:42
Message-ID: 8408.910193022@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

"Thomas G. Lockhart" <lockhart(at)alumni(dot)caltech(dot)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't multiply cash", NULL);

! *result = ((*f) * (*c));

return result;
} /* cash_mul_flt8() */
--- 420,426 ----
if (!PointerIsValid(result = palloc(sizeof(Cash))))
elog(ERROR, "Memory allocation failed, can't multiply 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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas G. Lockhart 1998-11-04 15:24:31 Re: [HACKERS] problem with latest snapshot
Previous Message Thomas G. Lockhart 1998-11-04 15:19:03 Re: [HACKERS] Warning!!