Skip to content

Commit

Permalink
Merge branch 'num_types' of github.com:blacatena/skulpt into blacaten…
Browse files Browse the repository at this point in the history
…a-num_types
  • Loading branch information
bnmnetp committed Mar 5, 2013
2 parents 903520a + f6e3a77 commit 0f1284c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 20 deletions.
67 changes: 48 additions & 19 deletions src/lib/math/__init__.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,102 @@
var $builtinmodule = function(name)
{
var mod = {};
mod.pi = Sk.builtin.assk$(Math.PI, Sk.builtin.float$);
mod.e = Sk.builtin.assk$(Math.E, Sk.builtin.float$);
mod.pi = Sk.builtin.assk$(Math.PI, Sk.builtin.nmber.float$);
mod.e = Sk.builtin.assk$(Math.E, Sk.builtin.nmber.float$);

mod.abs = new Sk.builtin.func(function(x) {
return Sk.builtin.assk$(Math.abs(Sk.builtin.asnum$(x)), undefined);
return Sk.builtin.assk$(Math.abs(Sk.builtin.asnum$(x)), undefined);
});

// RNL added
mod.fabs = new Sk.builtin.func(function(x) {
return Sk.builtin.assk$(Math.abs(Sk.builtin.asnum$(x)), Sk.builtin.float$);
return Sk.builtin.assk$(Math.abs(Sk.builtin.asnum$(x)), Sk.builtin.nmber.float$);
});

mod.asin = new Sk.builtin.func(function(rad) {
return Sk.builtin.assk$(Math.asin(Sk.builtin.asnum$(rad)), undefined);
return Sk.builtin.assk$(Math.asin(Sk.builtin.asnum$(rad)), Sk.builtin.nmber.float$);
});

mod.acos = new Sk.builtin.func(function(rad) {
return Sk.builtin.assk$(Math.acos(Sk.builtin.asnum$(rad)), undefined);
return Sk.builtin.assk$(Math.acos(Sk.builtin.asnum$(rad)), Sk.builtin.nmber.float$);
});

mod.atan = new Sk.builtin.func(function(rad) {
return Sk.builtin.assk$(Math.atan(Sk.builtin.asnum$(rad)), undefined);
return Sk.builtin.assk$(Math.atan(Sk.builtin.asnum$(rad)), Sk.builtin.nmber.float$);
});

mod.atan2 = new Sk.builtin.func(function(x, y) {
return Sk.builtin.assk$(Math.atan2(Sk.builtin.asnum$(x), Sk.builtin.asnum$(y)), Sk.builtin.nmber.float$);
});

mod.sin = new Sk.builtin.func(function(rad) {
return Sk.builtin.assk$(Math.sin(Sk.builtin.asnum$(rad)), undefined);
return Sk.builtin.assk$(Math.sin(Sk.builtin.asnum$(rad)), Sk.builtin.nmber.float$);
});

mod.cos = new Sk.builtin.func(function(rad) {
return Sk.builtin.assk$(Math.cos(Sk.builtin.asnum$(rad)), undefined);
return Sk.builtin.assk$(Math.cos(Sk.builtin.asnum$(rad)), Sk.builtin.nmber.float$);
});

mod.tan = new Sk.builtin.func(function(rad) {
return Sk.builtin.assk$(Math.tan(Sk.builtin.asnum$(rad)), undefined);
return Sk.builtin.assk$(Math.tan(Sk.builtin.asnum$(rad)), Sk.builtin.nmber.float$);
});

mod.ceil = new Sk.builtin.func(function(x) {
return Sk.builtin.assk$(Math.ceil(Sk.builtin.asnum$(x)), undefined);
return Sk.builtin.assk$(Math.ceil(Sk.builtin.asnum$(x)), Sk.builtin.nmber.float$);
});

mod.floor = new Sk.builtin.func(function(x) {
return Sk.builtin.assk$(Math.floor(Sk.builtin.asnum$(x)), undefined);
return Sk.builtin.assk$(Math.floor(Sk.builtin.asnum$(x)), Sk.builtin.nmber.float$);
});

mod.sqrt = new Sk.builtin.func(function(x) {
return Sk.builtin.assk$(Math.sqrt(Sk.builtin.asnum$(x)), undefined);
return Sk.builtin.assk$(Math.sqrt(Sk.builtin.asnum$(x)), Sk.builtin.nmber.float$);
});

mod.log = new Sk.builtin.func(function(x) {
return Sk.builtin.assk$(Math.log(Sk.builtin.asnum$(x)), undefined);
mod.log = new Sk.builtin.func(function(x, b) {
if (b) {
return Sk.builtin.assk$((Math.log(Sk.builtin.asnum$(x)) / Math.log(Sk.builtin.asnum$(b))), Sk.builtin.nmber.float$);
}
return Sk.builtin.assk$(Math.log(Sk.builtin.asnum$(x)), Sk.builtin.nmber.float$);
});

mod.log10 = new Sk.builtin.func(function(x, b) {
return Sk.builtin.assk$((Math.log(Sk.builtin.asnum$(x)) / Math.log(Sk.builtin.asnum$(10))), Sk.builtin.nmber.float$);
});

mod.exp = new Sk.builtin.func(function(x) {
return Sk.builtin.assk$(Math.exp(Sk.builtin.asnum$(x)), undefined);
return Sk.builtin.assk$(Math.exp(Sk.builtin.asnum$(x)), Sk.builtin.nmber.float$);
});

mod.pow = new Sk.builtin.func(function(x,y) {
return Sk.builtin.assk$(Math.pow(Sk.builtin.asnum$(x),Sk.builtin.asnum$(y)), undefined);
return Sk.builtin.assk$(Math.pow(Sk.builtin.asnum$(x),Sk.builtin.asnum$(y)), Sk.builtin.nmber.float$);
});

mod.radians = new Sk.builtin.func(function(deg) {
return Sk.builtin.assk$(Math.PI / 180.0 * Sk.builtin.asnum$(deg), undefined);
return Sk.builtin.assk$(Math.PI / 180.0 * Sk.builtin.asnum$(deg), Sk.builtin.nmber.float$);
});

mod.degrees = new Sk.builtin.func(function(rad) {
return Sk.builtin.assk$(180.0 / Math.PI * Sk.builtin.asnum$(rad), undefined);
return Sk.builtin.assk$(180.0 / Math.PI * Sk.builtin.asnum$(rad), Sk.builtin.nmber.float$);
});

mod.trunc = new Sk.builtin.func(function(x) {
return Sk.builtin.assk$(Math.floor(Sk.builtin.asnum$(x)), Sk.builtin.nmber.int$);
});

mod.hypot = new Sk.builtin.func(function(x, y) {
x = Sk.builtin.asnum$(x);
y = Sk.builtin.asnum$(y);
return Sk.builtin.assk$(Math.sqrt((x*x)+(y*y)), Sk.builtin.nmber.float$);
});

mod.factorial = new Sk.builtin.func(function(x) {
x = Math.floor(Sk.builtin.asnum$(x));
var r = 1;
for (var i = 2; i <= x; i++)
r *= i;
return Sk.builtin.assk$(r, Sk.builtin.nmber.int$);
});

return mod;
}
2 changes: 1 addition & 1 deletion src/lib/turtle/__init__.js
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ var $builtinmodule = function(name) {
$loc.distance = new Sk.builtin.func(function(self, tx, ty) {
tx = Sk.builtin.asnum$(tx);
ty = Sk.builtin.asnum$(ty);
if ((typeof(tx)).toLowerCase() === 'number')
if ((typeof(tx)).toLowerCase() === 'number') {
tx = [tx, ty, 0];
} else {
tx = [tx.theTurtle.getx(), tx.theTurtle.gety(), 0];
Expand Down
41 changes: 41 additions & 0 deletions test/t998.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import math

print "ceil",type(math.ceil(3)),math.ceil(3)
print "fabs",type(math.fabs(3)),math.fabs(3)
print "factorial",type(math.factorial(3)),math.factorial(3)
print "floor",type(math.floor(3)),math.floor(3)
#print "fmod",type(math.fmod(3,1)),math.fmod(3,1)
#print "frexp",type(math.frexp(3)),math.frexp(3)
#print "fsum",type(math.fsum([1,2,3])),math.fsum([1,2,3])
#print "isinf",type(math.isinf(3)),math.isinf(3)
#print "isnan",type(math.isnan(3)),math.isnan(3)
#print "ldexp",type(math.ldexp(3,1)),math.ldexp(3,1)
#print "modf",type(math.modf(3)),math.modf(3)
print "trunc",type(math.trunc(3)),math.trunc(3)
print "exp",type(math.exp(3)),math.exp(3)
print "log",type(math.log(3)),math.log(3)
#print "log1p",type(math.log1p(3)),math.log1p(3)
#print "log10",type(math.log10(3)),math.log10(3)
print "pow",type(math.pow(3,3)),math.pow(3,3)
print "sqrt",type(math.sqrt(9)),math.sqrt(9)
print "acos",type(math.acos(1)),math.acos(1)
print "asin",type(math.asin(1)),math.asin(1)
print "atan",type(math.atan(3)),math.atan(3)
print "atan2",type(math.atan2(3,3)),math.atan2(3,3)
print "cos",type(math.cos(3)),math.cos(3)
print "hypot",type(math.hypot(3,4)),math.hypot(3,4)
print "sin",type(math.sin(3)),math.sin(3)
print "tan",type(math.tan(3)),math.tan(3)
print "degrees",type(math.degrees(math.pi)),math.degrees(3)
print "radians",type(math.radians(3)),math.radians(3)
#print "acosh",type(math.acosh(1)),math.acosh(1)
#print "asinh",type(math.asinh(1)),math.asinh(1)
#print "cosh",type(math.cosh(3)),math.cosh(3)
#print "sinh",type(math.sinh(3)),math.sinh(3)
#print "tanh",type(math.tanh(3)),math.tanh(3)
#print "erf",type(math.erf(1)),math.erf(1)
#print "erfc",type(math.erfc(1)),math.erfc(1)
#print "gamma",type(math.gamma(3)),math.gamma(3)
#print "lgamma",type(math.lgamma(3)),math.lgamma(3)
print "pi",type(math.pi),math.pi
print "e",type(math.e),math.e

0 comments on commit 0f1284c

Please sign in to comment.