Re: floating point representation

From: Pete Forman <pete(dot)forman(at)westerngeco(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Hiroshi Inoue <Inoue(at)tpf(dot)co(dot)jp>, Peter Eisentraut <peter_e(at)gmx(dot)net>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: floating point representation
Date: 2001-02-20 09:40:30
Message-ID: 14994.15246.973584.711036@kryten.bedford.waii.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Tom Lane writes:
> Hiroshi Inoue <Inoue(at)tpf(dot)co(dot)jp> writes:
> > Tom Lane wrote:
> >> The defaults
> >> would be "%.7g" and "%.17g" (or thereabouts, not sure what number of
> >> digits we are currently using).
>
> > Wouldn't changing current '%.6g','%.15g'(on many platforms)
> > cause the regression test failure ?
>
> I didn't check my numbers. If the current behavior is '%.6g','%.15g'
> then we should stay with that as the default.
>
> Hmm, on looking at the code, this might mean we need some configure
> pushups to extract FLT_DIG and DBL_DIG and put those into the default
> strings. Do we support any platforms where these are not 6 & 15?

Please remind me what we are trying to do. 6 & 15 are values to
suppress trailing digits at the end of a number in a standard printf.
For example, 0.1 prints as 0.10000000000000001 at %.17g but as 0.1 at
%.16g. However those shorter formats are less precise. There are
several other doubles that will also print the same result. A round
trip of printf/scanf will not generally preserve the number.

Printing for display purposes may not be adequate for dumping with a
view to restoring. Are we talking about display or dump?

The ideal is to print just enough digits to be able to read the number
back. There should be no redundant digits at the end. Printf is
unable to do this by itself. The reason is that the correct number of
decimal digits for a %.*g is a function of the number being printed.

There are algorithms to do the right thing but they can be expensive.
I play with some in a program at the URI below. There is a minor typo
in the usage and a missing (optional) file. I'll correct those when
the site allows uploads again. The files' contents are currently
available at http://petef.8k.com/.
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent
pete(dot)forman(at)westerngeco(dot)com -./\.- opinion of Schlumberger, Baker
http://www.crosswinds.net/~petef -./\.- Hughes or their divisions.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Rainer Mager 2001-02-20 09:58:18 JDBC bug in 7.1b4
Previous Message Sezai YILMAZ 2001-02-20 09:24:59 Re: Turkish locale bug