Skip to content

Commit

Permalink
Expanded math library, set types as per spec
Browse files Browse the repository at this point in the history
  • Loading branch information
blacatena committed Mar 3, 2013
1 parent 46b84e3 commit caff358
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 19 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;
}
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 caff358

Please sign in to comment.