473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Help me understand a particular floating point error.

kiseitai2
Hello awesome people of bytes.com. After a year working on a thesis project, I stumbled upon an interesting phenomenon. When I set a float variable to 10060.000000, it never changed after going through some floating point arithmetic that involved a small number (10060f - 0.00027333f = 10060f?). Changing this variable to about 1000 or less gave the right result. Finally, changing this float variable to double fixed this problem (10060d - 0.00027333f = <10060d, which is correct). Is there a theoretical basis as to why the float-only arithmetic operation failed compared to the double - float one? I thought that 32 bits floats had enough decimals to handle most of the result of 10060 - 0.00027333. At the time of this issue, I was testing my code on Win Vista 32bits. I have not tested the Mac and Linux versions to see if they have the same issue. Thanks for your time!
May 15 '14 #1

✓ answered by weaknessforcats

Floats contain only 6 or 7 significant figures. More than that gets rounded to 6 or 7 significant figures.

A double will have 15-16 significant figures.

All calculations involving float will convert the float to a double, do the calculation as a double and then convert the result back to a float with possible loss of data.

All calculations should be using double only. A float is an obsolete data type left over from the olden days of computers with tiny memories.

4 1230
weaknessforcats
9,208 Expert Mod 8TB
Floats contain only 6 or 7 significant figures. More than that gets rounded to 6 or 7 significant figures.

A double will have 15-16 significant figures.

All calculations involving float will convert the float to a double, do the calculation as a double and then convert the result back to a float with possible loss of data.

All calculations should be using double only. A float is an obsolete data type left over from the olden days of computers with tiny memories.
May 15 '14 #2
Banfa
9,065 Expert Mod 8TB
If you repeated have to add (or subtract) a very small floating point value from a large floating point value then you quickly get inaccuracies due to rounding errors. There is a method called, the Kahan summation algorithm, that improves the accuracy of this sort of operation by correcting the sum as it goes along for low order bits dropped when you add a small floating point number to a large one..
May 15 '14 #3
Thanks for your responses. For some reason I confused the exponent range with the number of significant digits, so I thought floats had more significant spaces than they actually do. The reason why I was using floats is because I like to keep memory usage as low as I can for as long as I can. Thus, I tend to use the smallest possible type whenever possible. Thanks again for correcting me!
May 15 '14 #4
donbock
2,426 Expert 2GB
You might find this article helpful: What Every Computer Scientist Should Know About Floating-Point Arithmetic.
May 20 '14 #5

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

Similar topics

3
by: Dylan Nicholson | last post by:
The following C code: float f = 5.15002; double d = 5.15002; if (d + FLT_EPSILON < f) puts("huh?"); Where 'FLT_EPSILON' is defined as the minimum number whereby 1.0 + FLT_EPSILON != 1.0,...
13
by: Dylan Nicholson | last post by:
I just posted regarding a possible floating point error (not sure where), and have since discovered that: float f = 5.15002; double d = 5.15002; if (float(d) < f) puts("huh 1?"); float f2 =...
3
by: C. Sengstock | last post by:
Hi, i´ve got an iteration with random generated floating point numbers. During the iteration process sometimes the programm stops with the error: "floating point error: overflow" Is the...
2
by: devendra pardeshi | last post by:
hi friends/seniors i am stuck on one problem in VB 6.0 and need solution. see if u can help me. first i describe the problem. Can u imagin the WinZip scenario. we right click on some file...
0
by: mehulmite | last post by:
Hi! My program is compiling and running properly, but on the output screen it gives FLOATING POINT ERROR:INVALID DOMAIN ABNORMAL PROGRAM TERMINATION The same program runs fine on someone...
5
by: loftusoft | last post by:
While debugging the following code: #include <iostream> using namespace std; int main() { float f = 583312.0F * 0.1F; cout << f << endl; return 0; }
6
by: Torben Frandsen | last post by:
Hi I'm porting an old Access/VB application to .Net, and I've encountered a small problem. The VB application takes a value from a query and stores it in a Double. This value is the result of...
3
by: talibbhai | last post by:
hello friends; I have an error in my program what is the reason behind it. /*this is mathematical function of EXPONENT*/ #include<conio.h> #include<stdio.h> #include<math.h>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.