Files
cell/math/degrees.c

68 lines
2.6 KiB
C

#include "cell.h"
#include "cell_math.h"
#include <math.h>
#define DEG2RAD (3.14159265358979323846264338327950288419716939937510 / 180.0)
#define RAD2DEG (180.0 / 3.14159265358979323846264338327950288419716939937510)
static JSValue js_math_deg_arc_cosine (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
double x;
if (JS_ToFloat64 (ctx, &x, argv[0]) < 0) return JS_EXCEPTION;
return JS_NewFloat64 (ctx, acos (x) * RAD2DEG);
}
static JSValue js_math_deg_arc_sine (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
double x;
if (JS_ToFloat64 (ctx, &x, argv[0]) < 0) return JS_EXCEPTION;
return JS_NewFloat64 (ctx, asin (x) * RAD2DEG);
}
static JSValue js_math_deg_arc_tangent (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
double x;
if (JS_ToFloat64 (ctx, &x, argv[0]) < 0) return JS_EXCEPTION;
return JS_NewFloat64 (ctx, atan (x) * RAD2DEG);
}
static JSValue js_math_deg_cosine (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
double x;
if (JS_ToFloat64 (ctx, &x, argv[0]) < 0) return JS_EXCEPTION;
return JS_NewFloat64 (ctx, cos (x * DEG2RAD));
}
static JSValue js_math_deg_sine (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
double x;
if (JS_ToFloat64 (ctx, &x, argv[0]) < 0) return JS_EXCEPTION;
return JS_NewFloat64 (ctx, sin (x * DEG2RAD));
}
static JSValue js_math_deg_tangent (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
double x;
if (JS_ToFloat64 (ctx, &x, argv[0]) < 0) return JS_EXCEPTION;
return JS_NewFloat64 (ctx, tan (x * DEG2RAD));
}
static const JSCFunctionListEntry js_math_degrees_funcs[]
= { JS_CFUNC_DEF ("arc_cosine", 1, js_math_deg_arc_cosine),
JS_CFUNC_DEF ("arc_sine", 1, js_math_deg_arc_sine),
JS_CFUNC_DEF ("arc_tangent", 1, js_math_deg_arc_tangent),
JS_CFUNC_DEF ("cosine", 1, js_math_deg_cosine),
JS_CFUNC_DEF ("sine", 1, js_math_deg_sine),
JS_CFUNC_DEF ("tangent", 1, js_math_deg_tangent),
JS_CFUNC_DEF ("ln", 1, js_math_ln),
JS_CFUNC_DEF ("log", 1, js_math_log10),
JS_CFUNC_DEF ("log2", 1, js_math_log2),
JS_CFUNC_DEF ("power", 2, js_math_power),
JS_CFUNC_DEF ("root", 2, js_math_root),
JS_CFUNC_DEF ("sqrt", 1, js_math_sqrt),
JS_CFUNC_DEF ("e", 1, js_math_e) };
JSValue js_core_math_degrees_use (JSContext *ctx) {
JSGCRef obj_ref;
JS_PushGCRef (ctx, &obj_ref);
obj_ref.val = JS_NewObject (ctx);
JS_SetPropertyFunctionList (ctx, obj_ref.val, js_math_degrees_funcs, countof (js_math_degrees_funcs));
JSValue result = obj_ref.val;
JS_PopGCRef (ctx, &obj_ref);
return result;
}