+ 1

kaleidoscopes

https://code.sololearn.com/cVaRCJBB8Kst/?ref=app I wrote this code but it fails in some cases in rounding up the answer when it has 0.5 or p.005 to 1 or to 0.1 how can i solve this

15th Jan 2023, 8:24 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
7 odpowiedzi
+ 2
You need to round upto 2 decimal places only.. So ceil won't work. Need to use round function. Instead you can try by taking total as double type simply.. double total; // this solves there, instead float edit: Dareen Moughrabi that's a floating issue in programming languages. and default, a precision type is double. Doubles give you accurate values than float. when you use float, a double type input value is converted to float type so it is may be greater or less value than accurate value. p.s: when you need accurate values of precision type, then use double type. when accuracy is not important and memory usage is important then use float type as it takes only 4 bytes but double takes 8 bytes.
15th Jan 2023, 9:18 AM
Jayakrishna 🇮🇳
+ 2
You're welcome.. Preciously forgot to add reason, so late answer. Already, added as edit in previous reply.
17th Jan 2023, 11:01 AM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Thx it worked but can you explain why when changing the data type to double instead of float it works ?
15th Jan 2023, 12:36 PM
Dareen Moughrabi
Dareen Moughrabi - avatar
+ 1
Jayakrishna🇮🇳 Thx i was trying to develop a habit into using less data unless needed and thought double is more precise just if i want a huge number of digits thx for the information learnt a lot
17th Jan 2023, 10:57 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
I tried using ceil but it does rounds up number like 0.1 so it causes some errors
15th Jan 2023, 8:32 AM
Dareen Moughrabi
Dareen Moughrabi - avatar
0
test this : int main() { int num,price=5; cin>>num; float total; if(num>1){ total =num*price*0.90*1.07; }else{ total=num*price*1.07; } std::cout << std::setprecision(2) <<std::fixed << std::round(total * 100) / 100.0f << std::endl;; return 0; }
15th Jan 2023, 8:55 AM
Reza Mardani
Reza Mardani - avatar
0
im Reza i tried what you provided yet it did not work ,i recived some errors about the &gt and &lt yet thx for helping out 🤝
15th Jan 2023, 12:40 PM
Dareen Moughrabi
Dareen Moughrabi - avatar