Skip to content

Commit 7b0c6dd

Browse files
add many math functions
1 parent 6cb6913 commit 7b0c6dd

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed

cogni.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,60 @@ cog_object* fn_set() {
27422742
}
27432743
cog_modfunc fne_set = {"Set", COG_FUNC, fn_set, "Mutates a box in-place by changing the object it points to."};
27442744

2745+
#define _TRIG_FUNC(f1, f2) \
2746+
COG_ENSURE_N_ITEMS(1); \
2747+
cog_object* obj = cog_pop(); \
2748+
double n = 0; \
2749+
COG_GET_NUMBER(obj, n); \
2750+
cog_push(cog_box_float(f1(f2(n)))); \
2751+
return NULL;
2752+
2753+
double deg2rad(double deg) { return deg * M_PI / 180.; }
2754+
double rad2deg(double rad) { return rad * 180. / M_PI; }
2755+
2756+
cog_object* fn_sind() { _TRIG_FUNC(sin, deg2rad) }
2757+
cog_object* fn_cosd() { _TRIG_FUNC(cos, deg2rad) }
2758+
cog_object* fn_tand() { _TRIG_FUNC(tan, deg2rad) }
2759+
cog_object* fn_sin() { _TRIG_FUNC(sin,) }
2760+
cog_object* fn_cos() { _TRIG_FUNC(cos,) }
2761+
cog_object* fn_tan() { _TRIG_FUNC(tan,) }
2762+
cog_object* fn_exp() { _TRIG_FUNC(exp,) }
2763+
cog_object* fn_ln() { _TRIG_FUNC(log,) }
2764+
cog_object* fn_asind() { _TRIG_FUNC(rad2deg, asin) }
2765+
cog_object* fn_acosd() { _TRIG_FUNC(rad2deg, acos) }
2766+
cog_object* fn_atand() { _TRIG_FUNC(rad2deg, atan) }
2767+
cog_object* fn_asin() { _TRIG_FUNC(asin,) }
2768+
cog_object* fn_acos() { _TRIG_FUNC(acos,) }
2769+
cog_object* fn_atan() { _TRIG_FUNC(atan,) }
2770+
cog_object* fn_sinhd() { _TRIG_FUNC(sinh, deg2rad) }
2771+
cog_object* fn_coshd() { _TRIG_FUNC(cosh, deg2rad) }
2772+
cog_object* fn_tanhd() { _TRIG_FUNC(tanh, deg2rad) }
2773+
cog_object* fn_sinh() { _TRIG_FUNC(sinh,) }
2774+
cog_object* fn_cosh() { _TRIG_FUNC(cosh,) }
2775+
cog_object* fn_tanh() { _TRIG_FUNC(tanh,) }
2776+
cog_modfunc fne_sind = {"Sind", COG_FUNC, fn_sind, "Return the sine of the angle, which is expressed in degrees."};
2777+
cog_modfunc fne_cosd = {"Cosd", COG_FUNC, fn_cosd, "Return the cosine of the angle, which is expressed in degrees."};
2778+
cog_modfunc fne_tand = {"Tand", COG_FUNC, fn_tand, "Return the tangent of the angle, which is expressed in degrees."};
2779+
cog_modfunc fne_sin = {"Sin", COG_FUNC, fn_sin, "Return the sine of the angle, which is expressed in radians."};
2780+
cog_modfunc fne_cos = {"Cos", COG_FUNC, fn_cos, "Return the cosine of the angle, which is expressed in radians."};
2781+
cog_modfunc fne_tan = {"Tan", COG_FUNC, fn_tan, "Return the tangent of the angle, which is expressed in radians."};
2782+
cog_modfunc fne_exp = {"Exp", COG_FUNC, fn_exp, "Return the base-e exponential of the number."};
2783+
cog_modfunc fne_ln = {"Ln", COG_FUNC, fn_ln, "Return the natural (base-e) logarithm of the number."};
2784+
cog_modfunc fne_asind = {"Asind", COG_FUNC, fn_asind, "Return the inverse sine of the value, in degrees."};
2785+
cog_modfunc fne_acosd = {"Acosd", COG_FUNC, fn_acosd, "Return the inverse cosine of the value, in degrees."};
2786+
cog_modfunc fne_atand = {"Atand", COG_FUNC, fn_atand, "Return the inverse tangent of the value, in degrees."};
2787+
cog_modfunc fne_asin = {"Asin", COG_FUNC, fn_asin, "Return the inverse sine of the value, in radians."};
2788+
cog_modfunc fne_acos = {"Acos", COG_FUNC, fn_acos, "Return the inverse cosine of the value, in radians."};
2789+
cog_modfunc fne_atan = {"Atan", COG_FUNC, fn_atan, "Return the inverse tangent of the value, in radians."};
2790+
cog_modfunc fne_sinhd = {"Sinhd", COG_FUNC, fn_sinhd, "Return the hyperbolic sine of the angle, which is expressed in degrees."};
2791+
cog_modfunc fne_coshd = {"Coshd", COG_FUNC, fn_coshd, "Return the hyperbolic cosine of the angle, which is expressed in degrees."};
2792+
cog_modfunc fne_tanhd = {"Tanhd", COG_FUNC, fn_tanhd, "Return the hyperbolic tangent of the angle, which is expressed in degrees."};
2793+
cog_modfunc fne_sinh = {"Sinh", COG_FUNC, fn_sinh, "Return the hyperbolic sine of the angle, which is expressed in radians."};
2794+
cog_modfunc fne_cosh = {"Cosh", COG_FUNC, fn_cosh, "Return the hyperbolic cosine of the angle, which is expressed in radians."};
2795+
cog_modfunc fne_tanh = {"Tanh", COG_FUNC, fn_tanh, "Return the hyperbolic tangent of the angle, which is expressed in radians."};
2796+
2797+
cog_obj_type ot_continuation = {"Continuation", cog_walk_both, NULL};
2798+
27452799
// MARK: BUILTINS TABLES
27462800

27472801
static cog_modfunc* builtin_modfunc_table[] = {
@@ -2787,6 +2841,27 @@ static cog_modfunc* builtin_modfunc_table[] = {
27872841
&fne_round,
27882842
&fne_ceil,
27892843
&fne_abs,
2844+
// trig functions
2845+
&fne_sind,
2846+
&fne_cosd,
2847+
&fne_tand,
2848+
&fne_sin,
2849+
&fne_cos,
2850+
&fne_tan,
2851+
&fne_exp,
2852+
&fne_ln,
2853+
&fne_asind,
2854+
&fne_acosd,
2855+
&fne_atand,
2856+
&fne_asin,
2857+
&fne_acos,
2858+
&fne_atan,
2859+
&fne_sinhd,
2860+
&fne_coshd,
2861+
&fne_tanhd,
2862+
&fne_sinh,
2863+
&fne_cosh,
2864+
&fne_tanh,
27902865
// boolean functions
27912866
&fne_or,
27922867
&fne_and,
@@ -2904,6 +2979,7 @@ static cog_obj_type* builtin_types[] = {
29042979
&ot_def_or_let_special,
29052980
&ot_var,
29062981
&ot_box,
2982+
&ot_continuation,
29072983
NULL
29082984
};
29092985

cogni.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ extern cog_obj_type cog_ot_owned_pointer;
641641
*/
642642
#define COG_GET_NUMBER(obj, var) \
643643
do { \
644-
if ((obj) == NULL || ((obj)->type != &ot_int && (obj)->type != &ot_float && (obj)->type != &ot_bool)) { \
645-
COG_RETURN_ERROR(cog_sprintf("Expected a number or boolean, but got %s", (obj) ? (obj)->type->typename : "NULL")); \
644+
if ((obj) == NULL || ((obj)->type != &ot_int && (obj)->type != &ot_float)) { \
645+
COG_RETURN_ERROR(cog_sprintf("Expected a number, but got %s", (obj) ? (obj)->type->typename : "NULL")); \
646646
} \
647647
else var = (obj)->type == &ot_float ? (obj)->as_float : (obj)->as_int; \
648648
} while (0)

prelude2.cog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ Def SameLists as (
2222
) else False;
2323
);
2424
);
25+
26+
~~ easy here - change-of-base formula
27+
Def Log as (Let Base; Let X; / Ln Base Ln X);

prelude2.inc

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)