Обсуждение: BUG #1888: bug in cast from float4 to float8

Поиск
Список
Период
Сортировка

BUG #1888: bug in cast from float4 to float8

От
"Pablo Oses"
Дата:
The following bug has been logged online:

Bug reference:      1888
Logged by:          Pablo Oses
Email address:      opablo@gmail.com
PostgreSQL version: 8.0.x and 7.2.x
Operating system:   redhat, slackware and win32
Description:        bug in cast from float4 to float8
Details:

this is *really* simple

execute something like this:
select cast(123.456::float4 as float8)

what do you think its the answer ? 123.456 ?

well pgsql 7.2.2, 8.0.0, 8.0.1 and 8.0.3 (all i have used) answers this:
123.456001281738 which is WRONG

bye and thanks for a wonderfull contribution for the free software comunity

Re: BUG #1888: bug in cast from float4 to float8

От
Tom Lane
Дата:
"Pablo Oses" <opablo@gmail.com> writes:
> select cast(123.456::float4 as float8)

> what do you think its the answer ? 123.456 ?

> well pgsql 7.2.2, 8.0.0, 8.0.1 and 8.0.3 (all i have used) answers this:
> 123.456001281738 which is WRONG

This is not a bug.  Please go learn something about floating-point
arithmetic (or if you don't care to, use type numeric instead).

            regards, tom lane

Re: BUG #1888: bug in cast from float4 to float8

От
Michael Fuhr
Дата:
On Sat, Sep 17, 2005 at 04:00:50AM +0100, Pablo Oses wrote:
> select cast(123.456::float4 as float8)
>
> what do you think its the answer ? 123.456 ?
>
> well pgsql 7.2.2, 8.0.0, 8.0.1 and 8.0.3 (all i have used) answers this:
> 123.456001281738 which is WRONG

Float4 has a precision of only about 6 digits, so you can't assume
much about the digits beyond that.  See the documentation for
floating-point types:

http://www.postgresql.org/docs/8.0/interactive/datatype.html#DATATYPE-FLOAT

This is hardly a behavior unique to PostgreSQL; use a search engine
to find explanations of the problems of using binary floating-point
numbers.

If you need exact results then use the numeric type.

--
Michael Fuhr