Answer the question
In order to leave comments, you need to log in
How to get a fractional number when dividing 1 by 1?
i have a code
#include <iostream>
using namespace std;
int main() {
int i = 1;
int j = 1;
cout << 1.0 * i / j << endl;
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
you can do explicit data type conversion in
program code:
#include <iostream>
using namespace std;
int main() {
int i = 1;
int j = 1;
cout << fixed;
cout << (float)(j/j) << endl;
return 0;
}
[email protected]:~/temp> gcc test.cpp -lstdc++ -o test.o
[email protected]:~/temp> ./test.o
1.000000
See what you need.
#include <iostream>
#include <iomanip>
#include <cstdlib>
int main()
{
int a = 9;
int b = 4;
std::div_t result = div(a, b);
std::cout << result.quot << " " << result.rem << std::endl;
// Наверное это
std::cout.precision(1);
std::cout << std::fixed << static_cast<double>(a) / b << std::endl;
}
The type of your expression is double.
This can be checked in a very simple way (I did this):
create a template class declaration without an implementation and instantiate it with an expression. The compilation error will show the type:
template <typename T>
class TD;
int main() {
int i = 1;
int j = 1;
TD<decltype (1.0 * i / j)> td;
}
/home/maxim/Workspace/my/cdm/main.cpp: In function ‘int main()’:
/home/maxim/Workspace/my/cdm/main.cpp:61:32: error: aggregate ‘TD<double> a’ has incomplete type and cannot be defined
TD<decltype (1.0 * i / j)> a;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question