Why are these 2 cout statements giving opposite result? [duplicate] - c++

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?

Related

Float data type [duplicate]

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!

Comparing float to a decimal point number [duplicate]

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;
}

Initializing a long double; printing a different value [duplicate]

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?

I have written a code for converting decimal number to binary number which is not giving desired output [duplicate]

This question already has answers here:
Undesired output with modulo operator and ' pow '
(2 answers)
Strange behaviour of the pow function
(5 answers)
Closed 5 years ago.
//program to give binary equivalent of a decimal number without using recurssion
#include<stdio.h>
#include<math.h>
int main(void)
{
int i=0,num,bin=0;
printf("enter number : ");
scanf("%d",&num);
while(num>0)
{
bin+=((float)(num%2))*pow(10.0,(float)i++);
num/=2;
}
printf("Binary equivalent is : %d",bin);
getchar();
return 0;
}
This code dosen't provide desired output for inputs like 7, but does provide desired output for some numbers.Can you please help me out find the error.

Result is only 5 decimal places even with long double (in c++) [duplicate]

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.