Skip to content

Commit

Permalink
Add some basic handling of powers that are non-integers
Browse files Browse the repository at this point in the history
  • Loading branch information
olabini committed Mar 15, 2010
1 parent f43e83b commit 9d816ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ikj/main/ioke/lang/Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Map;

import gnu.math.Complex;
import gnu.math.DFloNum;
import gnu.math.BitOps;
import gnu.math.IntNum;
import gnu.math.RatNum;
Expand Down Expand Up @@ -536,7 +537,11 @@ public Object activate(IokeObject method, Object on, List<Object> args, Map<Stri
IokeData data = IokeObject.data(arg);

if(!(data instanceof Number)) {
arg = IokeObject.convertToRational(arg, message, context, true);
if(data instanceof Decimal) {
return context.runtime.newDecimal(((RealNum)(Complex.power(Number.value(on), new DFloNum(Decimal.value(arg).toString())))).asBigDecimal());
} else {
arg = IokeObject.convertToRational(arg, message, context, true);
}
}

return context.runtime.newNumber((RatNum)Number.value(on).power(Number.intValue(arg)));
Expand Down
5 changes: 5 additions & 0 deletions test/number_spec.ik
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@ describe(Number,
(2 ** 40) should == 1099511627776
)

it("should be able to raise a number to a decimal number",
(4 ** 0.5) should == 2
(124 ** 2.3) should be close(65293.4889760738)
)

it("should convert its argument to a rational if its not a number or a decimal",
x = Origin mimic
x asRational = method(3)
Expand Down

0 comments on commit 9d816ee

Please sign in to comment.