diff --git a/gracelib.c b/gracelib.c index 43abb9c..6bee2f6 100644 --- a/gracelib.c +++ b/gracelib.c @@ -1923,6 +1923,14 @@ Object Float64_Div(Object self, int nparts, int *argcv, b = integerfromAny(other); return alloc_Float64(a/b); } +Object Float64_Exp(Object self, int nparts, int *argcv, + Object *args, int flags) { + Object other = args[0]; + assertClass(other, Number); + double a = *(double*)self->data; + double b = *(double*)other->data; + return alloc_Float64(pow(a,b)); +} Object Float64_Mod(Object self, int nparts, int *argcv, Object *args, int flags) { Object other = args[0]; @@ -2083,11 +2091,12 @@ Object alloc_Float64(double num) { && Float64_Interned[ival-FLOAT64_INTERN_MIN] != NULL) return Float64_Interned[ival-FLOAT64_INTERN_MIN]; if (Number == NULL) { - Number = alloc_class2("Number", 26, (void*)&Float64__mark); + Number = alloc_class2("Number", 27, (void*)&Float64__mark); add_Method(Number, "+", &Float64_Add); add_Method(Number, "*", &Float64_Mul); add_Method(Number, "-", &Float64_Sub); add_Method(Number, "/", &Float64_Div); + add_Method(Number, "^", &Float64_Exp); add_Method(Number, "%", &Float64_Mod); add_Method(Number, "==", &Float64_Equals); add_Method(Number, "!=", &Object_NotEquals); diff --git a/js/gracelib.js b/js/gracelib.js index 20fd446..818e4b7 100644 --- a/js/gracelib.js +++ b/js/gracelib.js @@ -140,6 +140,10 @@ GraceNum.prototype = { var s = this._value - other._value; return new GraceNum(s) }, + "^": function(argcv, other) { + var s = Math.pow(this._value, other._value); + return new GraceNum(s) + }, "%": function(argcv, other) { var s = this._value % other._value; return new GraceNum(s) diff --git a/tests/t003_arithmetic.out b/tests/t003_arithmetic.out index 8a1218a..24ef04b 100644 --- a/tests/t003_arithmetic.out +++ b/tests/t003_arithmetic.out @@ -3,3 +3,5 @@ 3 4 5 +81 +2 diff --git a/tests/t003_arithmetic_test.grace b/tests/t003_arithmetic_test.grace index 9308575..1603436 100644 --- a/tests/t003_arithmetic_test.grace +++ b/tests/t003_arithmetic_test.grace @@ -3,3 +3,5 @@ print(1 + 1) print(6 / 2) print(2 * 2) print(15 % 10) +print(3 ^ 4) +print(4 ^ 0.5) diff --git a/typechecker.grace b/typechecker.grace index 3b644ef..5f40b61 100644 --- a/typechecker.grace +++ b/typechecker.grace @@ -28,6 +28,7 @@ def NumberType = ast.typeNode.new("Number", [ ast.methodTypeNode.new("*", [ast.signaturePart.new("*", [NumberOther])], NumberIdentifier), ast.methodTypeNode.new("-", [ast.signaturePart.new("-", [NumberOther])], NumberIdentifier), ast.methodTypeNode.new("/", [ast.signaturePart.new("/", [NumberOther])], NumberIdentifier), + ast.methodTypeNode.new("^", [ast.signaturePart.new("^", [NumberOther])], NumberIdentifier), ast.methodTypeNode.new("%", [ast.signaturePart.new("%", [NumberOther])], NumberIdentifier), ast.methodTypeNode.new("==", [ast.signaturePart.new("==", [TopOther])], BooleanIdentifier), ast.methodTypeNode.new("!=", [ast.signaturePart.new("!=", [TopOther])], BooleanIdentifier),