0

Where is the bug?

#include <iostream> #include<cmath> using namespace std; int main() { int color; float fixed = 40.0; cin >> color; float cost = (5.0 * color) + fixed; cost += cost * 0.10; cout << ceil(cost); return 0; }

27th Jun 2025, 3:16 AM
Md Mehedi Hasan
Md Mehedi Hasan - avatar
1 Respuesta
+ 2
Md Mehedi Hasan I added explicit int conversion in the cout statement as: cout << (int)ceil(cost); And it worked. But I can't really understand what the issue is. The result normally returns an integer anyway(and so it passes 4 of the 5 test cases). The error is, to my understanding, due to the cost being a certain floating number, that when converted with ceil, rounds up to a number it shouldn't. Maybe because of floating point precision, but I'm not sure either. I did check by using double instead of float, but that didn't fix the issue like (int) conversion did.
27th Jun 2025, 4:36 AM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar