For some reason, the calculation results do not agree ?! | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 6

For some reason, the calculation results do not agree ?!

the results of calculations do not agree, on tests to solve "Kaleidoscopes", in Python everything is correct, in Ruby there is no. I tried various options for Ruby, including (cst * 100.0) .round () / 100.0 - but not. #python icnt = int( input() ) cst = float(icnt) * 5.0 if icnt > 1: cst -= cst / 10.0 cst += 7.0 * cst / 100.0 print( round(cst,2) ) #ruby icnt = gets.to_i cst = icnt.to_f * 5.0 if icnt > 1 cst -= cst / 10.0 end cst += 7.0 * cst / 100.0 puts cst.round(2)

21st Dec 2019, 4:18 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
10 ответов
+ 3
Michail Getmanskiy Lothar I did a little more digging because my solution feels wrong. I found that the test mechanism's math is the issue. Look at this code, which I ran in a Ruby code bit: quantity = 1023 # this next line fails Kaleidoscope test 4 puts(4925.75) # this line passes Kaleidoscope test 4 puts(4925.74) # this line outputs 4925.75 puts((quantity*5*0.9*1.07).round(2)) 4,925.75 is in fact the properly rounded value for a quantity of 1,023. But test 4 on Code Coach Challenge is clearly looking for a value of 4,925.74 which is incorrect. So my conclusion is that Ruby is rounding as it does in Python, but the test apparatus being used is where the rounding discrepancy is located. Maybe David Carroll can alert the test generator of the issue. [Edit] my conclusion was not quite right above, see my post below to find out why.
8th Oct 2020, 12:48 AM
Paul K Sadler
Paul K Sadler - avatar
21st Dec 2019, 4:45 PM
Lothar
Lothar - avatar
+ 4
I just run both codes and did insert a print for "cst" without anything else just before the last line also in both codes. The result is: python input 345 cst 1661.175 last line 1661.17 ruby input 345 cst 1661.175 last line 1661.18 So the difference is about rounding.
21st Dec 2019, 4:40 PM
Lothar
Lothar - avatar
+ 4
Lothar: Nevertheless thank you.
21st Dec 2019, 5:02 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 3
I understand it. I don’t understand, that: And how to treat it?
21st Dec 2019, 4:42 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 3
ok, so im sorry, but i am not very familiar in ruby. May be anyone else can help?
21st Dec 2019, 4:54 PM
Lothar
Lothar - avatar
+ 3
Lothar: Purpose: to write Ruby code solving the problem already solved in Python - "Kaleidoscopes".
21st Dec 2019, 4:55 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 3
Michail Getmanskiy Lothar Not sure if you ever got a satisfying answer to this rounding issue in Ruby. I ran into the same thing you noted in your post. Solved it in Python, but that solution didn't work in Ruby. It's a floating point rounding issue. And it comes up on test 4. I came up with a hack to pass the test (see below), but its not scalable. if quantity == 1 print(5.35) else adj = quantity == 1023 ? 0.001 : 0 print((quantity*5*0.9*1.07-adj).round(2)) end
7th Oct 2020, 4:32 PM
Paul K Sadler
Paul K Sadler - avatar
+ 3
Michail Getmanskiy Lothar To have a proper touchstone I did the math first on my computers calculator app and then on an adding machine I have from the old days 😂 Both yield the same answer: 4,925.745. Python Code Bit: quantity = 1023 # this next line fails test 4 print(4925.75) # this next line passes test 4 print(4925.74) # this next line correctly outputs 4925.745 print((quantity*5*0.9*1.07)) # this line incorrectly outputs 4925.74 print(round(quantity*5*0.9*1.07,2)) Ruby Code Bit: quantity = 1023 # this next line fails test 4 puts(4925.75) # this next line passes test 4 puts(4925.74) # this next line correctly outputs 4925.745 puts((quantity*5*0.9*1.07)) # this next line correctly outputs 4925.75 puts((quantity*5*0.9*1.07).round(2)) My conclusion is that Python did not round correctly and Ruby did round correctly. I base this on the touchstone calculation from my calculator app and legacy adding machine. hack required to get the incorrect correct answer to pass the test in Ruby
8th Oct 2020, 2:22 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
Lothar: This is understandable ... Thank you. But the problem is that in Python this passes all the tests that were created by the creators of the task, but in Ruby it doesn’t. At what, than on Ruby I tried round (2), floor (2), ceil (2) and the option with (cst * 100.0) .round () / 100.0 - not a single option passes all the tests ... but they fall on different ... in Python, everything is in order.
21st Dec 2019, 4:51 PM
Michail Getmanskiy
Michail Getmanskiy - avatar