0

I am coding in Qt C++.

I am declaring a literal double value in my main rountine of 0.1. I then pass this variable to a function that takes type double. When I hover the mouse over the variable in debug mode I see 0.100000000000001

What is the cause of this change and how do I stop it please?

Method Definition:

void MyClass::MyMethod(double fNewCellSize)
{
// variable fNewCellSize appears as 0.10000000000001
}

Method Call with literal value of 0.1:

MyObj.MyMethod(0.1);

My environment is Windows 64 bit OS using Qt 5.2.1 and compiling using Microsoft 2010 Visual Studio compiler in 32 bit.

narmaps
  • 311
  • 4
  • 14
  • Floating point values in `double` are not exact. Depending on how you display them you may see bogus digits at the end. – Mark Ransom Mar 31 '14 at 22:29

2 Answers2

3

Many decimal numbers are not exactly representable in IEEE floating point (i.e. the binary representation used for double) and 0.1 falls in this camp. When you type 0.1 the C++ compiler is required to convert that into a double to be represented on your hardware. It does so by computing a near approximation of this and so you see a bit of error.

If you try something like a power of two: 0.5, 0.25 these will be exactly represented.

See this link for a much more in-depth description of the idea.

Community
  • 1
  • 1
Ryan Livingston
  • 1,824
  • 10
  • 16
  • Thanks for the responses... Been programming for 20 years and didn't know this! So I am doing math on very large numbers (geographic map coordinates) using cell sizes that are very small. How do I prevent errors creeping into my calculations? – narmaps Mar 31 '14 at 22:53
  • Not to worry, floating point arithmetic surprises many people. My best advice is to do some reading about how it actually works. There is a classic paper [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://www.validlab.com/goldberg/paper.pdf) that I've found provides a good introduction. Examples of things to consider: floating point arithmetic isn't necessarily associative so (a+b)+c may not equal a+(b+c), mixing large and small magnitude values in particular ways may cause surprises. – Ryan Livingston Apr 01 '14 at 00:32
0

This is normal behaviour in any computer. It's caused by the fact that decimal numbers are being stored in fixed-sized binary memory. The extra digits come from the inherent errors caused by converting from binary to decimal.

You will not get rid of them. The best you can do is choose a precision (either float or double) that is big enough so that the extra error digits will not make any difference to your solution, and then chop them off when you display the number.

RobbieE
  • 3,954
  • 3
  • 17
  • 33