isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
263 stars 57 forks source link

.R (round) with float second argument yields inconsistencies #133

Closed winny- closed 9 years ago

winny- commented 9 years ago

Hi, not sure if this is intended behavior or not. What I understood from the doc.txt is that .R with a float as arg b results in basically: round(a, get_precision(b)):

➜  pyth git:(master) git show-ref master
e72b4a42499b73b7fc50aca066752aa8ac0274c0 refs/heads/master
e72b4a42499b73b7fc50aca066752aa8ac0274c0 refs/remotes/origin/master
➜  pyth git:(master) python3
Python 3.4.2 (default, Jan 12 2015, 12:13:20) 
[GCC 4.9.2 20150107 (Red Hat 4.9.2-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyth import run_code
>>> run_code('.RQQ', '60.5')
('60.0\n', None)
>>> run_code('.RQQ', '-60.5')
('-60.5\n', None)
>>> run_code('.RQQ', '10.5')
('10.5\n', None)
>>> run_code('.RQQ', '-10.5')
('-10.5\n', None)

A quick and dirty fix: https://gist.github.com/winny-/8c13daeafd6850e0ac6d I think you can also do this using the decimal module.

Other discussion: http://chat.stackexchange.com/transcript/message/23007352#23007352

Comparison to plain python3 round(a, 1):

>>> round(60.5, 1)
60.5
>>> round(-60.5, 1)
-60.5
>>> round(10.5, 1)
10.5
>>> round(-10.5, 1)
-10.5
jakobkogler commented 9 years ago

@winny- Thanks for the bug report and the code.

Didn't use the code though, since there was an easier fix. Only had to change the condition b < 15 to round_len < 15.

winny- commented 9 years ago

:+1: Made my day! Enjoy some cake :cake:!