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!
Related
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)
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;
}
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I tried the below code i assigned a float value to variable and i compared it with a floating point value but it did not gave the desired output.
Here as a==13.30 is true i thought it will print "a" instead it prints "5"
#include<iostream>
using namespace std;
int main()
{
float a=13.30;
if(a==13.30)
cout<<a;
else
cout<<"5";
}
output is "5" not "a"
13.30 is a double. Try comparing against 13.30f.
0.30 cannot be represented exactly and since double has a higher precision, it is not an exact match.
Ofcourse you can simply compare float value with some numerical value , but just because you can , it doesn't mean you should. It's classical example of that .
There are many problems regarding storing exact floating point value in memory , due to hardware restrictions , this issue regarding storing exact floating point value in memory exist in virtually all the programming languages and platform . A better way to equating floating point values is checking if the difference of two values that you need to compare is less than some other very small number . In your code , you can implement that as ,
#include <iostream>
using namespace std;
const double EPSILON = 1e-5;
int main()
{
float a = 13.30;
if (abs(a - 13.30) < EPSILON)
cout << a;
else
cout << "5";
}
Now , this code will output 13.30 , here EPSILON is used as a very small double value to compare with the difference .
To know more about why this issue is prevalent read , Is-floating-point-math-broken and Why are floating point numbers inaccurate
This question already has answers here:
can't print more decimal of pi [duplicate]
(2 answers)
Closed 7 years ago.
I wrote a c++ program to calculate pi using the infinite expansion for pi/4. I've used long double but still i get pi = 3.14158 . Where is the extra decimal chopped off. How to correct it?
So is this is my code :
#include<iostream>
#include<math.h>
using namespace std;
int main(void)
{
long double pi=1,si,i;
for(i=1;i<100000;i++)
{
si = (pow(-1.0 ,i))/(2*i+1);
pi += si;
}
pi*=4.0;
cout<<" "<< pi <<" \n ";
}
Your problem here is not the actual accuracy of the computed number, but in the formatting of your displayed output. Check this answer for details.
This question already has answers here:
Why does this division result in zero?
(6 answers)
Why do my division results appear to be off?
(2 answers)
Closed 9 years ago.
My code is not showing exact float value means number after point is not displayed by the code
I am using Turbo C for sum up the series 1 + 1/3 + 1/5 + 1/7 + 1/9+….. Up to N terms
#include<iostream.h>
#include<conio.h>
void main()
{
int k=0;
int m=0;
int s=1;
clrscr();
cout<<"Enter the limit of the series: ";
cin>>m;
for(int j=1;j<=m;j=j+2)
{
m=1/j;
s+=m;
}
cout<<"Sum of the given series is: "<<s;
getch();
}
You're using int which only displays Integer (ie whole number) values. It truncates any decimal places because it assumes you don't want them. Try using float or double instead.
Integer division will not give you anything other than integer results.
You need to:
Change s to a float or double.
change m to a float or double.
Change 1 in 1/j to 1.0f or 1.0 (for float and double respectively).
Now, you probably also want to use a different variable than m for your input and for-loop limit variable, so that you don't stop too quickly once the calculation starts.
Use the type double for variables m and s.
Here are the variables you declared in your code.
int k=0;
int m=0;
int s=1;
Where exactly do you think you have a floating point number?
(which would have to be of type float or double)
You do know that int means integer, right?
(eg. Not a floating point number)