+ 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
7 Respostas
+ 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.
+ 2
You're welcome..
Preciously forgot to add reason, so late answer.
Already, added as edit in previous reply.
+ 1
Jayakrishna🇮🇳
Thx it worked but can you explain why when changing the data type to double instead of float it works ?
+ 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
0
I tried using ceil but it does rounds up number like 0.1 so it causes some errors
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;
}
0
im Reza i tried what you provided yet it did not work ,i recived some errors about the > and < yet thx for helping out 🤝