diff --git a/doc/site/core/num.markdown b/doc/site/core/num.markdown index a3f93bc5..42504c4d 100644 --- a/doc/site/core/num.markdown +++ b/doc/site/core/num.markdown @@ -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 π. diff --git a/src/vm/wren_core.c b/src/vm/wren_core.c index 84083258..580587ba 100644 --- a/src/vm/wren_core.c +++ b/src/vm/wren_core.c @@ -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); diff --git a/test/core/number/acos.wren b/test/core/number/acos.wren new file mode 100644 index 00000000..54d54da9 --- /dev/null +++ b/test/core/number/acos.wren @@ -0,0 +1,3 @@ +IO.print(0.acos) // expect: 1.5707963267949 +IO.print(1.acos) // expect: 0 +IO.print((-1).acos) // expect: 3.1415926535898 diff --git a/test/core/number/asin.wren b/test/core/number/asin.wren new file mode 100644 index 00000000..e6c312af --- /dev/null +++ b/test/core/number/asin.wren @@ -0,0 +1,3 @@ +IO.print(0.asin) // expect: 0 +IO.print(1.asin) // expect: 1.5707963267949 +IO.print((-1).asin) // expect: -1.5707963267949 diff --git a/test/core/number/atan.wren b/test/core/number/atan.wren new file mode 100644 index 00000000..822315d7 --- /dev/null +++ b/test/core/number/atan.wren @@ -0,0 +1,2 @@ +IO.print(0.atan) // expect: 0 +IO.print(1.atan) // expect: 0.78539816339745 diff --git a/test/core/number/atan2.wren b/test/core/number/atan2.wren new file mode 100644 index 00000000..e1f5289f --- /dev/null +++ b/test/core/number/atan2.wren @@ -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 diff --git a/test/core/number/cos.wren b/test/core/number/cos.wren new file mode 100644 index 00000000..b7be49af --- /dev/null +++ b/test/core/number/cos.wren @@ -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 diff --git a/test/core/number/sin.wren b/test/core/number/sin.wren new file mode 100644 index 00000000..da2218e9 --- /dev/null +++ b/test/core/number/sin.wren @@ -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 diff --git a/test/core/number/tan.wren b/test/core/number/tan.wren new file mode 100644 index 00000000..7173db4b --- /dev/null +++ b/test/core/number/tan.wren @@ -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