Upendra
Upendra

Reputation: 29

C++ double data type problem

Lets consider the following example. double x = 1234597000.0

When i save the value of x in DB, it is stored as 1234600000. When i do subtraction operation in program, it is getting treated as 1234600000. Would you please help me to understand what is going on there?

my system is solaris and compiling the program using Sun Studio C++(CC) compiler.

Thanks in advance.

Upvotes: 2

Views: 822

Answers (2)

cHao
cHao

Reputation: 86506

It looks like your double is getting converted to or from a float (read: losing precision) somewhere along the line. Floats only have about 6-7 significant digits IIRC, as opposed to over a dozen for doubles.

Upvotes: 2

yan
yan

Reputation: 20982

You're hitting the pitfalls of the IEEE floating-point encoding. This questions comes up quite a bit. Please see What Every Computer Scientist Should Know About Floating-Point Arithmetic

Upvotes: 6

Related Questions