1
0
forked from Mirror/wren

Merge branch 'num_methods' of git://github.com/bjorn/wren into bjorn-num_methods

This commit is contained in:
Bob Nystrom
2015-03-21 12:54:08 -07:00
9 changed files with 87 additions and 0 deletions

View File

@ -8,6 +8,23 @@ The absolute value of the number.
:::dart
-123.abs // 123
### **acos**
The arc cosine of the number.
### **asin**
The arc sine of the number.
### **atan**
The arc tangent of the number.
### **atan**(x)
The arc tangent of the number when divided by `x`, using the signs of the two
numbers to determine the quadrant of the result.
### **ceil**
Rounds the number up to the nearest integer.
@ -42,6 +59,10 @@ The sine of the number.
The square root of the number. Returns `nan` if the number is negative.
### **tan**
The tangent of the number.
### **-** operator
Negates the number.
@ -118,3 +139,6 @@ Attempts to parse `value` as a decimal literal and return it as an instance of
It is a runtime error if `value` is not a string.
### Num.**pi**
The value of π.

View File

@ -907,6 +907,26 @@ DEF_PRIMITIVE(num_abs)
RETURN_NUM(fabs(AS_NUM(args[0])));
}
DEF_PRIMITIVE(num_acos)
{
RETURN_NUM(acos(AS_NUM(args[0])));
}
DEF_PRIMITIVE(num_asin)
{
RETURN_NUM(asin(AS_NUM(args[0])));
}
DEF_PRIMITIVE(num_atan)
{
RETURN_NUM(atan(AS_NUM(args[0])));
}
DEF_PRIMITIVE(num_atan2)
{
RETURN_NUM(atan2(AS_NUM(args[0]), AS_NUM(args[1])));
}
DEF_PRIMITIVE(num_ceil)
{
RETURN_NUM(ceil(AS_NUM(args[0])));
@ -960,6 +980,11 @@ DEF_PRIMITIVE(num_sqrt)
RETURN_NUM(sqrt(AS_NUM(args[0])));
}
DEF_PRIMITIVE(num_tan)
{
RETURN_NUM(tan(AS_NUM(args[0])));
}
DEF_PRIMITIVE(num_toString)
{
RETURN_VAL(wrenNumToString(vm, AS_NUM(args[0])));
@ -1006,6 +1031,11 @@ DEF_PRIMITIVE(num_negate)
RETURN_NUM(-AS_NUM(args[0]));
}
DEF_PRIMITIVE(num_pi)
{
RETURN_NUM(3.14159265358979323846);
}
DEF_PRIMITIVE(num_minus)
{
if (!validateNum(vm, args, 1, "Right operand")) return PRIM_ERROR;
@ -1544,6 +1574,7 @@ void wrenInitializeCore(WrenVM* vm)
vm->numClass = defineClass(vm, "Num");
PRIMITIVE(vm->numClass->obj.classObj, "fromString(_)", num_fromString);
PRIMITIVE(vm->numClass->obj.classObj, "pi", num_pi);
PRIMITIVE(vm->numClass, "-", num_negate);
PRIMITIVE(vm->numClass, "-(_)", num_minus);
PRIMITIVE(vm->numClass, "+(_)", num_plus);
@ -1563,6 +1594,10 @@ void wrenInitializeCore(WrenVM* vm)
PRIMITIVE(vm->numClass, "..(_)", num_dotDot);
PRIMITIVE(vm->numClass, "...(_)", num_dotDotDot);
PRIMITIVE(vm->numClass, "abs", num_abs);
PRIMITIVE(vm->numClass, "acos", num_acos);
PRIMITIVE(vm->numClass, "asin", num_asin);
PRIMITIVE(vm->numClass, "atan", num_atan);
PRIMITIVE(vm->numClass, "atan(_)", num_atan2);
PRIMITIVE(vm->numClass, "ceil", num_ceil);
PRIMITIVE(vm->numClass, "cos", num_cos);
PRIMITIVE(vm->numClass, "floor", num_floor);
@ -1571,6 +1606,7 @@ void wrenInitializeCore(WrenVM* vm)
PRIMITIVE(vm->numClass, "sign", num_sign);
PRIMITIVE(vm->numClass, "sin", num_sin);
PRIMITIVE(vm->numClass, "sqrt", num_sqrt);
PRIMITIVE(vm->numClass, "tan", num_tan);
PRIMITIVE(vm->numClass, "toString", num_toString);
PRIMITIVE(vm->numClass, "truncate", num_truncate);

View File

@ -0,0 +1,3 @@
IO.print(0.acos) // expect: 1.5707963267949
IO.print(1.acos) // expect: 0
IO.print((-1).acos) // expect: 3.1415926535898

View File

@ -0,0 +1,3 @@
IO.print(0.asin) // expect: 0
IO.print(1.asin) // expect: 1.5707963267949
IO.print((-1).asin) // expect: -1.5707963267949

View File

@ -0,0 +1,2 @@
IO.print(0.atan) // expect: 0
IO.print(1.atan) // expect: 0.78539816339745

View File

@ -0,0 +1,4 @@
IO.print(0.atan(0)) // expect: 0
IO.print(0.atan(1)) // expect: 0
IO.print(1.atan(0)) // expect: 1.5707963267949

View File

@ -0,0 +1,6 @@
IO.print(0.cos) // expect: 1
IO.print(Num.pi.cos) // expect: -1
IO.print((2 * Num.pi).cos) // expect: 1
// this should of course be 0, but it's not that precise
IO.print((Num.pi / 2).cos) // expect: 6.1232339957368e-17

View File

@ -0,0 +1,6 @@
IO.print(0.sin) // expect: 0
IO.print((Num.pi / 2).sin) // expect: 1
// these should of course be 0, but it's not that precise
IO.print(Num.pi.sin) // expect: 1.2246467991474e-16
IO.print((2 * Num.pi).sin) // expect: -2.4492935982947e-16

View File

@ -0,0 +1,3 @@
IO.print(0.tan) // expect: 0
IO.print((Num.pi / 4).tan) // expect: 1
IO.print((-Num.pi / 4).tan) // expect: -1