472,984 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,984 developers and data experts.

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 5327
626 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

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

Similar topics

by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
by: Marc Schellens | last post by:
Is there a standard way of determining the number of digits a float needs (base 10) to be printed (the integer part)? log10 is too expensive. As is a loop dividing by 10. I could think of...
by: Steve Wasser | last post by:
Hi. What am I doing wrong: foreach (DataRow dr1 in MyTable1.Rows) { reg = (float)dr1; prem = (float)dr1; die = (float)dr1; eth = (float)dr1; term = reg+prem+die+eth;
by: xxx | last post by:
Hi all, i'm new in visual c++ and i'm having troubles converting types. Let me explain: i have an unmanaged c++ function that wants an float* parameter but i have an array<float>^, how i can covert...
by: progman | last post by:
Data Struct: from (string), to (string), rate (float) when i run this: cursor.executemany('insert into promo (`From`,`To`, `RATE`) \ values (%s,%s,%f)', ) i got this error: TypeError:...
by: cok119 | last post by:
Hi, all How can I add '+' or '-' before float value using float.ToString(fotmat-string) ? What's the format string should used ? thanks
by: Problematic coder | last post by:
Here is the code: 64 If objdr.Item("BD") = "" Or objdr.Item("BD") Is DBNull.Value Then 'test to see if result is blank or null 65 strBD = "00000000" 'No birthdate found so set to 8...
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
by: Car | last post by:
what dose this float value mean? The value is: -1.#IND0
by: chand arora | last post by:
i want to concatenate 4 byte value into 1 float value. i will receive data thorugh the microcontroller in the form of byte.Now i want to concatenate this recived 4 byte value into 1 float value.how...
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.