dbTalk Databases Forums  

Precision of stored doubles

mailing.database.mysql-plusplus mailing.database.mysql-plusplus


Discuss Precision of stored doubles in the mailing.database.mysql-plusplus forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Nis Haller Baggesen
 
Posts: n/a

Default Precision of stored doubles - 03-17-2006 , 09:58 AM






I'm having a problem where the doubles I try to store in my MySQL
database using the macro generated smart types in MySQL++. I tried
searching the mailing list for problem concerning 'double' and
'precision' but didn't really get anything.

Eg. when storing a number like 1142531234.0 it will get truncated to
1142530000.0 in the database. There seems to be no problem loading a
number like 1142531234.0 from the database.

Curiously this loss of precision is exactly the same as the precision
loss I would get when streaming out the number without using the
setprecision(x) stream operator. Going from the code in sql_string.cpp I
wonder if the two where somehow related. However changing the double
conversion from

SQLString::SQLString(double f) :
is_string(false),
dont_escape(false),
processed(false)
{
ostringstream outs;
outs << f;
assign(outs.str());
}

to

SQLString::SQLString(double f) :
is_string(false),
dont_escape(false),
processed(false)
{
ostringstream outs;
outs << setprecision(15) << f;
assign(outs.str());
}

and recompiling the library did not in itself seem to do anything, so I
was wondering if the conversion was happening using some other method.

The exact type giving me problems is

sql_create_8(Skill,
2, 8,
std::string, actorId,
std::string, skill,
int, level,
bool, inTraining,
int, trainPoints,
double, trainTimeStamp,
int, inspirationPoints,
bool, activated)

when stored using a simple

Skill data(...);
query.replace(data);

call to store it.

Any pointer would be appreciated. Otherwise thanks for a very nice
wrapper library definately made my life simpler.

mvh

Nis Haller Baggesen



--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw


Reply With Quote
  #2  
Old   
Warren Young
 
Posts: n/a

Default Re: Precision of stored doubles - 03-17-2006 , 09:58 PM






Nis Haller Baggesen wrote:
Quote:
Eg. when storing a number like 1142531234.0 it will get truncated to
1142530000.0 in the database.
Yes, it seems that it's common for the C and C++ standard libraries to
use only 6 digits of precision by default.

Quote:
SQLString::SQLString(double f) :
is_string(false),
dont_escape(false),
processed(false)
{
ostringstream outs;
outs << setprecision(15) << f;
assign(outs.str());
}

and recompiling the library did not in itself seem to do anything,
This fix is actually needed, but it only affects template queries, not
SSQLS queries. The correct fix is slightly different, but you've
basically nailed it. You also have to change several places in query.h
to fix this for SSQLS.

I've made these changes to the v2.1 branch. I'm probably going to
release a beta of it (i.e. "feature complete") soon. I had other plans
for things to add to v2.1, but the underwhelming amount of testing for
the current v2.1 alphas tells me I need to get what I have released if I
want it tested. So, look for this fix to appear in a released version
fairly soon.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.