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;
Related
This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 1 year ago.
I want to convert 3 to 3.00. I have done explicit type conversion. but guess it doesn't seem to work!whats wrong?
my code:
#include<iostream>
using namespace std;
int main(){
int PI = 3;
double a = static_cast<double>(PI);
cout<<a;
}
output: 3
This may help.
#include<iostream>
using namespace std;
int main(){
int PI = 3;
double a = static_cast<double>(PI);
cout.precision(2);
cout << std::fixed << a;
}
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:
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;
}
This question already has answers here:
Factorial does not work for all values
(2 answers)
Closed 4 years ago.
I'm new at c++, I'm trying to create a factorial calculator, but I'm having trouble calculating n! when n > 15.
Here's the Code:
#include <iostream>
using namespace std;
int calculate_factorial(int num)
{
int fact = num;
for (int i = fact - 1; i > 0; i--)
{
cout << i << " " << fact << endl;
fact *= i;
}
return fact;
}
int main()
{
int num = 16;
cout << calculate_factorial(num) << endl;
system("pause");
}
Apart from the fact that calling it a sum is just... no. Anyway, int can only go up to 2^31 - 1, and that's only good for 12!. Even changing it to unsigned int won't change that, and even using unsigned long long int would only allow you to go up to 20!.
Your result is overflowing. You might wanna look into the GNU Multiple Precision library.
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;