From 2dffb94785468947a59eb81285411b661a39b7a2 Mon Sep 17 00:00:00 2001 From: Charles Severance Date: Wed, 23 Jan 2013 18:15:29 -0500 Subject: [PATCH] Add support for the round() function Issue: 34 in on github Issue: 116 on Google code. Note: For some reason this does not pass the parser test. None of the unit tests I develop ever pass the parser test it might be my local dev envionment. But the unit test is solid and tests the functionality nicely. --- src/builtin.js | 15 ++++++++ src/builtindict.js | 1 + test/run/t332.py | 16 +++++++++ test/run/t332.py.real | 5 +++ test/run/t332.py.symtab | 42 ++++++++++++++++++++++ test/run/t332.trans | 79 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 test/run/t332.py create mode 100644 test/run/t332.py.real create mode 100644 test/run/t332.py.symtab create mode 100644 test/run/t332.trans diff --git a/src/builtin.js b/src/builtin.js index ae278308cc..072ef16904 100644 --- a/src/builtin.js +++ b/src/builtin.js @@ -76,6 +76,21 @@ Sk.builtin.abs = function abs(x) return Math.abs(x); }; +// http://stackoverflow.com/questions/11832914/round-up-to-2-decimal-places-in-javascript +Sk.builtin.round = function round(x,digits) +{ + if (typeof x != "number" ) { + throw "TypeError: a float is required"; + } + + if(typeof digits === "undefined") { + return Math.round(x); + } else { + var multiplier = Math.pow(10, digits); + return (Math.round(x * multiplier) / multiplier); + } +}; + Sk.builtin.ord = function ord(x) { if (x.constructor !== Sk.builtin.str || x.v.length !== 1) diff --git a/src/builtindict.js b/src/builtindict.js index 4d520dd601..caba1b3a86 100644 --- a/src/builtindict.js +++ b/src/builtindict.js @@ -9,6 +9,7 @@ Sk.builtins = { 'sum': Sk.builtin.sum, 'abs': Sk.builtin.abs, 'fabs': Sk.builtin.abs, // Added by RNL +'round': Sk.builtin.round, 'ord': Sk.builtin.ord, 'chr': Sk.builtin.chr, 'dir': Sk.builtin.dir, diff --git a/test/run/t332.py b/test/run/t332.py new file mode 100644 index 0000000000..088f99e20a --- /dev/null +++ b/test/run/t332.py @@ -0,0 +1,16 @@ +f = 2.515 +g = round(f) +# Deal with skulpt not printing whole floats with a ".0" +if ( g == 3.0 ) : + print "3.0" +else : + print g +g = round(f,1) +print g +g = round(f,2) +print g +g = round(f,3) +print g +g = round(f,4) +print g + diff --git a/test/run/t332.py.real b/test/run/t332.py.real new file mode 100644 index 0000000000..edd57f33e7 --- /dev/null +++ b/test/run/t332.py.real @@ -0,0 +1,5 @@ +3.0 +2.5 +2.52 +2.515 +2.515 diff --git a/test/run/t332.py.symtab b/test/run/t332.py.symtab new file mode 100644 index 0000000000..175b7c06ae --- /dev/null +++ b/test/run/t332.py.symtab @@ -0,0 +1,42 @@ +Sym_type: module +Sym_name: top +Sym_lineno: 0 +Sym_nested: False +Sym_haschildren: False +-- Identifiers -- +name: f + is_referenced: True + is_imported: False + is_parameter: False + is_global: False + is_declared_global: False + is_local: True + is_free: False + is_assigned: True + is_namespace: False + namespaces: [ + ] +name: g + is_referenced: True + is_imported: False + is_parameter: False + is_global: False + is_declared_global: False + is_local: True + is_free: False + is_assigned: True + is_namespace: False + namespaces: [ + ] +name: round + is_referenced: True + is_imported: False + is_parameter: False + is_global: True + is_declared_global: False + is_local: False + is_free: False + is_assigned: False + is_namespace: False + namespaces: [ + ] diff --git a/test/run/t332.trans b/test/run/t332.trans new file mode 100644 index 0000000000..3787011da3 --- /dev/null +++ b/test/run/t332.trans @@ -0,0 +1,79 @@ +Module(body=[Assign(targets=[Name(id='f', + ctx=Store())], + value=Num(n=2.5150000000000001)), + Assign(targets=[Name(id='g', + ctx=Store())], + value=Call(func=Name(id='round', + ctx=Load()), + args=[Name(id='f', + ctx=Load())], + keywords=[], + starargs=None, + kwargs=None)), + If(test=Compare(left=Name(id='g', + ctx=Load()), + ops=[Eq()], + comparators=[Num(n=3.0)]), + body=[Print(dest=None, + values=[Str(s='3.0')], + nl=True)], + orelse=[Print(dest=None, + values=[Name(id='g', + ctx=Load())], + nl=True)]), + Assign(targets=[Name(id='g', + ctx=Store())], + value=Call(func=Name(id='round', + ctx=Load()), + args=[Name(id='f', + ctx=Load()), + Num(n=1)], + keywords=[], + starargs=None, + kwargs=None)), + Print(dest=None, + values=[Name(id='g', + ctx=Load())], + nl=True), + Assign(targets=[Name(id='g', + ctx=Store())], + value=Call(func=Name(id='round', + ctx=Load()), + args=[Name(id='f', + ctx=Load()), + Num(n=2)], + keywords=[], + starargs=None, + kwargs=None)), + Print(dest=None, + values=[Name(id='g', + ctx=Load())], + nl=True), + Assign(targets=[Name(id='g', + ctx=Store())], + value=Call(func=Name(id='round', + ctx=Load()), + args=[Name(id='f', + ctx=Load()), + Num(n=3)], + keywords=[], + starargs=None, + kwargs=None)), + Print(dest=None, + values=[Name(id='g', + ctx=Load())], + nl=True), + Assign(targets=[Name(id='g', + ctx=Store())], + value=Call(func=Name(id='round', + ctx=Load()), + args=[Name(id='f', + ctx=Load()), + Num(n=4)], + keywords=[], + starargs=None, + kwargs=None)), + Print(dest=None, + values=[Name(id='g', + ctx=Load())], + nl=True)])