Reputation: 93
Using Devc++ for printf getting 0.00000 and for cout getting some weird output.
#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
main()
{
float i,j=M_PI;
i=sin(j);
printf("%f \n",i);
cout<<i;
}
Upvotes: 0
Views: 388
Reputation: 7603
As mentioned you get different output values because the cout
one uses scientific notation and the printf
defaults to 6 decimal places. If you increase it by using
printf("%.013f \n",i);
You'll get the same value.
Now not all decimal numbers can be represented with floating point representation which is why you don't get the result you are expecting.
Also here's a question on how sin might be implemented on different platform, you can then see why sine of PI might not be 0.
Upvotes: 2
Reputation: 96251
It looks to me like M_PI
isn't exactly pi (we know it can't be), and the result from sin
isn't exactly zero.
printf
with %f
represents output numbers rounded as a "normal" decimal while cout
uses an adaptive format (roughly %g
in printf as I recall) that uses scientific notation to represent the small number.
Also note that %f
means double
but that the varargs helpfully promotes your float
to a double when being passed in.
Upvotes: 2