This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 3 years ago.
I have this code and the function pow() is behaving differently in two cases.
For "p" I'm using the number 3, in the first output is showing 124 but the second shows 125.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
long long int p, ans;
cin >> p; //3
ans = pow(5,p);
cout << ans << endl; //124
cout << pow(5,p) << endl; //125
return 0;
}
Related
This question already has answers here:
Fixed Decimal Precision [duplicate]
(4 answers)
Closed 2 years ago.
I am trying a problem. I need to print the ans in 6 decimal digits. For example if the ans is 64, I want to print 64.000000
I tried the following way.
what I did wrong?
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
float n;
while(t--)
{
cin>>n;
float s=(n-2)*180.000000;
float w=(s/n)*1.000000;
cout<<w*1.000000<<setprecision(6)<<endl;
}
return 0;
}
You can make use of std::fixed:
#include <iostream>
#include <iomanip>
void MyPrint(int i)
{
double d = static_cast<double>(i);
std::cout << std::setprecision(6) << std::fixed << d << std::endl;
}
int main() {
MyPrint(64);
MyPrint(100);
return 0;
}
Running the above code online results in the following output:
64.000000
100.000000
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I return a double from two ints being divided
My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables (a & b) as integers?
user#box:~/c/precision$ cat precision.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 10, b = 3;
float ans = (a/b);
cout<<fixed<<setprecision(3);
cout << (a/b) << endl;
cout << ans << endl;
return 0;
}
user#box:~/c/precision$ g++ -o precision precision.cpp
user#box:~/c/precision$ ./precision
3
3.000
Cast the operands to floats:
float ans = (float)a / (float)b;
This question already has answers here:
cos returns wrong values?
(3 answers)
Closed 6 years ago.
#include <iostream>
#include <cmath>
Using namespace std;
void main ()
{
double z,a;
cout <<"input a"<<endl;
cin>>a;
z=sin (a);
cout <<"z="<<z <<endl;
system("pause");
}
When I input the variable a with value of 90 it gives me 0.893997
And when I make the variables int or float it gives the same value
The input of 'sin' is in radians (http://en.cppreference.com/w/cpp/numeric/math/sin)
So the answer is, in fact , correct
This question already has answers here:
unexpected output in cout and printf [duplicate]
(4 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I have the following code:
#include<iostream>
using namespace std;
int main()
{
int x=3;
cout<<x++<<++x<<x++<<++x<<endl;
return 0;
}
the output should be 3557
but it is 6747. why???
Also:
#include<iostream>
using namespace std;
int main()
{
int x=3;
cout <<x++<<endl;
cout<<++x<< endl;
cout<<x++<< endl;
cout<<++x<< endl;
return 0;
}
The above code gives:
3
5
5
7
(every digit in new line)
Can anyone explain why?
int x=3;
cout<<x++<<++x<<x++<<++x<<endl;
This is undefined behavior, so you could easily get any result.
int x=3;
cout <<x++<<endl; // x = 3, post incremented to 4...
cout<<++x<< endl; // here x = 4 and preincremented to x = 5
cout<<x++<< endl; // x = 5 , postincremented to 6...
cout<<++x<< endl; // so here x = 6 but preincremented to 7
Undefined behavior and sequence points
unexpected output in cout and printf
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I return a double from two ints being divided
My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables (a & b) as integers?
user#box:~/c/precision$ cat precision.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 10, b = 3;
float ans = (a/b);
cout<<fixed<<setprecision(3);
cout << (a/b) << endl;
cout << ans << endl;
return 0;
}
user#box:~/c/precision$ g++ -o precision precision.cpp
user#box:~/c/precision$ ./precision
3
3.000
Cast the operands to floats:
float ans = (float)a / (float)b;