470,643 Members | 1,566 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Float value is wrong and float error

69 64KB
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.
Feb 13 '20 #1
4 4861
570 Expert 512MB
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" https://docs.oracle.com/cd/E19957-01..._goldberg.html
Feb 14 '20 #2
69 64KB
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.
Feb 14 '20 #3
For rollerbladegirl: I have a solution for float inconsistencies.

Convert the entire float number into some single digit format that you like and can use, then write code that will do the math in some process similar to long-hand math that you would do on a sheet of paper with a pencil one digit at a time, then use the result instead of using float math via a processor's algorithm.

That way you skip the potential of a processor converting numbers to their logarithmic values (estimates) and then processing them and then converting the returned values back from logarithmic values to their (estimated) float values.

When you write the math part of your own calculations, create tables for single digit math so that the processor does not multiply and divide (etc.) via logarithmic conversions. It used to be ok and accurate "enough," when small numbers were being used, but if you are using floats then stay away from allowing the processors to do these type of conversions. Make your own single digit tables and use them. So you have to write more code and the processors are being limited to your pre-set decisions, you get more accuracy and if I remember correctly, this can be taken out to over 10,000,000 digits.

Have a nice day.
Mar 23 '20 #4
9,065 Expert Mod 8TB
When using floats you should remember they are approximations to numbers, not every single number can be represented exactly and the more calculations you do the more opportunity there is for errors to creep in and get multiplied.

You should never use the == and != operators on floats, you should assume the first always returns false and the second true. Even though you can probably produce a code example where that is not the case it is not reliable enough to put into production code.

Always use the double type not the float type, the extra precision means the errors, hopefully, occur in decimal places you do not care about.

Therefore if you need absolute accuracy then just do not use floats/doubles at all, for example no banking application makes use of these types, they can not afford the errors introduced.
Jun 11 '20 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Marc Schellens | last post: by
reply views Thread by Steve Wasser | last post: by
5 posts views Thread by xxx | last post: by
2 posts views Thread by progman | last post: by
2 posts views Thread by Problematic coder | last post: by
14 posts views Thread by Jim Langston | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.