mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-11 15:16:41 +08:00
Merge pull request #58 from xSavitar/cos-and-tan
Fix #56: Implement the `cos()` and `tan()` math functions
This commit is contained in:
commit
473daa4716
@ -880,6 +880,26 @@ static void stdMathSine(PKVM* vm)) {
|
|||||||
RET(VAR_NUM(sin(rad)));
|
RET(VAR_NUM(sin(rad)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PK_DOC(
|
||||||
|
"cosine(rad:num) -> num\n"
|
||||||
|
"Return the cosine value of the argument [rad] which is an angle expressed "
|
||||||
|
"in radians.",
|
||||||
|
static void stdMathCosine(PKVM* vm)) {
|
||||||
|
double rad;
|
||||||
|
if (!validateNumeric(vm, ARG(1), &rad, "Argument 1")) return;
|
||||||
|
RET(VAR_NUM(cos(rad)));
|
||||||
|
}
|
||||||
|
|
||||||
|
PK_DOC(
|
||||||
|
"tan(rad:num) -> num\n"
|
||||||
|
"Return the tangent value of the argument [rad] which is an angle expressed "
|
||||||
|
"in radians.",
|
||||||
|
static void stdMathTangent(PKVM* vm)) {
|
||||||
|
double rad;
|
||||||
|
if (!validateNumeric(vm, ARG(1), &rad, "Argument 1")) return;
|
||||||
|
RET(VAR_NUM(tan(rad)));
|
||||||
|
}
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
/* CORE INITIALIZATION */
|
/* CORE INITIALIZATION */
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
@ -964,8 +984,10 @@ void initializeCore(PKVM* vm) {
|
|||||||
moduleAddFunctionInternal(vm, math, "sign", stdMathSign, 1);
|
moduleAddFunctionInternal(vm, math, "sign", stdMathSign, 1);
|
||||||
moduleAddFunctionInternal(vm, math, "hash", stdMathHash, 1);
|
moduleAddFunctionInternal(vm, math, "hash", stdMathHash, 1);
|
||||||
moduleAddFunctionInternal(vm, math, "sin", stdMathSine, 1);
|
moduleAddFunctionInternal(vm, math, "sin", stdMathSine, 1);
|
||||||
// TODO: add - cos, tan.
|
moduleAddFunctionInternal(vm, math, "cos", stdMathCosine, 1);
|
||||||
// low priority: sinh, cosh, tanh, asin, acos, atan.
|
moduleAddFunctionInternal(vm, math, "tan", stdMathTangent, 1);
|
||||||
|
|
||||||
|
// TODO: low priority - sinh, cosh, tanh, asin, acos, atan.
|
||||||
|
|
||||||
// Note that currently it's mutable (since it's a global variable, not
|
// Note that currently it's mutable (since it's a global variable, not
|
||||||
// constant and pocketlang doesn't support constant) so the user shouldn't
|
// constant and pocketlang doesn't support constant) so the user shouldn't
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
## Core builtin functions and attribute tests.
|
## Core builtin functions and attribute tests.
|
||||||
|
|
||||||
assert(hex(12648430) == '0xc0ffee')
|
assert(hex(12648430) == '0xc0ffee')
|
||||||
@ -30,3 +29,17 @@ def fn(p1, p2, p3) end
|
|||||||
assert(fn.name == "fn")
|
assert(fn.name == "fn")
|
||||||
assert(fn.arity == 3)
|
assert(fn.arity == 3)
|
||||||
|
|
||||||
|
## Math functions
|
||||||
|
from math import PI, sin, cos, tan, abs
|
||||||
|
|
||||||
|
assert(sin(0) == 0)
|
||||||
|
assert(sin(PI/2) == 1)
|
||||||
|
|
||||||
|
threshold = 0.0000000000001
|
||||||
|
|
||||||
|
assert(abs(cos(PI/3) - 0.5) < threshold )
|
||||||
|
assert(abs(tan(PI/4) - 1.0) < threshold )
|
||||||
|
for i in 0..1000
|
||||||
|
assert(abs(sin(i) / cos(i) - tan(i)) < threshold)
|
||||||
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user