[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Weird problem with DOUBLE datatype in VC++

Hi Experts,

I am encountering a weird problem using double datatype in my VC++ application.

For ex:

double x = 132.23

When I debug what "x" is actually having, it shows me "132.22999999999999"

I thought this is understood because as double stores the floating point numbers in base 2 and as 0.23 CANNOT be absolutely represented in base 2, there is some rounding issue when converting back to decimal and so I see an error of  .00000000000001 due to precision limits.

However, when I change it to

double x = 132.16

X shows to be having exactly "132.16"!!

I dont understand how this is possible because I thought .16 cannot be absolutely represented in base 2!

Why do I see this difference? Any help would be greatly appreciated
0
aravindgopaluni
Asked:
aravindgopaluni
  • 6
  • 4
  • 3
  • +1
2 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
Floating point (double) stores the whole number in a binary format, not just the part after the decimal.  The number also includes an exponent to set the value to the correct range.  On this page http://www.h-schmidt.net/FloatConverter/IEEE754.html is a floating point calculator that shows how it all works.
0
 
sarabandeCommented:
it is indeed the case that the "same" floating point number may have two or more representations internally. if you make an assignment you are safe. it always will choose the same representation (not the one with the 99999 at end). however any division or multiplication gives an result where mantissa and exponent were calculated separatly and therefore the conversion to decimals can be different. note, also in math, the numbers 1.00000.... is equal to 0.99999....

the entire double-precision number uses 1 bit for sign, 11 bits for exponent and 52 bits for fraction (mantissa). a number is described in IEEE 754 by:

(-1)^sign * 2^(exponent - exponent bias) * 1.mantissa

In the case of subnormals(E=0) the double-precision number is described by:

(-1)^sign *  2^(1 - exponent bias) * 0.mantissa

if you want to compare doubles or have to output them you always need to consider a little rounding difference.

a good description you find at wikipedia or http://www.cquestions.com/2009/06/memory-representation-of-double-in-c.html

Sara
0
 
sarabandeCommented:
sorry Dave, didn't want to repeat ...

Sara
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Dave BaldwinFixer of ProblemsCommented:
No problem.
0
 
aravindgopaluniAuthor Commented:
Thank you for your answers.

I understand that double saves the whole number in base 2 (binary). However, what I couldn't understand is why in my VC++ application 132.23 is represented as 132.22999999999 while 132.16 is represented as 132.16 (absolute value) when both of these numbers result in infinite recurring when attempting to convert into base 2.

I used the calculator you provided and this is what it shows:

For 132.23

Decimal Representation      132.23
Binary Representation               01000011000001000011101011100001
Hexadecimal Representation      0x43043ae1
After casting to double precision      132.22999572753906

For 132.16

Decimal Representation      132.16
Binary Representation              01000011000001000010100011110110
Hexadecimal Representation      0x430428f6
After casting to double precision 132.16000366210938

So, when I do the below:

double x = 132.16

I thought x would have some indication of the overflowing base 2 conversion and so was surprised that it did not.

Issue here is I am saving this value to Oracle database and when somebody else is trying to pul the data, they complain about the discrepancy.

Is there a way I can make double to save unto only 2 decimal digits?

I tried doing floor(x*100.0 + 0.5)/100.0 but nothing seems to work.
0
 
Dave BaldwinFixer of ProblemsCommented:
For Oracle and most SQL databases, you need to use the DECIMAL datatype.  http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj15260.html  It doesn't have that problem.  But All IEEE754 floating point versions will have that problem.
0
 
ChloesDadCommented:
I'm not sure about saving the numbers as two decimal places, this will depend on your database structure, but you can certainly display the number to 2dp using tostring.
0
 
sarabandeCommented:
what I couldn't understand is why in my VC++ application 132.23 is represented as 132.22999999999 while 132.16 is represented as 132.16 (absolute value)

132.22999999999 is not a representation but output (presentation). when converting from base 2 to base 10 small rounding differences at the end of the precision (means after 15 - 17 decimal digits) are normal and cannot prevented principally. only the output can be made rightly by active rounding.

std::cout << std::fixed << std::setprecision(2) << mydouble; // two digits after decimal point

Open in new window


Sara
0
 
Dave BaldwinFixer of ProblemsCommented:
@aravindgopaluni, while you can do the display formatting in your own program, if the other people are using different software to access the database, they will see this problem as long as you are storing the values as floating point values.  The only way they will see the values with 2 correct decimal values is if you use the DECIMAL datatype in the database.  Even then, you will have to correctly format your values from floating point to correct decimal representation in your program before you store them in the database.
0
 
aravindgopaluniAuthor Commented:
Thanks Dave & Sara. Appreciate your time.

Sorry about the naive terminology I used when I said "representation" but essentially, I meant the output of the double variable.

So, even though both 132.23 and 132.16 cannot be finitely stored in base 2, do you think the reason I see the output as "132.16" in the latter case and not in the former is because of the rounding errors that cannot be avoided when converting back to base 10?

Thanks,
0
 
Dave BaldwinFixer of ProblemsCommented:
That's pretty much it.
0
 
aravindgopaluniAuthor Commented:
Both answers are helpful but the app forces me to select only one of them for "best". Sorry about that!
0
 
Dave BaldwinFixer of ProblemsCommented:
No problem, glad to help.
0
 
sarabandeCommented:
thank you. it is all fine.

Sara
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now