0

[Solved][Spoiler] Paint cost?

Don't read this question or it's answers if you don't want the solution So I'm doing the paint cost coach and this code fails 2 of the test cases. Can somebody give me a hint? What's wrong? https://code.sololearn.com/c9V8ShOswjsh/?ref=app

9th Jan 2020, 10:21 AM
Daniel Cooper
Daniel Cooper - avatar
17 ответов
+ 6
Must be some floating point precision stuff leading to errors there, I guess. It works if you a) divide cost by 10 instead of multiplying by 0.1 and b) cast the obtained value from rounding up to int when printing it, but don't ask me why, lol.
9th Jan 2020, 10:35 AM
Shadow
Shadow - avatar
+ 3
Another approach that I sometimes use is to replace double with float. I tried it in your code and saw consistently correct results. In similar fashion I have made improvements on other programs in SoloLearn. I had reasoned that using float filtered out unwanted smaller extra bits that double can store. (In technical terms, float has a larger epsilon than double has). However, I am not so sure that is a sufficient explanation here. I think this is worth further investigation and will see if I can find time to analyze it further. EDIT: Using 10 for input I found that the double precision calculation ended with its smallest bit set. That made it high by about 4.94x10^-324, which was enough to push the ceil up from 99 to 100. I did not find any wrong calculation. It is the nature of decimal-to-binary representation.
10th Jan 2020, 4:19 PM
Brian
Brian - avatar
+ 2
Shadow My guess is it works better because 0.10 itself can't be stored without some error. Then, when you multiply with it, that error gets even bigger. 10.0 can be stored without any error I believe. Daniel Cooper When you're working with money, in the real world, it's usually a better idea to work with integrals instead. Instead of storing 3.56 in a float, you'd store 356 in an int ( or long long ) for 2 decimal precision or 3560 for 3. That way you're not affected by floating point errors.
9th Jan 2020, 12:26 PM
Dennis
Dennis - avatar
+ 2
// Paint costs in c++ #include <iostream> #include <cmath> using namespace std; int main() { int numberOfcolor,total_cost; double cost,tax; cin>>numberOfcolor; cost=(numberOfcolor*5)+40; tax=cost/10; total_cost=round(tax)+cost; cout<<total_cost; }
24th Feb 2020, 12:54 PM
Harshit
Harshit - avatar
+ 2
Naman Chhabra you will get the most value out of Code Coach if you solve it yourself. As for me, floats in C and had no problems passing the tests.
23rd Sep 2020, 3:28 AM
Brian
Brian - avatar
+ 2
Naman Chhabra I think it works best if you separately calculate and store the subtotal, tax, total, and then find the ceiling of the total to round up to dollars. Just now I went back to my code and tried combining some steps to see if that was why some attempts fail, and this time some tests failed. Of course, restoring the code worked again.
23rd Sep 2020, 4:27 AM
Brian
Brian - avatar
+ 2
Naman Chhabra you are welcome!
23rd Sep 2020, 5:27 AM
Brian
Brian - avatar
+ 2
#include <iostream> #include<cmath> using namespace std; int main() { int color,tax,total,final; cin>>color; final=40 +color*5; tax=floor(final/10); if(tax>1000 && tax<10000){ tax++; } total =final +tax; cout<<total; return 0; } //THIS WORKED FOR ME GUYS
26th Apr 2021, 5:57 PM
Alto
Alto - avatar
+ 1
Dennis, I also had some ideas like that, thanks for clarifying. Do you also happen to have any thoughts on why the result needs to be casted to int? For example, casting the return value of std::ceil(), which is supposed to be an integer value - although returned as a float - and then storing the result in a double variable also provokes an error for me, although the final output that is being printed is identical to when you cast the variable itself to int when printing it. It would be really interesting to know how the solutions are evaluated and compared.
9th Jan 2020, 12:43 PM
Shadow
Shadow - avatar
+ 1
Shadow I did a quick test using both methods and comparing the results, but there were no differences. So I think this is just a sololearn issue.
9th Jan 2020, 4:30 PM
Dennis
Dennis - avatar
+ 1
Thanks to everyone who answered. I ended up doing what Shadow said and it worked. Why did it work Shadow? Anyway, I'll probably do some research to figure out the weird behavior.
10th Jan 2020, 6:46 AM
Daniel Cooper
Daniel Cooper - avatar
+ 1
Daniel Cooper In case you did not follow our conversation, I can't exactly explain it either, especially without knowing how they fetch the solution and evaluate it to yours. As stated by Dennis, some issues from the side of SoloLearn should play a role here as well. Dennis Thanks for taking your time checking it out anyway. Hopefully it might be resolved in the future.
10th Jan 2020, 3:04 PM
Shadow
Shadow - avatar
+ 1
Brian My code is fail two test codes i am unable to find the mistake
23rd Sep 2020, 3:31 AM
Naman Chhabra
Naman Chhabra - avatar
+ 1
Thank you so much brother🙏
23rd Sep 2020, 4:35 AM
Naman Chhabra
Naman Chhabra - avatar
0
Can anybody give the correct solution of paint cost problem??
23rd Sep 2020, 3:21 AM
Naman Chhabra
Naman Chhabra - avatar
0
This works for me I know it is very old Post, after many years I back to SL a few days back. #include <iostream> #include <cmath> using namespace std; int main() { int noOfPaint; cin>>noOfPaint; cout<< int(round(((noOfPaint * 5) + 40) * 1.1)); return 0; }
11th Jun 2021, 8:21 AM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
0
This was a beginner challenge marked "easy" and meant to be solved using beginner tools. Using things like #include <cmath>, round and ceil had not been taught yet at the "easy" level so the expectation is probably to use what you *have* been taught to solve the challenge. #include <iostream> using namespace std; int main() { int colors ; cin >> colors ; float preTax(40 + (colors*5)) ; float taxExp(preTax+preTax/10+.99); int roundUp(taxExp); cout << roundUp ; return 0; } https://code.sololearn.com/c8Pz0487WIIQ/?ref=app
10th Nov 2022, 12:09 AM
Scott D
Scott D - avatar