We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

hint: use bc -l. Use 'scale' or an equivalent for appropriate decimal places! As you can see, many have got this challenge right. 17.92857 rounds off to 17.949 when corrected to 3 decimal places.

you are missing a " ; " after read X ... also, bc truncates in a strange way and the challenge author, in spite of writing (now) "...rounded up ...", expects a value rounded in another strange way. (Seems there are solutions using printf, but for doing it within bc only I had to use "... if ( x < 0 ) { x = x - 0.0004999;} else { x = x + 0.0004999; } ..." which looks highly nonstandard to me. (For normal mathematicians, round(x) := floor(x+0.5), so round(1.5) = 2 and round(-1.5) = -1. This is not the convention used here...)

## Arithmetic Operations

You are viewing a single comment's thread. Return to all comments →

Here's the result from a calculator: 5+50*3/20 + (19*2)/7 = 17.92857142857143

and yet you're expecting the solution to be: 17.929 ....

The solution is literally 1 line and I cannot see why you're expecting 17.929 instead of 17.928

hint: use bc -l. Use 'scale' or an equivalent for appropriate decimal places! As you can see, many have got this challenge right. 17.92857 rounds off to 17.949 when corrected to 3 decimal places.

I have used bc -l along with scale , still it rounds off it to 17.92857

Use a bc scale that's higher than printf's. e.g. Use scale=4 together with printf "%.3f". Only then you'll get hackerrank's expected result.

Please take a look at my submission:

read X echo "scale = 3; $X" | bc -l

this is not passing the first test for that specific error. All other tests are passing fine.

you are missing a " ; " after read X ... also,

`bc`

truncates in a strange way and the challenge author, in spite of writing (now) "...roundedup...", expects a value rounded in another strange way. (Seems there are solutions using printf, but for doing it within bc only I had to use "... if ( x < 0 ) { x = x - 0.0004999;} else { x = x + 0.0004999; } ..." which looks highly nonstandard to me. (For normal mathematicians, round(x) := floor(x+0.5), so round(1.5) = 2 and round(-1.5) = -1. This is not the convention used here...)it's a one line answer - can't really give a bigger hint without revealing the answer altogether :)

Instead of saying "display the output correct to 3 decimal places" you should say "display the output

roundedto 3 decimal places".'Correct' just means that users should give you what you want, and you never say that you want it rounded and not truncated.

I had the same problem and I used printf approach, which should not be what is expected...

printf "%.3f" $(echo "scope=3;5+50*3/20 + (19*2)/7"|bc -l)

No, leafmoon - people have got it correct. Or, message me the code you submitted and I'll try to see what the issue is.

using

`scale`

won't round the number. You could just try it yourself.