From: Richard Frankland <rfrankla@yahoo.com>
To: Sun, Way <Way.Sun@sciatl.com>
To: pgsql-interfaces@postgresql.org
Subject: Re: libpq: PQexecParams, binaryFormat and float
Date: Mon, 9 Jul 2007 15:46:06 -0700 (PDT)
Message-ID: <959570.69903.qm@web32613.mail.mud.yahoo.com> (raw)
In-Reply-To: <7FBC16D72C0ABF45A88EAEB9ED3464C48CC172@sausatlexch01.corp.sa.net>
Hi, This is my first reply/post to postgres forums. I hope I'm doing it OK.
If not, please help.
So, onto how I see the problem and a possible solution.
As Tom Lane pointed out. Using a "union" is another solution to this problem.
Here is a simple testing program and output:
#include <stdio.h>
/* for ntohl/htonl */
#include <netinet/in.h>
void show_hex (void * vptr) {
unsigned char * xptr;
int k;
/* display each byte in heX */
xptr = (unsigned char *)vptr;
for (k=0; k<4; k++)
{
printf("%.2X ", *(xptr+k));
}
printf("\n");
}
int
main(int argc, char **argv)
{
float starting_value = 1.23;
float loadavg;
unsigned long reversed_float;
/* prove we really are starting with 1.23 */
printf( "%f \n", starting_value);
show_hex(&starting_value);
/* see man page for return type of htonl() */
reversed_float = htonl(starting_value);
show_hex(&reversed_float);
/* implicit conversion shown explicitly (i.e. has no effect) */
reversed_float = htonl((long)starting_value);
show_hex(&reversed_float);
/* and another implicit conversion */
loadavg = reversed_float; //htonl(starting_value);
show_hex(&loadavg);
/* again, shown explicitly */
loadavg = (float)(unsigned long)16777216; //htonl(starting_value);
show_hex(&loadavg);
loadavg = (float)(unsigned long)htonl((long)starting_value); //htonl(starting_value);
show_hex(&loadavg);
/* show what was stored into loadavg */
printf( "%f \n", loadavg);
/* show this is the same as the original problem */
loadavg = htonl(1.23);
printf( "%f \n", loadavg);
/* A (not the only one) correct solution */
/* Casting a pointer: */
/* address of source */
/* | */
/* pointer to UNSIGNED type | */
/* | | */
/* dereference pointer | | */
/* | | | */
/* | | | */
/* | | | */
reversed_float = htonl(*(unsigned long *)&starting_value);
show_hex(&reversed_float);
/*
* paramValues[0] = (unsigned char *) &reversed_float;
*/
}
------------------------------------------------------
$ ./tst
1.230000
A4 70 9D 3F
00 00 00 01
00 00 00 01
00 00 80 4B
00 00 80 4B
16777216.000000
16777216.000000
3F 9D 70 A4
$
"Sun, Way" <Way.Sun@sciatl.com> wrote:
Hi,
I'm having trouble inserting a float into pg using binary format.
Hope someone on the list can help me point out what I'm doing wrong....
float loadavg;
loadavg = htonl(1.23);
paramValues[0] = (char *) &loadavg
paramLengths[0] = sizeof(loadavg);
paramFormats[0] = 1;
res = PQexecParams(conn,
"INSERT INTO cpu (loadavg) VALUES ($1::real)",
1,
NULL,
paramValues,
paramLengths,
paramFormats,
1);
From psql, select returns the value as 1.20765e-38 instead of 1.23.....:(
Thanks
Way
---------------------------------
- - - - - Appended by Scientific Atlanta, a Cisco company - - - - -
This e-mail and any attachments may contain information which is confidential,
proprietary, privileged or otherwise protected by law. The information is solely
intended for the named addressee (or a person responsible for delivering it to
the addressee). If you are not the intended recipient of this message, you are
not authorized to read, print, retain, copy or disseminate this message or any
part of it. If you have received this e-mail in error, please notify the sender
immediately by return e-mail and delete it from your computer.
---------------------------------
Boardwalk for $500? In 2007? Ha!
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-interfaces@postgresql.org
Cc: rfrankla@yahoo.com, Way.Sun@sciatl.com
Subject: Re: libpq: PQexecParams, binaryFormat and float
In-Reply-To: <959570.69903.qm@web32613.mail.mud.yahoo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox