Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ casting int to double

Tags:

c++

casting

I'm not a C++ developer, but today I've found a C++ code and try to understand it. So I've stacked on this piece of code:

int m = 2, n = 3, i = 1;
double mid = (double)m / n * i;
int d = (int)mid + 1;

printf("%d %d\n", mid, d);

The result which is going to be printed to the console is: 1431655765 1071994197. It seems to be related with the casting of variable m to double, but I have no idea how it is happening. I need someone to help me understand it. Thanks in advance!

like image 672
nenito Avatar asked Sep 12 '25 05:09

nenito


1 Answers

You should print a double(mid) with the %lf format specifier in printf.

like image 147
Ivaylo Strandjev Avatar answered Sep 16 '25 05:09

Ivaylo Strandjev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!