+ 21
'Paint Cost' task in Code Coach
'A number that represents the cost of your purchase *rounded up* to the nearest whole number' they write. However, ceil let's the code fail, round let's it pass. What's going on?
36 ответов
+ 11
Well spotted, Coder Kitten!
I guess they have to revamp their tests. It's not good if you make people fail with the logical solution and pass with some side track that only works because of some language specific float shenanigans.
+ 9
Use "n*5.5+44" instead. Multiplying by 1.1 can lead to floating-point inacurracies.
For example, you can get 42.00000000000003 as the result. If you use round() you get 42, but if you use ceil() you get 43. See for yourself:
import math
print("n f(n) round() ceil()\n"+"-"*36)
for n in range(42):
x = (40+5*n)*1.1
print(f"{n:<4}{x:<20}{round(x):<5}{math.ceil(x)}")
+ 7
Ok this is how did it in java
if ( cost / 100d >=5)
{//ceil it)
else if (cost / 100d < 5)
{round it}
// basically if the decimal point is > = 5,I'd make the cost go up, else make round off to real digit
+ 7
Schindlabua You mean even after type conversion??
+ 6
Daniel Cooper, I'm not asking for help, I passed the task.
But when I used ceil, as the description demands, the code failed, and when I used round, the code succeeded. Both in C and Python.
+ 6
I solved it using Python/C/C++/Ruby and ceil() works, but round() doesn't.
+ 5
Aah, interesting! oO
So it's basically a coincidence that rounding with my version passes all the tests?
It's a bit strange to write it like that, because intuitively you want to calculate the cost, then add taxes.
I get the feeling, the tasks could use a bit of tweaking!
+ 5
I ran into the same thing in C++ and ended up writing a solution that uses integer arithmetic only. Floats are kind of annoying, once you go float you can never go back. :/
+ 5
Schindlabua Yess I completely agree on that, once A-float, niether . floor nor . ceil-ing can save you from getting point errors 😂😂
+ 4
Okay, that's strange.
+ 4
Diego, this was my Python code:
n = int(input())
print(round((40 + 5*n) *1.1))
And this is the C version (basically the same):
#include <stdio.h>
#include <math.h>
int main() {
unsigned colors;
scanf("%d", &colors);
printf("%d", (int)round((40+colors*5)*1.1));
return 0;
}
Do you see how they could have passed, while with ceil they failed?
+ 4
This works for me in Python:
import math
n = int(input())
cost = ((40 + (n * 5)) * 11)
cost = math.ceil(cost/10)
print(cost)
But it also looks like a work a round 😉
+ 4
Selin Genkur, I don't think it's a good idea to generalize that.
Also, sometimes the problem may be a bit more tricky than that. Read for example here:
https://www.sololearn.com/discuss/2104980/?ref=app
I think the tasks are just designed a bit sloppily (too little time invested in testing properly and to find precise wording) and could use some adjustments.
+ 3
Diego, I just tried it:
If I use your method in Python, I fail one test with round, but pass them all with ceil.
But in C, it *still* works, even with round!
Although the underlying 'doubles' should be the very same in Python and C!
Coder Kitten, writing literals in C should create doubles rather than floats.
So the same problem should occur in the C version as well, right?
+ 3
A. S. M. [ iNACTIVE ] No I mean, if you use floats you'll run into floating point errors and all the maths is slightly wrong and you can't really prevent that.
For finance stuff you'd better use integral or fractional datatypes so stuff like that doesn't happen.
+ 3
A. S. M. [ iNACTIVE ] All we can do is floor and pray :D
+ 2
What do you mean by the line with Indians, Selin Genkur? What's that got to do with it?
+ 2
Selin Genkur Ohh no no no,You can't offend us Indians anymore, You see Sundar Pichai is the CEO of google, We have our own Silicon valley in India called Bangalore, So guess what, the jokes on you 😁😝
+ 2
I tried and: round() failed, ceil() failed, so I had to first round it up to two decimal places and then ceil()... it is usually unexpected 0.0000000001 that causes the problem
+ 1
HonFu Can I see your attempt?