+ 1

Why do I have the wrong result for a simple double calculation?

I calculated 2000/4000 and get 0. https://code.sololearn.com/cChdU45Yrso1/?ref=app

2nd May 2022, 6:34 AM
1337element
1337element - avatar
5 Antworten
+ 2
You divide 2 ints, store them as double
2nd May 2022, 6:39 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 1
The program is doing integer division and then storing the result into a double precision float. It is doing integer division because both operands are integer. If you make either one into floating point then the result will be kept in floating point.
2nd May 2022, 6:44 AM
Brian
Brian - avatar
0
1337element did you mean 2000 / 4000 = 0 or 4000 / 2000 = 2
2nd May 2022, 6:40 AM
BroFar
BroFar - avatar
0
Oh yeah thanks I thought it is fine when result is double. Thanks.
2nd May 2022, 6:42 AM
1337element
1337element - avatar
0
1337element result is fine as a double as 2.0 if 4000/2000 the int/int later simply left it as int 2
2nd May 2022, 6:44 AM
BroFar
BroFar - avatar