473,671 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Float value is wrong and float error

rollerbladegirl
69 New Member
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 5428
dev7060
644 Recognized Expert Contributor
I stumbled upon a few codes in the past that made me research this topic. Codes such as:
(C)
Expand|Select|Wrap|Line Numbers
  1. float x=0.7;
  2. printf("%.10f %.10f\n",0.7, x);
  3.  
Output: 0.7000000000 0.6999999881


(C++)
Expand|Select|Wrap|Line Numbers
  1. float x=1.7;
  2. if(x==1.7)
  3.  cout<<"Equal";
  4. else
  5.  cout<<"Unequal";
  6.  
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
rollerbladegirl
69 New Member
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
SmithAnchorWork
18 New Member
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
Banfa
9,065 Recognized Expert Moderator Expert
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

2
3611
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
4
2603
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 something dividing by 10^numeric_limits::max_exponent10/2, then dividing by 10^numeric_limits::max_exponent10/4, ....
0
1392
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;
5
2391
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 it? the following code doesn't show up any error during compile time but crashes at runtime telling "unrecognized or unsupported array type": array<float>^ vals = gcnew array<float>(dimension); pin_ptr<float>ss=&vals; unmanaged_function(ss);
2
4204
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: float argument required
6
5460
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
2
2104
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 spaces 66 Else 67 strBD = objdr.Item("BD") 'All ok so leave as is 68 End If Here is the error I get:
14
5536
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?
3
8008
by: Car | last post by:
what dose this float value mean$B!)(B The value is: -1.#IND0
0
1553
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 it is done . some one help me for doing this.That's the code:-- Reply soon ...... CODE ........................................................................................ Public Function GetByt(byt As Integer) As single Dim CropL As...
0
8473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8911
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.