This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Floating point comparison `a != 0.7` [duplicate]
(3 answers)
Closed 1 year ago.
This is a simple code which should have printed "No" , but its printing "Yes". It works fine if I use double instead of float Or compare digits upto 1 decimal place. I am using online C++14(gcc+14) compiler here. Why is this happening?
#include <iostream>
using namespace std;
int main() {
float s= 10.11;
cout<<(( s < 10.11) ? "Yes": "No")<<endl;
return 0;
}
Related
This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Is floating point math broken?
(31 answers)
Closed 4 months ago.
I am initializing a variable a of type float with value 1.2.
If I print it on screen, it give me 1.2, but when I check it with if/else, it does not run the if statement whose condition is if(a == 1.2). Instead it runs the else part.
Can someone please help, I am still learning the basics.
Here is my code:
#include<iostream>
#include<iomanip>
using namespace std;
int main ()
{
float a = 1.2;
cout<<fixed<<setprecision(1)<<a<<endl;
if (a == 1.2)
cout<<"\n a is 1.2";
else
cout<<"\n a is not 1.2";
return 0;
}
The it-statement is comparing a float to a double. 1.2 is a double and not a float.
Maybe this will give you some clarity: Why comparing double and float leads to unexpected result?
Hope this helps!
This question already has answers here:
Is floating point math broken?
(31 answers)
GCC C++ pow accuracy
(1 answer)
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 1 year ago.
I can't understand why the statements are giving different results as According to Me
a==b is same as b==a
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
cout<<(pow(10,2)==(pow(8,2)+pow(6,2)))<<endl;
cout<<((pow(8,2)+pow(6,2))==pow(10,2))<<endl;
return 0;
}
OUTPUT IS-
1
0
This is double comparison issue. You can use something like:
cout<<(fabs(pow(10,2) - (pow(8,2)+pow(6,2))) < std::numeric_limits<double>::epsilon() ) <<endl;
cout<<(fabs((pow(8,2)+pow(6,2))-pow(10,2)) < std::numeric_limits<double>::epsilon() ) <<endl;
Ref:
What is the most effective way for float and double comparison?
This question already has answers here:
Is floating point math broken?
(31 answers)
Are all integer values perfectly represented as doubles? [duplicate]
(5 answers)
Closed 2 years ago.
I have the following snippet
cout.precision(30);
long double d = 85952643841691072928.0;
cout << d << endl;
The print statement outputs 85952643841691074560. Why are the last 4 digits before the decimal incorrect? A long double should be capable of handling a number of this size?
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I use codeblock for compilation.
code:
int main(){
int l=pow(10,2);
cout<<l;
return 0;
}
OUTPUT-99
int main(){
int l=pow(10,3);
cout<<l;
return 0;
}
OUTPUT-1000
In short - because of integer truncation.
The function pow operates on two floating-point values and returns a floating-point value. When working on integers, the result of pow(10,2) might be stored as 99.9999999, or 100.0000000001. Due to integer truncation, 99.9999999 gets truncated down to 99, and 100.0000000001 gets truncated down to 100.
Same goes for pow(10,3), with 999.9999999, or 1000.0000000001 - which will be truncated to 999 or 1000.
You can read more about the pow function here.
This question already has answers here:
Floating point inaccuracy examples
(7 answers)
exact representation of floating points in c
(9 answers)
Closed 8 years ago.
As the title states, I'd like to start from 1 and decrement with by 0.01 all the way down to zero.
Problem is, I'm using floats and I keep getting values such as 0.5000000001.
Simply use an int, Start at 100 decrement down to 0 and divide the value by 100.0
for (int i=100; i>=0; --i)
{
float f = i/100.0f;
...
}