Thread: Output float number with hex format

Output float number with hex format

From
沈雷
Date:
Hi!

In C Language, there is a way to format float numbers into a hex string by using "%a" in printf.
eg:
the value: 1.2345 can be expressed as '0x1.3c083126e978dp+0' which is the hex representation of a float number.

I have tried this in Postgres:
SELECT '0x1.3c083126e978dp+0'::float;
 float8
--------
 1.2345
(1 row)
This means that Postgres can accepty hex float as *input*. However I cannot find any format function for *output*.
to_char(..., ...) does not have "%a"-like format. Is there any way to do that?

Thank you in advance.

--
ShenLei


Re: Output float number with hex format

From
Vincenzo Romano
Date:
2010/1/29 沈雷 <drshenlei@gmail.com>:
> Hi!
>
> In C Language, there is a way to format float numbers into a hex string by
> using "%a" in printf.
> eg:
> the value: 1.2345 can be expressed as '0x1.3c083126e978dp+0' which is the
> hex representation of a float number.
>
> I have tried this in Postgres:
> SELECT '0x1.3c083126e978dp+0'::float;
>  float8
> --------
>  1.2345
> (1 row)
> This means that Postgres can accepty hex float as *input*. However I cannot
> find any format function for *output*.
> to_char(..., ...) does not have "%a"-like format. Is there any way to do
> that?
>
> Thank you in advance.
>
> --
> ShenLei

Well, your question sould be about a scanf, not printf, as this is
input, not output.
While being an "interesting" feature, I don't see and use for it.
And, in my opinion, this is not the first and only feature that works
and is not documented.
So, please, don't rely on it until it gets documented.

Re: Output float number with hex format

From
"A. Kretschmer"
Date:
In response to Vincenzo Romano :
> 2010/1/29 ?????? <drshenlei@gmail.com>:
> > Hi!
> >
> > In C Language, there is a way to format float numbers into a hex string by
> > using "%a" in printf.
> > eg:
> > the value: 1.2345 can be expressed as '0x1.3c083126e978dp+0' which is the
> > hex representation of a float number.
> >
> > I have tried this in Postgres:
> > SELECT '0x1.3c083126e978dp+0'::float;
> >  float8
> > --------
> >  1.2345
> > (1 row)
> > This means that Postgres can accepty hex float as *input*. However I cannot
> > find any format function for *output*.
> > to_char(..., ...) does not have "%a"-like format. Is there any way to do
> > that?
> >
> > Thank you in advance.
> >
> > --
> > ShenLei
>
> Well, your question sould be about a scanf, not printf, as this is
> input, not output.

I think, you have misinterpreted the question, he is looking for a
output in a hex representation, and he found a way for input.

Hrm, i think, this is possible with pl/perl. For instance.

A. Kretschmer
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99

Re: Output float number with hex format

From
沈雷
Date:
Thank you for the reply.

After 3 hours trials and reading the source code of Postgres backend, now I can figure out a way to transfer float data from sever to client and then write back to server *without lose any precision*.

At server part, it uses strtod to convert received string to float value, while it uses sprintf(str, "%.*g", DBL_DIG + extra_float_digits, val) to convert float value to string. Here the extra_float_digits == 0 in default and DBL_DIG ==15 which is defined in "float.h".

Therefore, at client part, we should use the *EXACT* same way to do conversion. I mean we should use sprintf(str, "%.15g", val) and strtod.

I have tested about 10000 random float values, it works fine. Interestingly, if I use "%.16g", "%.17g"...etc,  precision lost will occur.

--
ShenLei

2010/1/29 Vincenzo Romano <vincenzo.romano@notorand.it>
2010/1/29 沈雷 <drshenlei@gmail.com>:
> Hi!
>
> In C Language, there is a way to format float numbers into a hex string by
> using "%a" in printf.
> eg:
> the value: 1.2345 can be expressed as '0x1.3c083126e978dp+0' which is the
> hex representation of a float number.
>
> I have tried this in Postgres:
> SELECT '0x1.3c083126e978dp+0'::float;
>  float8
> --------
>  1.2345
> (1 row)
> This means that Postgres can accepty hex float as *input*. However I cannot
> find any format function for *output*.
> to_char(..., ...) does not have "%a"-like format. Is there any way to do
> that?
>
> Thank you in advance.
>
> --
> ShenLei

Well, your question sould be about a scanf, not printf, as this is
input, not output.
While being an "interesting" feature, I don't see and use for it.
And, in my opinion, this is not the first and only feature that works
and is not documented.
So, please, don't rely on it until it gets documented.