Search Postgresql Archives

Re: dividing integers not producing decimal fractions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Nov 03, 2006 at 02:03:59PM -0800, rloefgren@xxxxxxxxxxxxxxx wrote:
> You're right (I dug around in the documentation and edjoocated myself).
> However:

<snip>

> sales=# select 1/2::float;
>  ?column?
> ----------
>       0.5
> (1 row)

Note that in this case the "float" cast only applies to the last
number. That's why you get this:

> sales=# select (1/2)*4::float;
>  ?column?
> ----------
>         0
> (1 row)

The integer divide happens first. It is best to apply the cast to the
first element of the expression, as expressions are parsed
left-to-right, so:

select (1::float/2)::4;

Works better. However, mostly it's better to explicitly make all your
constants non-integer if that's what you mean. This statement:

select (1.0/2.0)*4.0;

Gives the same result, but doesn't need any casts.

Have a nice day,
-- 
Martijn van Oosterhout   <kleptog@xxxxxxxxx>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment: signature.asc
Description: Digital signature


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux