By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,275 Members | 1,921 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Float value is wrong and float error

P: 69
Float has not always been interpreted correctly.

I suggest using float. I like float. It is a powerful tool. But, if you use it, then be careful to test the returned results that you get from using it.

I am learning C++11. After much research and study I am using CODE::BLOCKS 17.12 (it is free). I am not using the wxWidgets, which seems to get in the way of my learning C++. C++11 seems to be the latest (in year 2020 AD) version of C++ that is used in the majority of big industry programs. Specifically programs that are used in big industry, heavy manufacturing, very large retail analysis and control. I am learning C++11. This article has to do with what I have learned as it relates to float. I worked at getting to the facts of how float did work and how it was miss-used by various computer systems.

I used float and examined the logic process with injected self created reports. At first it seemed to maybe be fine, but when the results were tested and reported, it was returning incorrect values. I tried float with many other testing, and thereupon wrote a code reporter that verified step by step the logic and reported it. My opinion now is that float is powerful, but be careful not to trust it's use without testing its use and checking the math.

The problem seems to not be the float itself. It seems to be that various compilers and maybe various operating systems, and maybe various video cards, and maybe even various CPU's process float values differently and maybe even wrongly. I studied this and concluded to test and verify as I programmed.

In the past I programmed in VB6(sp5) and recently moved to C++ (partially) so that I could use larger numbers in calculations. VB6(sp5) is still better than any version of Visual Studio beyond 6.0(sp5). It is better than any ".net" and Visual Studio [anything] past VS6(sp5). But, I am pressed to move on to larger number capacity, and a full programmer level UNICODE interface. Now I am learning that even C++ has its limits via other processes that it has to work with that are not up to the intricate C++ standards. Test and verify. That is what I suggest in your use of float.

If you use float, you might find your end results more accurate if you do your calculations in 4 extra places past the decimal point. Example: you might need pi to this accuracy 3.14, therefore use it to this accuracy 3.141592 (including the four extra digits 1592). If you are converting your other numbers to float or from float and then doing math, test it and verify the results. You might find use in adding a 00001 to your numbers like this: 1234.1234 becomes 1234.123400001 . It might not look correct to you at first, but you might find better results. This of course depends upon your system. Different systems seem to use or abuse float differently.

If you have a few hours or days to read into what float actually is and how it is compiled, then I suggest taking the time to read that and then you might see a lot of logic in my previous paragraph.

Test and verify.
1 Week Ago #1
Share this Article
Share on Google+

Expert 100+
P: 183
I stumbled upon a few codes in the past that made me research this topic. Codes such as:
Expand|Select|Wrap|Line Numbers
  1. float x=0.7;
  2. printf("%.10f %.10f\n",0.7, x);
Output: 0.7000000000 0.6999999881

Expand|Select|Wrap|Line Numbers
  1. float x=1.7;
  2. if(x==1.7)
  3.  cout<<"Equal";
  4. else
  5.  cout<<"Unequal";
Output: Unequal

, etc...

Interesting enough for me to dig into the rabbit hole to understand the behavior of floating points.

Here's a read if anyone's interested:
"What Every Computer Scientist Should Know About Floating-Point Arithmetic"
1 Week Ago #2

P: 69
Thank you dev7060.

Did you try those work-arounds that I suggested? I am really new at C++11, but they seem maybe logical.

I had been surprised to find that some work around might be required to force the use of float to supply validly usable math returns. I am glad that I checked line by line in my math and verified the results.
1 Week Ago #3