Skip to content

Commit

Permalink
Add support for the round() function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
csev committed Jan 23, 2013
1 parent 9f3630f commit 2dffb94
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/builtindict.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions test/run/t332.py
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions test/run/t332.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
3.0
2.5
2.52
2.515
2.515
42 changes: 42 additions & 0 deletions test/run/t332.py.symtab
Original file line number Diff line number Diff line change
@@ -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: [
]
79 changes: 79 additions & 0 deletions test/run/t332.trans
Original file line number Diff line number Diff line change
@@ -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)])

0 comments on commit 2dffb94

Please sign in to comment.