Skip to content

Commit

Permalink
Add ^ method on Numbers for exponentiation
Browse files Browse the repository at this point in the history
  • Loading branch information
apblack authored and mwh committed Nov 1, 2012
1 parent 32e9c81 commit da8d697
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion gracelib.c
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions js/gracelib.js
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/t003_arithmetic.out
Expand Up @@ -3,3 +3,5 @@
3
4
5
81
2
2 changes: 2 additions & 0 deletions tests/t003_arithmetic_test.grace
Expand Up @@ -3,3 +3,5 @@ print(1 + 1)
print(6 / 2)
print(2 * 2)
print(15 % 10)
print(3 ^ 4)
print(4 ^ 0.5)
1 change: 1 addition & 0 deletions typechecker.grace
Expand Up @@ -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),
Expand Down

0 comments on commit da8d697

Please sign in to comment.