This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 8 years ago.
Now I understand floats are less accurate than double, but does this explain when I have the std::string:
"7.6317"
and I do:
float x = atof(myString.c_str());
getting 7.63170004 is expected? Is there any way I can tell the assignment of x to only read the first 4 decimal places? Or is this because of the way the float representation stores the number 7.6317?
Yes. It is expected. It is so-called floating point error.
Some floating point literals do not have an accurate representation in the computer, even if -- in decimal notation -- the number seems harmless. This is because the computer uses 2 as a base. So even if a number might have a finite representation in base 10, it might not have on in base 2.
you can do it like:
float x = floorf(val * 10000) / 10000;
i think it should work! see See
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Below you can find a basic C++ exercise with a for loop, the question is: what is the value of i after the loop runs. (Apparently, the value is 5.)
My question, on the 5th run (i=5), the value of a is 100, shouldn't the loop stop since the condition is a<100? Isn't the condition checked after a is multiplied with 10? (so after a is already 100)
If I check the value of a after the loop exit, it will be 1000. I am confused. Why does the loop not stop at 100?
#include <iostream>
using namespace std;
int main(void) {
float a;
int i = 0;
for(a = 0.01; a < 1e2; a *= 1e1)
++i;
return 0;
}
The exercise is designed to show that float (and double etc., floating point numbers in general) do not have the exact decimal value as what you see in the program text.
0.01 (decimal) cannot be exactly represented in the binary floating point format used, therefore multiplying 10 five times will not yield exactly 100.0.
See also: Floating point inaccuracy examples
You can't tell exactly what will happen with this code without knowing the details of the floating point implementation on your platform; the C++ standard leaves this intentionally flexible.
In essence 0.01 could be an approximation for the closest float to that double literal. So the stopping conditional a < 1e2 could be met prematurely or otherwise.
Note that along with 0.01, 1e1 is a literal of type double.
So the conversions to float are complicating things further. Then there is floating point strictness to consider.
Cut a long story short: don't use a floating point as the "index" in a loop.
This question already has answers here:
C++ floating point precision [duplicate]
(5 answers)
Closed 5 years ago.
I'm trying to round doubles to a specific precision, however the following functions give me different results:
Version 1:
static double RoundPrecision(double& val)
{
val = floor(val * 1000 + 0.5) * 0.001;
return val;
}
Version 2:
static double RoundPrecision(double& val)
{
val = floor(val * 1000 + 0.5) / 1000;
return val;
}
Example output when rounding the number 300.9:
Version 1: 300.90000000000003
Version 2: 300.89999999999998
Both versions sometimes give the same result, but for specific inputs the results differ. I have to have consistent behavior when equating numbers to other variables in the program.
EDIT:
I am aware of the problems with floating point precision, which is exactly what I'm trying to avoid here by rounding. I need a consistent way to round to 3 decimal point precision.
Computers are limited in their precision. You either accept the available precision in double or float, or you seek other libraries that give you sufficient precision, but with worse performance. Realistically, you cannot achieve exact real-numbers on computers, and you won't even need it. double gives you 10^-16 relative precision, then try long double. If that isn't enough, then seek external libraries that would do that for you. Here's an example of an arbitrary precision library.
From the comments I see that you need 3 decimal places precision. If you read this number up to 3 decimal points, then both results are the same. I think the reasons you're confused is that you don't know how to compare floats. Here's how you do it.
This question already has answers here:
Division not outputting correct answer c++
(2 answers)
Closed 7 years ago.
I'm currently working on a C++ program where I have a bankAccount class and I need to calculate interest. Problem is, my function is rounding off my numbers to the whole number, even though I'm using a float as my variable type. So if I had code like this:
float BankAccount::CalculateInterest(int Time)
{
float thing;
thing = (967 / 365);
return thing;
}
thing ends up equaling 2.00000000, when it should equal 2.649315068.
Any ideas? I had my interest equation in there before, and the result was always 1.00000000 no matter what, so I put this in as a test and I saw that it's rounding to the "floor".
Thanks for your help!
It's not rounding.
You are dividing two integers and the result will be an integer.
You can remedy this by simply adding ".0" to both numbers, or explicitly defining them as floats.
967 and 365 are integers, so integer division is used and the remainder discarded. Make one a floating point number and floating point division will be done: 967 / 365.0.
Alternatively:
float thing = 967; // integer converted to float during assignment
thing /= 365; // division of float and converted integer
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I'v ran into another anomaly:
float T = 19.0 / 99.0;
float moo = (99.0 * T) - 19.0;
as expected, T = 0.191919, however 'moo' is meant to be 0 but instead is 7.450586.
I can only presume some form of casting is once again required somewhere.
Change your code to this and you will get 0 on moo
float T = 19.0f / 99.0f;
float moo = (99.0f * T) - 19.0f;
Also the value on moo without this is not 7.45, it is 7.45058e-08 which is almost zero.
Since any floating point numbers are represented in 2's complement notation in c++.you will usually get approximate values , while manipulating floating point values .So 7.450586e-008 is exact behaviour . If you need more precision use double.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
Why when I use multiplication
float a= 1.0500000f;
float b= a*100.0f;
why b is 104.99999 but not 105.0 ?
and when I
int f= (int)b;
f is 104
Floating point numbers are not infinitely accurate -- the wikipedia page on Float numbers is quite interesting to read.
On your second question: (int)b truncates anything that occurs after the comma. This means that, in your case, 104.99999 becomes 104. So when you create a rounding error, and then cast it to an integer, you are indeed running the risk of getting a lower number.