+ 2

Can someone please correct the code. There's an error in sololearn compiler.

when i enter 657 then it shows wrong output https://code.sololearn.com/cf1pGSJm9YOh/?ref=app

12th May 2017, 8:16 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
16 Antworten
+ 7
I apologize. Seems that this is indeed wierd as hell. still think it is doing some truncating but not on pow obviously. but because of the typecast. maybe it is converting the p to a double during the formula.. because 5.99 x 100 would equal the 599 result
12th May 2017, 12:03 PM
jay
jay - avatar
+ 6
If I solve it for you, do I get a cookie ?? https://code.sololearn.com/cwwyyCO3N8cb/#cpp
12th May 2017, 8:41 AM
jay
jay - avatar
+ 6
@JPM7 - Are we not allowed to use double?
12th May 2017, 8:51 AM
jay
jay - avatar
+ 6
@Rishabh: Hey but that wasn't part of the original question... If you break down the formula in the original you will see it converts a double into an int and as a result some data is lost. thusly in my solution i used a double to store results. Where as JDM removed the pow function and some math to remove the double from the equation entirely.
12th May 2017, 11:15 AM
jay
jay - avatar
+ 6
i.e either use a double to store temp or remove it entirely as JDM has. why type cast. you still loose accuracy as the pow formula is always going to return a double. implicitly type casting it inside the formula wont stop the decimal places from getting truncated
12th May 2017, 11:41 AM
jay
jay - avatar
+ 6
I already have... see the code I posted and enter a number. It works.
12th May 2017, 11:43 AM
jay
jay - avatar
+ 6
but they arent. pow is probably returning 9.9999999
12th May 2017, 11:45 AM
jay
jay - avatar
+ 5
ooooo! ok! I see
12th May 2017, 8:53 AM
jay
jay - avatar
+ 4
No need to apologize buddy.. it happens sometimes happy coding...
12th May 2017, 12:07 PM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 3
No its returning 100 check my code i have posted as compiler bug
12th May 2017, 11:46 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 2
but if both are integers i.e. 6*100 it should give 600 not 599?
12th May 2017, 11:43 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 2
So if i convert 6*100 to int implicitly than rather than getting 600 it gets converted to 599. That's why you declared temp as double
12th May 2017, 11:45 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 1
@jpm7 Hey but my question is why i am getting incorrect output in the input condition that I gave?
12th May 2017, 10:53 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 1
@JAY #include <iostream> #include <math.h> using namespace std; void express(int p) { int n=0,temp; while(p>0) { temp=(p%10)*(int)pow(10,n++);//type casting if(temp!=0) { cout<<temp; } p/=10; if(p!=0 && temp!=0) cout<<"+"; } } int main() { int a; cin>>a; express(a); } now the output reduces even more 7+50+594
12th May 2017, 11:23 AM
Rishabh Agrawal
Rishabh Agrawal - avatar
+ 1
@JAY just input a number and you will see it https://code.sololearn.com/cGA5S2L0DhZo/?ref=app
12th May 2017, 11:37 AM
Rishabh Agrawal
Rishabh Agrawal - avatar