Skip site navigation (1) Skip section navigation (2)

Re: [HACKERS] Warning!!

From: Bruce Momjian <maillist(at)candle(dot)pha(dot)pa(dot)us>
To: tgl(at)sss(dot)pgh(dot)pa(dot)us (Tom Lane)
Cc: lockhart(at)alumni(dot)caltech(dot)edu, darcy(at)druid(dot)net, pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] Warning!!
Date: 1998-12-12 03:54:06
Message-ID: 199812120354.WAA28277@candle.pha.pa.us (view raw or flat)
Thread:
Lists: pgsql-hackers
Should I add this to the TODO list?

> "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
> 
> 


-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist(at)candle(dot)pha(dot)pa(dot)us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

In response to

pgsql-hackers by date

Next:From: Bruce MomjianDate: 1998-12-12 03:56:53
Subject: Re: [HACKERS] v6.4 - duplicate entries in announce
Previous:From: Matthew N. DoddDate: 1998-12-11 23:39:58
Subject: Re: [HACKERS] JOIN syntax. Examples?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group