27th Oct 2025, 5:32 AM
Axxcii
Axxcii - avatar
3 Réponses
+ 5
Hi! 5/2 is 2.5, not 3. You're most likely getting the result 2 because you declared your variables a and b as int at the beginning. An int is an integer, and the fractional part is discarded. If you want to get 3, declare the variables as float (double) and round the result up at the end of the calculation. If you want an exact result, declare the variables as floating-point numbers.
27th Oct 2025, 5:57 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 4
https://sololearn.com/compiler-playground/clcr05N0JtlC/?ref=app note that C's round does not follow the standard rounding rule of 'round half to even' for 0.5 (which should round up ony if the preceeeding number is odd). 2.5 rounds to 3.0 instead of 2.0 in python, round is to the nearest even value: print(round(5/2)) # 2.5 rounded to 2 print(round(7/2)) # 3.5 rounded to 4 round in C++ and Go https://sololearn.com/compiler-playground/cWt2POjvZMee/?ref=app https://sololearn.com/compiler-playground/cXnGEuCbShU2/?ref=app
27th Oct 2025, 6:00 AM
Bob_Li
Bob_Li - avatar
+ 3
integer division in C truncates, it does not round. so 2.1, 2.5, or 2.9 all becomes 2 the numbers after the decimal point are just dropped, they're not rounded.
27th Oct 2025, 5:49 AM
Bob_Li
Bob_Li - avatar