From 4d0583f62dedb37ef44cee33ce564aa478e78d19 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Mon, 13 Apr 2020 22:13:10 +0900 Subject: [PATCH 01/48] add float_gte --- include/object/floatobject.h | 2 ++ src/object/float.c | 7 +++++++ src/runtime/vm.c | 14 ++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/object/floatobject.h b/include/object/floatobject.h index 15444bca..81e303a4 100644 --- a/include/object/floatobject.h +++ b/include/object/floatobject.h @@ -14,7 +14,9 @@ typedef struct MxcFloat { MxcValue float_eq(MxcValue, MxcValue); MxcValue float_neq(MxcValue, MxcValue); MxcValue float_lt(MxcValue, MxcValue); +MxcValue float_lte(MxcValue, MxcValue); MxcValue float_gt(MxcValue, MxcValue); +MxcValue float_gte(MxcValue, MxcValue); MxcValue float_div(MxcValue, MxcValue); #define FloatAdd(l, r) (mval_float((l).fnum + (r).fnum)) diff --git a/src/object/float.c b/src/object/float.c index 77339899..5da9e431 100644 --- a/src/object/float.c +++ b/src/object/float.c @@ -48,6 +48,13 @@ MxcValue float_gt(MxcValue l, MxcValue r) { return mval_false; } +MxcValue float_gte(MxcValue l, MxcValue r) { + if(l.fnum >= r.fnum) + return mval_true; + else + return mval_false; +} + MxcValue float_div(MxcValue l, MxcValue r) { if(r.fnum == 0.0) { return mval_invalid; diff --git a/src/runtime/vm.c b/src/runtime/vm.c index 4aa1ebfe..ee81e4d3 100644 --- a/src/runtime/vm.c +++ b/src/runtime/vm.c @@ -519,6 +519,17 @@ int vm_exec(Frame *frame) { Dispatch(); } + CASE(FGTE) { + ++pc; + MxcValue r = Pop(); + MxcValue l = Top(); + SetTop(float_gte(l, r)); + + DECREF(r); + DECREF(l); + + Dispatch(); + } CASE(INC) { ++pc; MxcValue u = Pop(); @@ -809,8 +820,7 @@ int vm_exec(Frame *frame) { // TODO CASE(FLOGOR) CASE(FLOGAND) - CASE(FMOD) - CASE(FGTE) { + CASE(FMOD) { mxc_raise_err(frame, RTERR_UNIMPLEMENTED); goto exit_failure; } From e63b9365775f66e3b573201fcb6ad1f9345814db Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Mon, 13 Apr 2020 23:04:43 +0900 Subject: [PATCH 02/48] add the builtin function, toint() --- src/module/bltinmod.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/module/bltinmod.c b/src/module/bltinmod.c index 2a94f9df..70f8b5d0 100644 --- a/src/module/bltinmod.c +++ b/src/module/bltinmod.c @@ -52,6 +52,15 @@ MxcValue int_tofloat_core(Frame *f, MxcValue *sp, size_t narg) { return mval_float(fnum); } +MxcValue float_toint_core(Frame *f, MxcValue *sp, size_t narg) { + INTERN_UNUSE(f); + INTERN_UNUSE(narg); + MxcValue val = sp[0]; + int num = (int)val.fnum; + + return mval_int(num); +} + MxcValue object_id_core(Frame *f, MxcValue *sp, size_t narg) { INTERN_UNUSE(f); INTERN_UNUSE(narg); @@ -117,6 +126,7 @@ void builtin_Init() { define_cmethod(Global_Cbltins, "echo", println_core, mxcty_none, mxcty_any_vararg, NULL); define_cmethod(Global_Cbltins, "len", strlen_core, mxcty_int, mxcty_string, NULL); define_cmethod(Global_Cbltins, "tofloat", int_tofloat_core, mxcty_float, mxcty_int, NULL); + define_cmethod(Global_Cbltins, "toint", float_toint_core, mxcty_int, mxcty_float, NULL); define_cmethod(Global_Cbltins, "objectid", object_id_core, mxcty_int, mxcty_any, NULL); define_cmethod(Global_Cbltins, "exit", sys_exit_core, mxcty_none, mxcty_int, NULL); define_cmethod(Global_Cbltins, "readline", readline_core, mxcty_string, NULL); From 1a7897beaffcf97961696df3c6ca2bdc38337987 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Mon, 13 Apr 2020 23:20:35 +0900 Subject: [PATCH 03/48] add float_mod --- include/object/floatobject.h | 1 + src/compiler/operator.c | 1 + src/object/float.c | 5 +++++ src/runtime/vm.c | 14 ++++++++++++-- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/object/floatobject.h b/include/object/floatobject.h index 81e303a4..03f06cd0 100644 --- a/include/object/floatobject.h +++ b/include/object/floatobject.h @@ -18,6 +18,7 @@ MxcValue float_lte(MxcValue, MxcValue); MxcValue float_gt(MxcValue, MxcValue); MxcValue float_gte(MxcValue, MxcValue); MxcValue float_div(MxcValue, MxcValue); +MxcValue float_mod(MxcValue, MxcValue); #define FloatAdd(l, r) (mval_float((l).fnum + (r).fnum)) #define FloatSub(l, r) (mval_float((l).fnum - (r).fnum)) diff --git a/src/compiler/operator.c b/src/compiler/operator.c index 5cb7369e..1fb39085 100644 --- a/src/compiler/operator.c +++ b/src/compiler/operator.c @@ -43,6 +43,7 @@ MxcOperator opdefs_float[] = { {OPE_BINARY, BIN_SUB, mxcty_float, mxcty_float, NULL, "-"}, {OPE_BINARY, BIN_MUL, mxcty_float, mxcty_float, NULL, "*"}, {OPE_BINARY, BIN_DIV, mxcty_float, mxcty_float, NULL, "/"}, + {OPE_BINARY, BIN_MOD, mxcty_float, mxcty_float, NULL, "%"}, {OPE_BINARY, BIN_EQ, mxcty_float, mxcty_bool, NULL, "=="}, {OPE_BINARY, BIN_NEQ, mxcty_float, mxcty_bool, NULL, "!="}, {OPE_BINARY, BIN_LT, mxcty_float, mxcty_bool, NULL, "<"}, diff --git a/src/object/float.c b/src/object/float.c index 5da9e431..df740050 100644 --- a/src/object/float.c +++ b/src/object/float.c @@ -63,6 +63,11 @@ MxcValue float_div(MxcValue l, MxcValue r) { return mval_float(l.fnum / r.fnum); } +MxcValue float_mod(MxcValue l, MxcValue r) { + int n = l.fnum / r.fnum; + return mval_float(l.fnum - n * r.fnum); +} + MxcValue float_tostring(MxcValue val) { double f = val.fnum; size_t len = get_digit((int)f) + 10; diff --git a/src/runtime/vm.c b/src/runtime/vm.c index ee81e4d3..76d492ac 100644 --- a/src/runtime/vm.c +++ b/src/runtime/vm.c @@ -365,6 +365,17 @@ int vm_exec(Frame *frame) { Dispatch(); } + CASE(FMOD) { + ++pc; + MxcValue r = Pop(); + MxcValue l = Top(); + SetTop(float_mod(l, r)); + + DECREF(r); + DECREF(l); + + Dispatch(); + } CASE(LOGOR) { ++pc; MxcValue r = Pop(); @@ -819,8 +830,7 @@ int vm_exec(Frame *frame) { } // TODO CASE(FLOGOR) - CASE(FLOGAND) - CASE(FMOD) { + CASE(FLOGAND) { mxc_raise_err(frame, RTERR_UNIMPLEMENTED); goto exit_failure; } From cc20332e5dca93e3ad44c51295b9ad0f54432fba Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 00:08:17 +0900 Subject: [PATCH 04/48] add the funtions, sin() and cos() --- lib/math.mxc | 165 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 161 insertions(+), 4 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 2ed8a3b9..13b96690 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -6,6 +6,166 @@ let { // 三角関数 // sin(), cos(), tan(), asin(), acos(), atan() +// これは前方宣言が実装されるか前方宣言が要らなくなれば削除 +fn __cos(x: float): float { + if x > 0.5689773361501509 { + let { + x2 = x * x; + x4 = x2 * x2; + x6 = x2 * x4; + x8 = x2 * x6; + } + return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x6 * 0.001388888888888889 + x8 * 0.0000248015873015873 - x2 * x8 / 3628800.0; + } + else if x > 0.2809980095710868 { + let { + x2 = x * x; + x4 = x2 * x2; + x6 = x2 * x4; + } + return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x6 * 0.001388888888888889 + x2 * x6 * 0.0000248015873015873; + } + else if x > 0.0942477796076937 { + let { + x2 = x * x; + x4 = x2 * x2; + } + return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x2 * x4 * 0.001388888888888889; + } + else if x > 0.012391837689159725 { + let x2 = x * x; + return 1.0 - x2 * 0.5 + x2 * x2 * 0.041666666666666664; + } + else if x > 0.00004537856055185252 { + return 1.0 - x * x * 0.5; + } + else { + return 1.0; + } +} + +fn sin(x: float): float { + let sign = 1.0; + if x < 0.0 { + sign = -sign; + x = -x; + } + let pi2 = PI * 2.0; + if x > pi2 { + x = x % pi2; + } + if x > PI { + sign = -sign; + x = pi2 - x; + } + let piHalf = PI * 0.5; + let rounding_error = 0.0000000000000001; + if x > piHalf { + x = PI - x + rounding_error; + } + if x > PI * 0.25 { + return sign * __cos(piHalf - x + rounding_error); + } + else if x > 0.745255590601578 { + let { + x2 = x * x; + x3 = x * x2; + x5 = x2 * x3; + x7 = x2 * x5; + x9 = x2 * x7; + } + return sign * (x - x3 * 0.16666666666666666 + x5 * 0.008333333333333333 - x7 * 0.0001984126984126984 + x9 / 362880.0 - x2 * x9 / 39916800.0); + } + else if x > 0.4136430327226556 { + let { + x2 = x * x; + x3 = x * x2; + x5 = x2 * x3; + x7 = x2 * x5; + } + return sign * (x - x3 * 0.16666666666666666 + x5 * 0.008333333333333333 - x7 * 0.0001984126984126984 + x2 * x7 / 362880.0); + } + else if x > 0.17453292519943278 { + let { + x2 = x * x; + x3 = x * x2; + x5 = x2 * x3; + } + return sign * (x - x3 * 0.16666666666666666 + x5 * 0.008333333333333333 - x2 * x5 * 0.0001984126984126984); + } + else if x > 0.04014257279586953 { + let { + x2 = x * x; + x3 = x * x2; + } + return sign * (x - x3 * 0.16666666666666666 + x2 * x3 * 0.008333333333333333); + } + else if x > 0.0017976891295541577 { + let x2 = x * x; + return sign * (x - x * x2 * 0.16666666666666666); + } + else { + return sign * x; + } +} + +fn cos(x: float): float { + let sign = 1.0; + if x < 0.0 { + x = -x; + } + let pi2 = PI * 2.0; + if x > pi2 { + x = x % pi2; + } + if x > PI { + x = pi2 - x; + } + let piHalf = PI * 0.5; + let rounding_error = 0.0000000000000001; + if x > piHalf { + sign = -sign; + x = PI - x + rounding_error; + } + if x > PI * 0.25 { + return sign * sin(piHalf - x + rounding_error); + } + else if x > 0.5689773361501509 { + let { + x2 = x * x; + x4 = x2 * x2; + x6 = x2 * x4; + x8 = x2 * x6; + } + return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x6 * 0.001388888888888889 + x8 * 0.0000248015873015873 - x2 * x8 / 3628800.0); + } + else if x > 0.2809980095710868 { + let { + x2 = x * x; + x4 = x2 * x2; + x6 = x2 * x4; + } + return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x6 * 0.001388888888888889 + x2 * x6 * 0.0000248015873015873); + } + else if x > 0.0942477796076937 { + let { + x2 = x * x; + x4 = x2 * x2; + } + return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x2 * x4 * 0.001388888888888889); + } + else if x > 0.012391837689159725 { + let x2 = x * x; + return sign * (1.0 - x2 * 0.5 + x2 * x2 * 0.041666666666666664); + } + else if x > 0.00004537856055185252 { + return sign * (1.0 - x * x * 0.5); + } + else { + return sign; + } +} + // 双曲線関数 // sinh(), cosh(), tanh(), asinh(), acosh(), atanh() @@ -18,9 +178,6 @@ fn pow(x: int, n: int): int { let result = 1; while n > 1 { let { - // キャストがあれば - // n2 = (int)(n * 0.5); - // の方が早そうだけどそこは内部の実装にも依りそう n2 = n / 2; i = n - (2 * n2); } @@ -34,7 +191,7 @@ fn pow(x: int, n: int): int { } fn sqrt(x :float): float { - if x < 0.0 || x == 0.0 { + if x <= 0.0 { return 0.0; } let { From b2d510052bd79228c87ae233c98a5f7557c870b4 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 08:34:51 +0900 Subject: [PATCH 05/48] add the tan() function --- lib/math.mxc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/math.mxc b/lib/math.mxc index 13b96690..bf0f39e9 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -166,6 +166,10 @@ fn cos(x: float): float { } } +fn tan(x: float): float { + return sin(x) / cos(x); +} + // 双曲線関数 // sinh(), cosh(), tanh(), asinh(), acosh(), atanh() From 61fc39a14915418b2e1b7e47dad2f4274d9777e6 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 12:03:21 +0900 Subject: [PATCH 06/48] fix the functions, sin() and cos() --- lib/math.mxc | 167 +++++++++++++++++++++------------------------------ 1 file changed, 70 insertions(+), 97 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index bf0f39e9..5be967d1 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -8,40 +8,32 @@ let { // これは前方宣言が実装されるか前方宣言が要らなくなれば削除 fn __cos(x: float): float { - if x > 0.5689773361501509 { - let { - x2 = x * x; - x4 = x2 * x2; - x6 = x2 * x4; - x8 = x2 * x6; - } - return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x6 * 0.001388888888888889 + x8 * 0.0000248015873015873 - x2 * x8 / 3628800.0; - } - else if x > 0.2809980095710868 { - let { - x2 = x * x; - x4 = x2 * x2; - x6 = x2 * x4; - } - return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x6 * 0.001388888888888889 + x2 * x6 * 0.0000248015873015873; - } - else if x > 0.0942477796076937 { - let { - x2 = x * x; - x4 = x2 * x2; - } - return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x2 * x4 * 0.001388888888888889; - } - else if x > 0.012391837689159725 { - let x2 = x * x; - return 1.0 - x2 * 0.5 + x2 * x2 * 0.041666666666666664; + if x < 0.00004537856055185252 { + return 1.0; } - else if x > 0.00004537856055185252 { + if x < 0.012391837689159725 { return 1.0 - x * x * 0.5; } - else { - return 1.0; + let x2 = x * x; + if x < 0.0942477796076937 { + return 1.0 - x2 * 0.5 + x2 * x2 * 0.041666666666666664; } + let x4 = x2 * x2; + if(x < 0.2809980095710868) { + return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - + x2 * x4 * 0.001388888888888889; + } + let x6 = x2 * x4; + if(x < 0.5689773361501509) { + return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - + x6 * 0.001388888888888889 + + x2 * x6 * 0.0000248015873015873; + } + let x8 = x2 * x6; + return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - + x6 * 0.001388888888888889 + + x8 * 0.0000248015873015873 - + x2 * x8 / 3628800.0; } fn sin(x: float): float { @@ -66,47 +58,36 @@ fn sin(x: float): float { if x > PI * 0.25 { return sign * __cos(piHalf - x + rounding_error); } - else if x > 0.745255590601578 { - let { - x2 = x * x; - x3 = x * x2; - x5 = x2 * x3; - x7 = x2 * x5; - x9 = x2 * x7; - } - return sign * (x - x3 * 0.16666666666666666 + x5 * 0.008333333333333333 - x7 * 0.0001984126984126984 + x9 / 362880.0 - x2 * x9 / 39916800.0); - } - else if x > 0.4136430327226556 { - let { - x2 = x * x; - x3 = x * x2; - x5 = x2 * x3; - x7 = x2 * x5; - } - return sign * (x - x3 * 0.16666666666666666 + x5 * 0.008333333333333333 - x7 * 0.0001984126984126984 + x2 * x7 / 362880.0); - } - else if x > 0.17453292519943278 { - let { - x2 = x * x; - x3 = x * x2; - x5 = x2 * x3; - } - return sign * (x - x3 * 0.16666666666666666 + x5 * 0.008333333333333333 - x2 * x5 * 0.0001984126984126984); - } - else if x > 0.04014257279586953 { - let { - x2 = x * x; - x3 = x * x2; - } - return sign * (x - x3 * 0.16666666666666666 + x2 * x3 * 0.008333333333333333); + if x < 0.0017976891295541577 { + return sign * x; } - else if x > 0.0017976891295541577 { - let x2 = x * x; + let x2 = x * x; + if x < 0.0401425727958695 { return sign * (x - x * x2 * 0.16666666666666666); } - else { - return sign * x; + let x3 = x * x2; + if x < 0.17453292519943278 { + return sign * (x - x3 * 0.16666666666666666 + x2 * x3 * 0.008333333333333333); } + let x5 = x2 * x3; + if x < 0.4136430327226556 { + return sign * (x - x3 * 0.16666666666666666 + + x5 * 0.008333333333333333 - + x2 * x5 * 0.0001984126984126984); + } + let x7 = x2 * x5; + if x < 0.74525559060157 { + return sign * (x - x3 * 0.16666666666666666 + + x5 * 0.008333333333333333 - + x7 * 0.0001984126984126984 + + x2 * x7 / 362880.0); + } + let x9 = x2 * x7; + return sign * (x - x3 * 0.16666666666666666 + + x5 * 0.008333333333333333 - + x7 * 0.0001984126984126984 + + x9 / 362880.0 - + x2 * x9 / 39916800.0); } fn cos(x: float): float { @@ -130,40 +111,32 @@ fn cos(x: float): float { if x > PI * 0.25 { return sign * sin(piHalf - x + rounding_error); } - else if x > 0.5689773361501509 { - let { - x2 = x * x; - x4 = x2 * x2; - x6 = x2 * x4; - x8 = x2 * x6; - } - return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x6 * 0.001388888888888889 + x8 * 0.0000248015873015873 - x2 * x8 / 3628800.0); - } - else if x > 0.2809980095710868 { - let { - x2 = x * x; - x4 = x2 * x2; - x6 = x2 * x4; - } - return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x6 * 0.001388888888888889 + x2 * x6 * 0.0000248015873015873); - } - else if x > 0.0942477796076937 { - let { - x2 = x * x; - x4 = x2 * x2; - } - return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - x2 * x4 * 0.001388888888888889); - } - else if x > 0.012391837689159725 { - let x2 = x * x; - return sign * (1.0 - x2 * 0.5 + x2 * x2 * 0.041666666666666664); + if x < 0.00004537856055185252 { + return sign; } - else if x > 0.00004537856055185252 { + if x < 0.012391837689159725 { return sign * (1.0 - x * x * 0.5); } - else { - return sign; + let x2 = x * x; + if x < 0.0942477796076937 { + return sign * (1.0 - x2 * 0.5 + x2 * x2 * 0.041666666666666664); } + let x4 = x2 * x2; + if x < 0.2809980095710868 { + return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - + x2 * x4 * 0.001388888888888889); + } + let x6 = x2 * x4; + if x < 0.5689773361501509 { + return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - + x6 * 0.001388888888888889 + + x2 * x6 * 0.0000248015873015873); + } + let x8 = x2 * x6; + return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - + x6 * 0.001388888888888889 + + x8 * 0.0000248015873015873 - + x2 * x8 / 3628800.0); } fn tan(x: float): float { From 2caa96faf56fd4b07f9d73036ba5ddab75753aae Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 12:39:17 +0900 Subject: [PATCH 07/48] fix float_lte --- src/runtime/vm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/vm.c b/src/runtime/vm.c index 76d492ac..75bb23c5 100644 --- a/src/runtime/vm.c +++ b/src/runtime/vm.c @@ -490,7 +490,7 @@ int vm_exec(Frame *frame) { ++pc; MxcValue r = Pop(); MxcValue l = Top(); - SetTop(float_lt(l, r)); + SetTop(float_lte(l, r)); DECREF(r); DECREF(l); From d812e0fdac38eae1f65b8580137fab81746cfeff Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 12:52:46 +0900 Subject: [PATCH 08/48] add the functions, asin() and acos() --- lib/math.mxc | 352 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 321 insertions(+), 31 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 5be967d1..24dfd447 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -6,6 +6,24 @@ let { // 三角関数 // sin(), cos(), tan(), asin(), acos(), atan() +// 双曲線関数 +// sinh(), cosh(), tanh(), asinh(), acosh(), atanh() + +// 指数関数, 対数関数 +// exp(), exp2(), log(), log10(), log2() + +// 累乗, 冪根, 絶対値 +// pow(), sqrt(), cbrt(), hypot(), abs() + +// 最近傍整数 +// ceil(), floor() + +// 剰余 +// fmod() + +// 線形補間 +// lerp() + // これは前方宣言が実装されるか前方宣言が要らなくなれば削除 fn __cos(x: float): float { if x < 0.00004537856055185252 { @@ -143,30 +161,6 @@ fn tan(x: float): float { return sin(x) / cos(x); } -// 双曲線関数 -// sinh(), cosh(), tanh(), asinh(), acosh(), atanh() - -// 指数関数, 対数関数 -// exp(), exp2(), log(), log10(), log2() - -// 累乗, 冪根, 絶対値 -// pow(), sqrt(), cbrt(), hypot(), abs() -fn pow(x: int, n: int): int { - let result = 1; - while n > 1 { - let { - n2 = n / 2; - i = n - (2 * n2); - } - if i > 0 { - result = result * x; - } - x = x * x; - n = n2; - } - return result * x; -} - fn sqrt(x :float): float { if x <= 0.0 { return 0.0; @@ -187,14 +181,310 @@ fn sqrt(x :float): float { return after; } -fn abs(i: int) = if i >= 0 i else -i; +fn asin(x: float): float { + if x < 0.0 { + return -asin(-x); + } + if x > 1.0 { + assert x > 1.0; + } + if x > 0.7071067811865475 { + return PI * 0.5 - asin(sqrt(1.0 - x * x)); + } + if x < 0.0266 { + return x + x * x * x * 0.16666666666666666; + } + let x2 = x * x; + let x3 = x * x2; + if x < 0.0807 { + return x + x3 * 0.16666666666666666 + x2 * x3 * 0.075; + } + let x5 = x2 * x3; + if x < 0.1471 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x2 * x5 * 0.044642857142857144; + } + let x7 = x2 * x5; + if x < 0.214 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x2 * x7 * 0.030381944444444444; + } + let x9 = x2 * x7; + if x < 0.276 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x2 * x9 * 0.022372159090909092; + } + let x11 = x2 * x9; + if x < 0.3318 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x2 * x11 * 0.017352764423076924; + } + let x13 = x2 * x11; + if x < 0.3812 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x2 * x13 * 0.01396484375; + } + let x15 = x2 * x13; + if x < 0.4248 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x2 * x15 * 0.011551800896139705; + } + let x17 = x2 * x15; + if x < 0.4634 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x2 * x17 * 0.009761609529194078; + } + let x19 = x2 * x17; + if x < 0.4976 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x2 * x19 * 0.008390335809616815; + } + let x21 = x2 * x19; + if x < 0.528 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x2 * x21 * 0.0073125258735988454; + } + let x23 = x2 * x21; + if x < 0.5551 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x2 * x23 * 0.006447210311889649; + } + let x25 = x2 * x23; + if x < 0.5795 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x2 * x25 * 0.005740037670841924; + } + let x27 = x2 * x25; + if x < 0.6015 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x27 * 0.005740037670841924 + + x2 * x27 * 0.005153309682319905; + } + let x29 = x2 * x27; + if x < 0.6214 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x27 * 0.005740037670841924 + + x29 * 0.005153309682319905 + + x2 * x29 * 0.004660143486915096; + } + let x31 = x2 * x29; + if x < 0.6395 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x27 * 0.005740037670841924 + + x29 * 0.005153309682319905 + + x31 * 0.004660143486915096 + + x2 * x31 * 0.004240907093679363; + } + let x33 = x2 * x31; + if x < 0.656 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x27 * 0.005740037670841924 + + x29 * 0.005153309682319905 + + x31 * 0.004660143486915096 + + x33 * 0.004240907093679363 + + x2 * x33 * 0.003880964558837669; + } + let x35 = x2 * x33; + if x < 0.6711 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x27 * 0.005740037670841924 + + x29 * 0.005153309682319905 + + x31 * 0.004660143486915096 + + x33 * 0.004240907093679363 + + x35 * 0.003880964558837669 + + x2 * x35 * 0.0035692053938259347; + } + let x37 = x2 * x35; + if x < 0.685 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x27 * 0.005740037670841924 + + x29 * 0.005153309682319905 + + x31 * 0.004660143486915096 + + x33 * 0.004240907093679363 + + x35 * 0.003880964558837669 + + x37 * 0.0035692053938259347 + + x2 * x37 * 0.003297059503473485; + } + let x39 = x2 * x37; + if x < 0.6978 { + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x27 * 0.005740037670841924 + + x29 * 0.005153309682319905 + + x31 * 0.004660143486915096 + + x33 * 0.004240907093679363 + + x35 * 0.003880964558837669 + + x37 * 0.0035692053938259347 + + x39 * 0.003297059503473485 + + x2 * x39 * 0.0030578216492580306; + } + let x41 = x2 * x39; + return x + x3 * 0.16666666666666666 + x5 * 0.075 + + x7 * 0.044642857142857144 + + x9 * 0.030381944444444444 + + x11 * 0.022372159090909092 + + x13 * 0.017352764423076924 + + x15 * 0.01396484375 + + x17 * 0.011551800896139705 + + x19 * 0.009761609529194078 + + x21 * 0.008390335809616815 + + x23 * 0.0073125258735988454 + + x25 * 0.006447210311889649 + + x27 * 0.005740037670841924 + + x29 * 0.005153309682319905 + + x31 * 0.004660143486915096 + + x33 * 0.004240907093679363 + + x35 * 0.003880964558837669 + + x37 * 0.0035692053938259347 + + x39 * 0.003297059503473485 + + x41 * 0.0030578216492580306 + + x2 * x41 * 0.002846178401108942; +} -// 最近傍整数 -// ceil(), floor() +fn acos(x: float): float{ + return PI * 0.5 - asin(x); +} -// 剰余 -// fmod() +fn pow(x: int, n: int): int { + let result = 1; + while n > 1 { + let { + n2 = n / 2; + i = n - (2 * n2); + } + if i > 0 { + result = result * x; + } + x = x * x; + n = n2; + } + return result * x; +} + +fn abs(i: int) = if i >= 0 i else -i; -// 線形補間 -// lerp() fn lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From 27d59b990c49b7b9bf82c4b7360582399b3e8dc2 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 15:57:58 +0900 Subject: [PATCH 09/48] add the atan() function --- lib/math.mxc | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 24dfd447..ffb113af 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -3,23 +3,21 @@ let { E = 2.7182818284590452353; } +// *は未実装 // 三角関数 -// sin(), cos(), tan(), asin(), acos(), atan() +// sin(), cos(), tan(), asin(), acos(), *atan() // 双曲線関数 -// sinh(), cosh(), tanh(), asinh(), acosh(), atanh() +// *sinh(), *cosh(), *tanh(), *asinh(), *acosh(), *atanh() // 指数関数, 対数関数 -// exp(), exp2(), log(), log10(), log2() +// *exp(), *exp2(), *log(), *log10(), *log2() // 累乗, 冪根, 絶対値 -// pow(), sqrt(), cbrt(), hypot(), abs() +// pow(), sqrt(), *cbrt(), *hypot(), abs(), *fabs() // 最近傍整数 -// ceil(), floor() - -// 剰余 -// fmod() +// *ceil(), *floor() // 線形補間 // lerp() @@ -469,6 +467,40 @@ fn acos(x: float): float{ return PI * 0.5 - asin(x); } +fn atan(x: float): float { + if x < 0.0 { + return -atan(-x); + } + let rounding_error = 0.0000000000000001; + if x > 2.414213562373095 { + return PI * 0.5 - atan(1.0 / x); + } + if x > 0.414213562373095 { + return PI * 0.25 + atan((x - 1.0) / (x + 1.0)); + } + let { + before = x; + after = x - x * x * x * 0.3333333333333333; + sign = 1.0; + d = before - after; + x2 = x * x; + xx = x * x * x * x2; + n = 2.0; + } + while(d > 0.000000000000001) { + before = after; + after = after + sign * xx / (2.0 * n + 1.0); + xx = xx * x2; + sign = -sign; + n = n + 1.0; + d = before - after; + if(d < 0.0) { + d = -d; + } + } + return after; +} + fn pow(x: int, n: int): int { let result = 1; while n > 1 { From 73fdb1d5509cf17ab0299f6e666b4d46ae71215a Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 16:35:48 +0900 Subject: [PATCH 10/48] fix the functions. sin() and cos() --- lib/math.mxc | 94 +++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 52 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index ffb113af..a1a406aa 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -74,36 +74,29 @@ fn sin(x: float): float { if x > PI * 0.25 { return sign * __cos(piHalf - x + rounding_error); } - if x < 0.0017976891295541577 { - return sign * x; - } - let x2 = x * x; - if x < 0.0401425727958695 { - return sign * (x - x * x2 * 0.16666666666666666); - } - let x3 = x * x2; - if x < 0.17453292519943278 { - return sign * (x - x3 * 0.16666666666666666 + x2 * x3 * 0.008333333333333333); - } - let x5 = x2 * x3; - if x < 0.4136430327226556 { - return sign * (x - x3 * 0.16666666666666666 + - x5 * 0.008333333333333333 - - x2 * x5 * 0.0001984126984126984); + let { + before = x; + after = x - x * x * x * 0.16666666666666666; + d = before - after; + n = 2.0; + s = 1.0; + f = 120.0; + x2 = x * x; + xx = x * x2 * x2; } - let x7 = x2 * x5; - if x < 0.74525559060157 { - return sign * (x - x3 * 0.16666666666666666 + - x5 * 0.008333333333333333 - - x7 * 0.0001984126984126984 + - x2 * x7 / 362880.0); + while d > 0.000000001 { + before = after; + after = after + s * xx / f; + s = -s; + xx = xx * x2; + n = n + 1.0; + f = f * 2.0 * n * (2.0 * n + 1.0); + d = before - after; + if d < 0.0 { + d = -d; + } } - let x9 = x2 * x7; - return sign * (x - x3 * 0.16666666666666666 + - x5 * 0.008333333333333333 - - x7 * 0.0001984126984126984 + - x9 / 362880.0 - - x2 * x9 / 39916800.0); + return sign * after; } fn cos(x: float): float { @@ -127,32 +120,29 @@ fn cos(x: float): float { if x > PI * 0.25 { return sign * sin(piHalf - x + rounding_error); } - if x < 0.00004537856055185252 { - return sign; - } - if x < 0.012391837689159725 { - return sign * (1.0 - x * x * 0.5); - } - let x2 = x * x; - if x < 0.0942477796076937 { - return sign * (1.0 - x2 * 0.5 + x2 * x2 * 0.041666666666666664); - } - let x4 = x2 * x2; - if x < 0.2809980095710868 { - return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - - x2 * x4 * 0.001388888888888889); + let { + before = 1.0; + after = 1.0 - x * x * 0.5; + d = before - after; + n = 2.0; + s = 1.0; + f = 24.0; + x2 = x * x; + xx = x2 * x2; } - let x6 = x2 * x4; - if x < 0.5689773361501509 { - return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - - x6 * 0.001388888888888889 + - x2 * x6 * 0.0000248015873015873); + while d > 0.000000001 { + before = after; + after = after + s * xx / f; + s = -s; + xx = xx * x2; + n = n + 1.0; + f = f * 2.0 * n * (2.0 * n - 1.0); + d = before - after; + if d < 0.0 { + d = -d; + } } - let x8 = x2 * x6; - return sign * (1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - - x6 * 0.001388888888888889 + - x8 * 0.0000248015873015873 - - x2 * x8 / 3628800.0); + return sign * after; } fn tan(x: float): float { From e9c7976133b0ac2d2ed411a1a2fb8660753d416f Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 17:15:53 +0900 Subject: [PATCH 11/48] fix the functions, sin() and cos() --- lib/math.mxc | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index a1a406aa..75baab0f 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -24,32 +24,29 @@ let { // これは前方宣言が実装されるか前方宣言が要らなくなれば削除 fn __cos(x: float): float { - if x < 0.00004537856055185252 { - return 1.0; + let { + before = 1.0; + after = 1.0 - x * x * 0.5; + d = before - after; + n = 2.0; + s = 1.0; + f = 24.0; + x2 = x * x; + xx = x2 * x2; } - if x < 0.012391837689159725 { - return 1.0 - x * x * 0.5; + while d != 0.0 { + before = after; + after = after + s * xx / f; + s = -s; + xx = xx * x2; + n = n + 1.0; + f = f * 2.0 * n * (2.0 * n - 1.0); + d = before - after; + if d < 0.0 { + d = -d; + } } - let x2 = x * x; - if x < 0.0942477796076937 { - return 1.0 - x2 * 0.5 + x2 * x2 * 0.041666666666666664; - } - let x4 = x2 * x2; - if(x < 0.2809980095710868) { - return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - - x2 * x4 * 0.001388888888888889; - } - let x6 = x2 * x4; - if(x < 0.5689773361501509) { - return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - - x6 * 0.001388888888888889 + - x2 * x6 * 0.0000248015873015873; - } - let x8 = x2 * x6; - return 1.0 - x2 * 0.5 + x4 * 0.041666666666666664 - - x6 * 0.001388888888888889 + - x8 * 0.0000248015873015873 - - x2 * x8 / 3628800.0; + return after; } fn sin(x: float): float { @@ -84,7 +81,7 @@ fn sin(x: float): float { x2 = x * x; xx = x * x2 * x2; } - while d > 0.000000001 { + while d != 0.0 { before = after; after = after + s * xx / f; s = -s; @@ -130,7 +127,7 @@ fn cos(x: float): float { x2 = x * x; xx = x2 * x2; } - while d > 0.000000001 { + while d != 0.0 { before = after; after = after + s * xx / f; s = -s; From 2723ea357edc54b9ba09f9e084e982a446782653 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 22:21:53 +0900 Subject: [PATCH 12/48] add the fabs() function --- lib/math.mxc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index 75baab0f..e8229b0d 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -22,7 +22,8 @@ let { // 線形補間 // lerp() -// これは前方宣言が実装されるか前方宣言が要らなくなれば削除 +fn fabs(x: float): float = if x < 0.0 -x else x; + fn __cos(x: float): float { let { before = 1.0; From 72f9e60df94752eb24e2f327985efcdebbc2838d Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 22:23:44 +0900 Subject: [PATCH 13/48] fix the fabs() function --- lib/math.mxc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index e8229b0d..da3d2e95 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -22,7 +22,7 @@ let { // 線形補間 // lerp() -fn fabs(x: float): float = if x < 0.0 -x else x; +fn fabs(x: float): float = if x >= 0.0 x else -x; fn __cos(x: float): float { let { From 64c1e8c95b35bc80f04d96d47e924ec15efbebb5 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Tue, 14 Apr 2020 22:33:26 +0900 Subject: [PATCH 14/48] fix the functions, sin() and cos() --- lib/math.mxc | 58 +++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index da3d2e95..3989b43a 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -52,35 +52,35 @@ fn __cos(x: float): float { fn sin(x: float): float { let sign = 1.0; - if x < 0.0 { - sign = -sign; - x = -x; - } let pi2 = PI * 2.0; - if x > pi2 { + if fabs(x) > pi2 { x = x % pi2; } - if x > PI { + if fabs(x) > PI { sign = -sign; x = pi2 - x; } let piHalf = PI * 0.5; let rounding_error = 0.0000000000000001; - if x > piHalf { + if fabs(x) > piHalf { x = PI - x + rounding_error; } - if x > PI * 0.25 { + if fabs(x) > PI * 0.25 { return sign * __cos(piHalf - x + rounding_error); } let { - before = x; - after = x - x * x * x * 0.16666666666666666; + s3 = 0.16666666666666666; + s5 = 0.008333333333333333; + s7 = 0.0001984126984126984; + x2 = x * x; + before = x * (1.0 - x2 * (s3 + x2 * s5)); + after = x * (1.0 - x2 * (s3 + x2 * (s5 - x2 * s7))); d = before - after; - n = 2.0; + n = 4.0; s = 1.0; - f = 120.0; - x2 = x * x; - xx = x * x2 * x2; + f = 362880.0; + x4 = x2 * x2; + xx = x * x4 * x4; } while d != 0.0 { before = after; @@ -99,34 +99,35 @@ fn sin(x: float): float { fn cos(x: float): float { let sign = 1.0; - if x < 0.0 { - x = -x; - } let pi2 = PI * 2.0; - if x > pi2 { + if fabs(x) > pi2 { x = x % pi2; } - if x > PI { + if fabs(x) > PI { x = pi2 - x; } let piHalf = PI * 0.5; let rounding_error = 0.0000000000000001; - if x > piHalf { + if fabs(x) > piHalf { sign = -sign; x = PI - x + rounding_error; } - if x > PI * 0.25 { + if fabs(x) > PI * 0.25 { return sign * sin(piHalf - x + rounding_error); } let { - before = 1.0; - after = 1.0 - x * x * 0.5; + c2 = 0.5; + c4 = 0.041666666666666664; + c6 = 0.001388888888888889; + x2 = x * x; + before = 1.0 - x2 * (c2 + x2 * c4); + after = 1.0 - x2 * (c2 + x2 * (c4 - x2 * c6)); d = before - after; - n = 2.0; + n = 4.0; s = 1.0; - f = 24.0; - x2 = x * x; - xx = x2 * x2; + f = 40320.0; + x4 = x2 * x2; + xx = x4 * x4; } while d != 0.0 { before = after; @@ -136,9 +137,6 @@ fn cos(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n - 1.0); d = before - after; - if d < 0.0 { - d = -d; - } } return sign * after; } From 5ad0e0583fc0f2617527e30a61ee3e57b7e84992 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Wed, 15 Apr 2020 00:01:32 +0900 Subject: [PATCH 15/48] fix the asin() function --- lib/math.mxc | 344 +++++++++------------------------------------------ 1 file changed, 58 insertions(+), 286 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 3989b43a..023899f8 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -5,7 +5,7 @@ let { // *は未実装 // 三角関数 -// sin(), cos(), tan(), asin(), acos(), *atan() +// sin(), cos(), tan(), asin(), acos(), atan() // 双曲線関数 // *sinh(), *cosh(), *tanh(), *asinh(), *acosh(), *atanh() @@ -14,7 +14,7 @@ let { // *exp(), *exp2(), *log(), *log10(), *log2() // 累乗, 冪根, 絶対値 -// pow(), sqrt(), *cbrt(), *hypot(), abs(), *fabs() +// pow(), sqrt(), *cbrt(), *hypot(), abs(), fabs() // 最近傍整数 // *ceil(), *floor() @@ -26,14 +26,18 @@ fn fabs(x: float): float = if x >= 0.0 x else -x; fn __cos(x: float): float { let { - before = 1.0; - after = 1.0 - x * x * 0.5; + c2 = 0.5; + c4 = 0.041666666666666664; + c6 = 0.001388888888888889; + x2 = x * x; + before = 1.0 - x2 * (c2 - x2 * c4); + after = 1.0 - x2 * (c2 - x2 * (c4 - x2 * c6)); d = before - after; - n = 2.0; + n = 4.0; s = 1.0; - f = 24.0; - x2 = x * x; - xx = x2 * x2; + f = 40320.0; + x4 = x2 * x2; + xx = x4 * x4; } while d != 0.0 { before = after; @@ -43,9 +47,6 @@ fn __cos(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n - 1.0); d = before - after; - if d < 0.0 { - d = -d; - } } return after; } @@ -90,9 +91,6 @@ fn sin(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n + 1.0); d = before - after; - if d < 0.0 { - d = -d; - } } return sign * after; } @@ -120,8 +118,8 @@ fn cos(x: float): float { c4 = 0.041666666666666664; c6 = 0.001388888888888889; x2 = x * x; - before = 1.0 - x2 * (c2 + x2 * c4); - after = 1.0 - x2 * (c2 + x2 * (c4 - x2 * c6)); + before = 1.0 - x2 * (c2 - x2 * c4); + after = 1.0 - x2 * (c2 - x2 * (c4 - x2 * c6)); d = before - after; n = 4.0; s = 1.0; @@ -175,278 +173,52 @@ fn asin(x: float): float { if x > 0.7071067811865475 { return PI * 0.5 - asin(sqrt(1.0 - x * x)); } - if x < 0.0266 { - return x + x * x * x * 0.16666666666666666; - } - let x2 = x * x; - let x3 = x * x2; - if x < 0.0807 { - return x + x3 * 0.16666666666666666 + x2 * x3 * 0.075; - } - let x5 = x2 * x3; - if x < 0.1471 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x2 * x5 * 0.044642857142857144; - } - let x7 = x2 * x5; - if x < 0.214 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x2 * x7 * 0.030381944444444444; - } - let x9 = x2 * x7; - if x < 0.276 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x2 * x9 * 0.022372159090909092; - } - let x11 = x2 * x9; - if x < 0.3318 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x2 * x11 * 0.017352764423076924; - } - let x13 = x2 * x11; - if x < 0.3812 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x2 * x13 * 0.01396484375; - } - let x15 = x2 * x13; - if x < 0.4248 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x2 * x15 * 0.011551800896139705; - } - let x17 = x2 * x15; - if x < 0.4634 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x2 * x17 * 0.009761609529194078; - } - let x19 = x2 * x17; - if x < 0.4976 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x2 * x19 * 0.008390335809616815; - } - let x21 = x2 * x19; - if x < 0.528 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x2 * x21 * 0.0073125258735988454; - } - let x23 = x2 * x21; - if x < 0.5551 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x2 * x23 * 0.006447210311889649; - } - let x25 = x2 * x23; - if x < 0.5795 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x2 * x25 * 0.005740037670841924; - } - let x27 = x2 * x25; - if x < 0.6015 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x27 * 0.005740037670841924 + - x2 * x27 * 0.005153309682319905; - } - let x29 = x2 * x27; - if x < 0.6214 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x27 * 0.005740037670841924 + - x29 * 0.005153309682319905 + - x2 * x29 * 0.004660143486915096; - } - let x31 = x2 * x29; - if x < 0.6395 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x27 * 0.005740037670841924 + - x29 * 0.005153309682319905 + - x31 * 0.004660143486915096 + - x2 * x31 * 0.004240907093679363; - } - let x33 = x2 * x31; - if x < 0.656 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x27 * 0.005740037670841924 + - x29 * 0.005153309682319905 + - x31 * 0.004660143486915096 + - x33 * 0.004240907093679363 + - x2 * x33 * 0.003880964558837669; - } - let x35 = x2 * x33; - if x < 0.6711 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x27 * 0.005740037670841924 + - x29 * 0.005153309682319905 + - x31 * 0.004660143486915096 + - x33 * 0.004240907093679363 + - x35 * 0.003880964558837669 + - x2 * x35 * 0.0035692053938259347; - } - let x37 = x2 * x35; - if x < 0.685 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x27 * 0.005740037670841924 + - x29 * 0.005153309682319905 + - x31 * 0.004660143486915096 + - x33 * 0.004240907093679363 + - x35 * 0.003880964558837669 + - x37 * 0.0035692053938259347 + - x2 * x37 * 0.003297059503473485; + let s = [ + 1.0, + 0.16666666666666666, + 0.075, + 0.044642857142857144, + 0.030381944444444444, + 0.022372159090909092, + 0.017352764423076924, + 0.01396484375, + 0.011551800896139705, + 0.009761609529194078, + 0.008390335809616815, + 0.0073125258735988454, + 0.006447210311889649, + 0.005740037670841924, + 0.005153309682319905, + 0.004660143486915096, + 0.004240907093679363, + 0.003880964558837669, + 0.0035692053938259347, + 0.003297059503473485, + 0.0030578216492580306, + 0.002846178401108942 + ]; + let { + s1 = s[1]; + s2 = s[2]; + s3 = s[3]; + x2 = x * x; + before = x * (1.0 + x2 * (s1 + x2 * s2)); + after = x * (1.0 + x2 * (s1 + x2 * (s2 + x2 * s3))); + d = after - before; + x4 = x2 * x2; + xx = x * x4 * x4; } - let x39 = x2 * x37; - if x < 0.6978 { - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x27 * 0.005740037670841924 + - x29 * 0.005153309682319905 + - x31 * 0.004660143486915096 + - x33 * 0.004240907093679363 + - x35 * 0.003880964558837669 + - x37 * 0.0035692053938259347 + - x39 * 0.003297059503473485 + - x2 * x39 * 0.0030578216492580306; + + let n = 4; + + while d != 0.0 && n < 22 { + before = after; + after = after + xx * s[n]; + n = n + 1; + xx = xx * x2; + d = after - before; } - let x41 = x2 * x39; - return x + x3 * 0.16666666666666666 + x5 * 0.075 + - x7 * 0.044642857142857144 + - x9 * 0.030381944444444444 + - x11 * 0.022372159090909092 + - x13 * 0.017352764423076924 + - x15 * 0.01396484375 + - x17 * 0.011551800896139705 + - x19 * 0.009761609529194078 + - x21 * 0.008390335809616815 + - x23 * 0.0073125258735988454 + - x25 * 0.006447210311889649 + - x27 * 0.005740037670841924 + - x29 * 0.005153309682319905 + - x31 * 0.004660143486915096 + - x33 * 0.004240907093679363 + - x35 * 0.003880964558837669 + - x37 * 0.0035692053938259347 + - x39 * 0.003297059503473485 + - x41 * 0.0030578216492580306 + - x2 * x41 * 0.002846178401108942; + return after; } fn acos(x: float): float{ From b7f47c31c8e7f75778b4da6a9d0600df307e693c Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Wed, 15 Apr 2020 11:11:25 +0900 Subject: [PATCH 16/48] add the functions, ceil() and floor() --- lib/math.mxc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/math.mxc b/lib/math.mxc index 023899f8..46234ddc 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -277,4 +277,24 @@ fn pow(x: int, n: int): int { fn abs(i: int) = if i >= 0 i else -i; +fn ceil(x: float): int { + let n = toint(x); + if tofloat(n) < x { + return n + 1; + } + else { + return n; + } +} + +fn floor(x: float): int { + let n = toint(x); + if tofloat(n) > x { + return n - 1; + } + else { + return n; + } +} + fn lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From 88bd3a40b3eeb4e5725cfbb610636ffb9a6b165e Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 00:26:50 +0900 Subject: [PATCH 17/48] add the ldexp() function --- lib/math.mxc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 46234ddc..c056f64a 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -13,11 +13,14 @@ let { // 指数関数, 対数関数 // *exp(), *exp2(), *log(), *log10(), *log2() +// 仮数, 指数 +// ldexp(), *frexp(), *modf() + // 累乗, 冪根, 絶対値 // pow(), sqrt(), *cbrt(), *hypot(), abs(), fabs() // 最近傍整数 -// *ceil(), *floor() +// ceil(), floor() // 線形補間 // lerp() @@ -150,15 +153,10 @@ fn sqrt(x :float): float { let { before = x * 0.5; after = (before + x / before) * 0.5; - d = before - after; - } - if d < 0.0 { - d = -d; } - while d > 0.000000001 { + while after != before { before = after; after = (before + x / before) * 0.5; - d = before - after; } return after; } @@ -275,6 +273,8 @@ fn pow(x: int, n: int): int { return result * x; } +fn ldexp(x: float, exp: int): float = x * tofloat(pow(2, exp)); + fn abs(i: int) = if i >= 0 i else -i; fn ceil(x: float): int { From 755eb50c7188f79ae650aff8be39aadab0ab16e9 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 10:47:06 +0900 Subject: [PATCH 18/48] remove the errors --- lib/math.mxc | 264 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 248 insertions(+), 16 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index d0cc26f5..c056f64a 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -17,9 +17,247 @@ let { // ldexp(), *frexp(), *modf() // 累乗, 冪根, 絶対値 +// pow(), sqrt(), *cbrt(), *hypot(), abs(), fabs() -// pow(), sqrt(), cbrt(), hypot(), abs() -def pow(x: int, n: int): int { +// 最近傍整数 +// ceil(), floor() + +// 線形補間 +// lerp() + +fn fabs(x: float): float = if x >= 0.0 x else -x; + +fn __cos(x: float): float { + let { + c2 = 0.5; + c4 = 0.041666666666666664; + c6 = 0.001388888888888889; + x2 = x * x; + before = 1.0 - x2 * (c2 - x2 * c4); + after = 1.0 - x2 * (c2 - x2 * (c4 - x2 * c6)); + d = before - after; + n = 4.0; + s = 1.0; + f = 40320.0; + x4 = x2 * x2; + xx = x4 * x4; + } + while d != 0.0 { + before = after; + after = after + s * xx / f; + s = -s; + xx = xx * x2; + n = n + 1.0; + f = f * 2.0 * n * (2.0 * n - 1.0); + d = before - after; + } + return after; +} + +fn sin(x: float): float { + let sign = 1.0; + let pi2 = PI * 2.0; + if fabs(x) > pi2 { + x = x % pi2; + } + if fabs(x) > PI { + sign = -sign; + x = pi2 - x; + } + let piHalf = PI * 0.5; + let rounding_error = 0.0000000000000001; + if fabs(x) > piHalf { + x = PI - x + rounding_error; + } + if fabs(x) > PI * 0.25 { + return sign * __cos(piHalf - x + rounding_error); + } + let { + s3 = 0.16666666666666666; + s5 = 0.008333333333333333; + s7 = 0.0001984126984126984; + x2 = x * x; + before = x * (1.0 - x2 * (s3 + x2 * s5)); + after = x * (1.0 - x2 * (s3 + x2 * (s5 - x2 * s7))); + d = before - after; + n = 4.0; + s = 1.0; + f = 362880.0; + x4 = x2 * x2; + xx = x * x4 * x4; + } + while d != 0.0 { + before = after; + after = after + s * xx / f; + s = -s; + xx = xx * x2; + n = n + 1.0; + f = f * 2.0 * n * (2.0 * n + 1.0); + d = before - after; + } + return sign * after; +} + +fn cos(x: float): float { + let sign = 1.0; + let pi2 = PI * 2.0; + if fabs(x) > pi2 { + x = x % pi2; + } + if fabs(x) > PI { + x = pi2 - x; + } + let piHalf = PI * 0.5; + let rounding_error = 0.0000000000000001; + if fabs(x) > piHalf { + sign = -sign; + x = PI - x + rounding_error; + } + if fabs(x) > PI * 0.25 { + return sign * sin(piHalf - x + rounding_error); + } + let { + c2 = 0.5; + c4 = 0.041666666666666664; + c6 = 0.001388888888888889; + x2 = x * x; + before = 1.0 - x2 * (c2 - x2 * c4); + after = 1.0 - x2 * (c2 - x2 * (c4 - x2 * c6)); + d = before - after; + n = 4.0; + s = 1.0; + f = 40320.0; + x4 = x2 * x2; + xx = x4 * x4; + } + while d != 0.0 { + before = after; + after = after + s * xx / f; + s = -s; + xx = xx * x2; + n = n + 1.0; + f = f * 2.0 * n * (2.0 * n - 1.0); + d = before - after; + } + return sign * after; +} + +fn tan(x: float): float { + return sin(x) / cos(x); +} + +fn sqrt(x :float): float { + if x <= 0.0 { + return 0.0; + } + let { + before = x * 0.5; + after = (before + x / before) * 0.5; + } + while after != before { + before = after; + after = (before + x / before) * 0.5; + } + return after; +} + +fn asin(x: float): float { + if x < 0.0 { + return -asin(-x); + } + if x > 1.0 { + assert x > 1.0; + } + if x > 0.7071067811865475 { + return PI * 0.5 - asin(sqrt(1.0 - x * x)); + } + let s = [ + 1.0, + 0.16666666666666666, + 0.075, + 0.044642857142857144, + 0.030381944444444444, + 0.022372159090909092, + 0.017352764423076924, + 0.01396484375, + 0.011551800896139705, + 0.009761609529194078, + 0.008390335809616815, + 0.0073125258735988454, + 0.006447210311889649, + 0.005740037670841924, + 0.005153309682319905, + 0.004660143486915096, + 0.004240907093679363, + 0.003880964558837669, + 0.0035692053938259347, + 0.003297059503473485, + 0.0030578216492580306, + 0.002846178401108942 + ]; + let { + s1 = s[1]; + s2 = s[2]; + s3 = s[3]; + x2 = x * x; + before = x * (1.0 + x2 * (s1 + x2 * s2)); + after = x * (1.0 + x2 * (s1 + x2 * (s2 + x2 * s3))); + d = after - before; + x4 = x2 * x2; + xx = x * x4 * x4; + } + + let n = 4; + + while d != 0.0 && n < 22 { + before = after; + after = after + xx * s[n]; + n = n + 1; + xx = xx * x2; + d = after - before; + } + return after; +} + +fn acos(x: float): float{ + return PI * 0.5 - asin(x); +} + +fn atan(x: float): float { + if x < 0.0 { + return -atan(-x); + } + let rounding_error = 0.0000000000000001; + if x > 2.414213562373095 { + return PI * 0.5 - atan(1.0 / x); + } + if x > 0.414213562373095 { + return PI * 0.25 + atan((x - 1.0) / (x + 1.0)); + } + let { + before = x; + after = x - x * x * x * 0.3333333333333333; + sign = 1.0; + d = before - after; + x2 = x * x; + xx = x * x * x * x2; + n = 2.0; + } + while(d > 0.000000000000001) { + before = after; + after = after + sign * xx / (2.0 * n + 1.0); + xx = xx * x2; + sign = -sign; + n = n + 1.0; + d = before - after; + if(d < 0.0) { + d = -d; + } + } + return after; +} + +fn pow(x: int, n: int): int { let result = 1; while n > 1 { let { @@ -35,10 +273,14 @@ def pow(x: int, n: int): int { return result * x; } +fn ldexp(x: float, exp: int): float = x * tofloat(pow(2, exp)); -def sqrt(x: float): float { - if x < 0.0 || x == 0.0 { - return 0.0; +fn abs(i: int) = if i >= 0 i else -i; + +fn ceil(x: float): int { + let n = toint(x); + if tofloat(n) < x { + return n + 1; } else { return n; @@ -55,14 +297,4 @@ fn floor(x: float): int { } } -def abs(i: int) = if i >= 0 i else -i; - -// 最近傍整数 -// ceil(), floor() - -// 剰余 -// fmod() - -// 線形補間 -// lerp() -def lerp(a: float, b: float, t: float) = a + t * (b - a); +fn lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From 474ce52fa673f282941cb904b10c4738c128ea3e Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 10:51:15 +0900 Subject: [PATCH 19/48] remove the errors --- src/runtime/vm.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/runtime/vm.c b/src/runtime/vm.c index c68a7420..0ec3254d 100644 --- a/src/runtime/vm.c +++ b/src/runtime/vm.c @@ -470,6 +470,14 @@ int vm_exec(Frame *frame) { Dispatch(); } + CASE(FGTE) { + ++pc; + MxcValue r = Pop(); + MxcValue l = Top(); + SetTop(float_gte(l, r)); + + Dispatch(); + } CASE(INEG) { ++pc; MxcValue u = Top(); From 4ad41161d1e50a358feb7a37dae68754f6fd048f Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 10:58:29 +0900 Subject: [PATCH 20/48] want to remove the error;; --- src/runtime/vm.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/runtime/vm.c b/src/runtime/vm.c index 0ec3254d..ef8acf57 100644 --- a/src/runtime/vm.c +++ b/src/runtime/vm.c @@ -478,6 +478,20 @@ int vm_exec(Frame *frame) { Dispatch(); } + CASE(INC) { + ++pc; + MxcValue u = Pop(); + Push(mval_int(u.num + 1)); + + Dispatch(); + } + CASE(DEC) { + ++pc; + MxcValue u = Pop(); + Push(mval_int(u.num - 1)); + + Dispatch(); + } CASE(INEG) { ++pc; MxcValue u = Top(); From 3028781afa216365aadf94d7473216f479f9d0fa Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 11:09:04 +0900 Subject: [PATCH 21/48] change "fn" to "def" --- lib/math.mxc | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index c056f64a..6b35ec91 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -25,9 +25,9 @@ let { // 線形補間 // lerp() -fn fabs(x: float): float = if x >= 0.0 x else -x; +def fabs(x: float): float = if x >= 0.0 x else -x; -fn __cos(x: float): float { +def __cos(x: float): float { let { c2 = 0.5; c4 = 0.041666666666666664; @@ -54,7 +54,7 @@ fn __cos(x: float): float { return after; } -fn sin(x: float): float { +def sin(x: float): float { let sign = 1.0; let pi2 = PI * 2.0; if fabs(x) > pi2 { @@ -98,7 +98,7 @@ fn sin(x: float): float { return sign * after; } -fn cos(x: float): float { +def cos(x: float): float { let sign = 1.0; let pi2 = PI * 2.0; if fabs(x) > pi2 { @@ -142,11 +142,11 @@ fn cos(x: float): float { return sign * after; } -fn tan(x: float): float { +def tan(x: float): float { return sin(x) / cos(x); } -fn sqrt(x :float): float { +def sqrt(x :float): float { if x <= 0.0 { return 0.0; } @@ -161,7 +161,7 @@ fn sqrt(x :float): float { return after; } -fn asin(x: float): float { +def asin(x: float): float { if x < 0.0 { return -asin(-x); } @@ -219,11 +219,11 @@ fn asin(x: float): float { return after; } -fn acos(x: float): float{ +def acos(x: float): float{ return PI * 0.5 - asin(x); } -fn atan(x: float): float { +def atan(x: float): float { if x < 0.0 { return -atan(-x); } @@ -257,7 +257,7 @@ fn atan(x: float): float { return after; } -fn pow(x: int, n: int): int { +def pow(x: int, n: int): int { let result = 1; while n > 1 { let { @@ -273,11 +273,11 @@ fn pow(x: int, n: int): int { return result * x; } -fn ldexp(x: float, exp: int): float = x * tofloat(pow(2, exp)); +def ldexp(x: float, exp: int): float = x * tofloat(pow(2, exp)); -fn abs(i: int) = if i >= 0 i else -i; +def abs(i: int) = if i >= 0 i else -i; -fn ceil(x: float): int { +def ceil(x: float): int { let n = toint(x); if tofloat(n) < x { return n + 1; @@ -287,7 +287,7 @@ fn ceil(x: float): int { } } -fn floor(x: float): int { +def floor(x: float): int { let n = toint(x); if tofloat(n) > x { return n - 1; @@ -297,4 +297,4 @@ fn floor(x: float): int { } } -fn lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file +def lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From 06cdca3b84e74bda3b88a0aafc86b1499a97376d Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 15:12:14 +0900 Subject: [PATCH 22/48] fix the accuraacy of sin() and cos() --- lib/math.mxc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 6b35ec91..8cbe3de0 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -14,7 +14,7 @@ let { // *exp(), *exp2(), *log(), *log10(), *log2() // 仮数, 指数 -// ldexp(), *frexp(), *modf() +// ldexp(), *frexp(), *modf(), *logb() // 累乗, 冪根, 絶対値 // pow(), sqrt(), *cbrt(), *hypot(), abs(), fabs() @@ -42,7 +42,7 @@ def __cos(x: float): float { x4 = x2 * x2; xx = x4 * x4; } - while d != 0.0 { + while d > 0.0000000000000001 { before = after; after = after + s * xx / f; s = -s; @@ -50,6 +50,9 @@ def __cos(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n - 1.0); d = before - after; + if(d < 0.0) { + d = -d; + } } return after; } @@ -86,7 +89,7 @@ def sin(x: float): float { x4 = x2 * x2; xx = x * x4 * x4; } - while d != 0.0 { + while d > 0.0000000000000001 { before = after; after = after + s * xx / f; s = -s; @@ -94,6 +97,9 @@ def sin(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n + 1.0); d = before - after; + if d < 0.0 { + d = -d; + } } return sign * after; } @@ -130,7 +136,7 @@ def cos(x: float): float { x4 = x2 * x2; xx = x4 * x4; } - while d != 0.0 { + while d > 0.0000000000000001 { before = after; after = after + s * xx / f; s = -s; @@ -138,6 +144,9 @@ def cos(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n - 1.0); d = before - after; + if d < 0.0 { + d = -d; + } } return sign * after; } @@ -243,7 +252,7 @@ def atan(x: float): float { xx = x * x * x * x2; n = 2.0; } - while(d > 0.000000000000001) { + while(d > 0.0000000000000001) { before = after; after = after + sign * xx / (2.0 * n + 1.0); xx = xx * x2; From af371a7fc0769765227c7f4d4c419e3cd92776f4 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 16:26:42 +0900 Subject: [PATCH 23/48] add the log10() fuunction --- lib/math.mxc | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index 8cbe3de0..abd4ee99 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -11,7 +11,7 @@ let { // *sinh(), *cosh(), *tanh(), *asinh(), *acosh(), *atanh() // 指数関数, 対数関数 -// *exp(), *exp2(), *log(), *log10(), *log2() +// *exp(), *exp2(), *log(), log10(), *log2() // 仮数, 指数 // ldexp(), *frexp(), *modf(), *logb() @@ -282,6 +282,35 @@ def pow(x: int, n: int): int { return result * x; } +def log10(x: float): float { + if x <= 0.0 { + return 0.0; + } + let n = 0.0; + while x < 1.0 { + x = x * 10.0; + n = n - 1.0; + } + while x >= 2.0 { + x = x * 0.1; + n = n + 1.0; + } + if x == 1.0 { + return n; + } + let retval = n; + let add = 0.5; + while add > 0.0000000000000001 { + x = x * x; + if x >= 10.0 { + retval = retval + add; + x = x * 0.1; + } + add = add * 0.5; + } + return retval; +} + def ldexp(x: float, exp: int): float = x * tofloat(pow(2, exp)); def abs(i: int) = if i >= 0 i else -i; From f6fbbbb605e0ead624b8fd83f111cb9905c511d8 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 16:29:07 +0900 Subject: [PATCH 24/48] add the functions, log() and log2() --- lib/math.mxc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index abd4ee99..8dfa8f98 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -11,7 +11,7 @@ let { // *sinh(), *cosh(), *tanh(), *asinh(), *acosh(), *atanh() // 指数関数, 対数関数 -// *exp(), *exp2(), *log(), log10(), *log2() +// *exp(), *exp2(), log(), log10(), log2() // 仮数, 指数 // ldexp(), *frexp(), *modf(), *logb() @@ -311,6 +311,14 @@ def log10(x: float): float { return retval; } +def log2(x: float): float { + return 3.321928094887362 * log10(x); +} + +def log(x: float): float { + return 2.302585092994046 * log10(x); +} + def ldexp(x: float, exp: int): float = x * tofloat(pow(2, exp)); def abs(i: int) = if i >= 0 i else -i; From 18f79e6d90882d53dd091ce66cb82856f480de32 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 16:35:29 +0900 Subject: [PATCH 25/48] add the logb() function --- lib/math.mxc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index 8dfa8f98..824b1ba2 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -14,7 +14,7 @@ let { // *exp(), *exp2(), log(), log10(), log2() // 仮数, 指数 -// ldexp(), *frexp(), *modf(), *logb() +// ldexp(), *frexp(), *modf(), logb() // 累乗, 冪根, 絶対値 // pow(), sqrt(), *cbrt(), *hypot(), abs(), fabs() @@ -343,4 +343,8 @@ def floor(x: float): int { } } +def logb(x: float): int { + return floor(log2(x)); +} + def lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From 7bb7b1002b91a23ab472a4ac988583bbeb583781 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Thu, 16 Apr 2020 18:17:03 +0900 Subject: [PATCH 26/48] add the round() function --- lib/math.mxc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index 824b1ba2..f561fb9b 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -20,7 +20,7 @@ let { // pow(), sqrt(), *cbrt(), *hypot(), abs(), fabs() // 最近傍整数 -// ceil(), floor() +// ceil(), floor(), round() // 線形補間 // lerp() @@ -343,6 +343,10 @@ def floor(x: float): int { } } +def round(x: float): int { + return floor(x + 0.5); +} + def logb(x: float): int { return floor(log2(x)); } From ceffa9361de2c6d0ffa83e6b4596120da5c18012 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 00:14:11 +0900 Subject: [PATCH 27/48] add the exp() function --- lib/math.mxc | 138 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 112 insertions(+), 26 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index f561fb9b..11e60cc3 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -27,6 +27,32 @@ let { def fabs(x: float): float = if x >= 0.0 x else -x; +def abs(i: int) = if i >= 0 i else -i; + +def ceil(x: float): int { + let n = toint(x); + if tofloat(n) < x { + return n + 1; + } + else { + return n; + } +} + +def floor(x: float): int { + let n = toint(x); + if tofloat(n) > x { + return n - 1; + } + else { + return n; + } +} + +def round(x: float): int { + return floor(x + 0.5); +} + def __cos(x: float): float { let { c2 = 0.5; @@ -266,6 +292,92 @@ def atan(x: float): float { return after; } +def exp(x: float): float { + let coeff = [ + 1.0, + 0.5, + 0.1666666666666666574148081281236954964697360992431640625, + 0.041666666666666664353702032030923874117434024810791015625, + 0.00833333333333333321768510160154619370587170124053955078125, + 0.00138888888888888894189432843262466121814213693141937255859375, + 0.0001984126984126984125263171154784913596813566982746124267578125, + 0.0000248015873015873015657896394348114199601695872843265533447265625, + 0.000002755731922398589251095059327045788677423843182623386383056640625, + 0.000000275573192239858882757858569989561914326259284280240535736083984375, + 0.0000000250521083854417202238661793213536643776251366944052278995513916015625, + 0.000000002087675698786810018655514943446138698135428057867102324962615966796875, + 0.0000000001605904383682161334086291829494519585452838583705670316703617572784423828125, + 0.00000000001147074559772972450729657045037464453175746204038887299248017370700836181640625 + ]; + let s = round(x); + let t = x - tofloat(s); + let es = [ + 1.0, + 2.718281828459045090795598298427648842334747314453125, + 7.3890560989306504069418224389664828777313232421875, + 20.08553692318766792368478490971028804779052734375, + 54.59815003314423620395245961844921112060546875, + 148.413159102576599934764089994132518768310546875, + 403.42879349273511024875915609300136566162109375, + 1096.63315842845850056619383394718170166015625, + 2980.9579870417283018468879163265228271484375, + 8103.083927575384223018772900104522705078125, + 22026.465794806717894971370697021484375 + ]; + let ess = [ + 1.0, + 0.367879441171442334024277442949824035167694091796875, + 0.13533528323661270231781372785917483270168304443359375, + 0.049787068367863944462481384789498406462371349334716796875, + 0.0183156388887341821380960737997156684286892414093017578125, + 0.00673794699908546700084510661099557182751595973968505859375, + 0.002478752176666358490730868169293898972682654857635498046875, + 0.000911881965554516243747940063002488386700861155986785888671875, + 0.0003354626279025118532235716362066568763111717998981475830078125, + 0.00012340980408667953410924156276706753487815149128437042236328125 + ]; + let exp_s = 1.0; + let u = 0; + if s >= 0 { + u = s / 10; + while(u > 0) { + exp_s = exp_s * es[10]; + u = u - 1; + } + exp_s = exp_s * es[s % 10]; + } + else { + s = -s; + u = s / 10; + while(u > 0) { + exp_s = exp_s * ess[10]; + u = u - 1; + } + exp_s = exp_s * ess[s % 10]; + } + let exp_t = 0.0; + let t2 = t * t; + let t4 = t2 * t2; + let t8 = t4 * t4; + exp_t = exp_t + t8 * t4 * t2 * coeff[13]; + exp_t = exp_t + t8 * t4 * t * coeff[12]; + exp_t = exp_t + t8 * t4 * coeff[11]; + exp_t = exp_t + t8 * t2 * t * coeff[10]; + exp_t = exp_t + t8 * t2 * coeff[9]; + exp_t = exp_t + t8 * t * coeff[8]; + exp_t = exp_t + t8 * coeff[7]; + exp_t = exp_t + t4 * t2 * t * coeff[6]; + exp_t = exp_t + t4 * t2 * coeff[5]; + exp_t = exp_t + t4 * t * coeff[4]; + exp_t = exp_t + t4 * coeff[3]; + exp_t = exp_t + t2 * t * coeff[2]; + exp_t = exp_t + t2 * coeff[1]; + exp_t = exp_t + t * coeff[0]; + exp_t = exp_t + 1.0; + + return exp_s * exp_t; +} + def pow(x: int, n: int): int { let result = 1; while n > 1 { @@ -321,32 +433,6 @@ def log(x: float): float { def ldexp(x: float, exp: int): float = x * tofloat(pow(2, exp)); -def abs(i: int) = if i >= 0 i else -i; - -def ceil(x: float): int { - let n = toint(x); - if tofloat(n) < x { - return n + 1; - } - else { - return n; - } -} - -def floor(x: float): int { - let n = toint(x); - if tofloat(n) > x { - return n - 1; - } - else { - return n; - } -} - -def round(x: float): int { - return floor(x + 0.5); -} - def logb(x: float): int { return floor(log2(x)); } From 9d56bfc02dfc78e604df92f0a2f2d15ff88253f2 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 00:40:07 +0900 Subject: [PATCH 28/48] fix the log10() function --- lib/math.mxc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index 11e60cc3..9389a9ce 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -403,7 +403,7 @@ def log10(x: float): float { x = x * 10.0; n = n - 1.0; } - while x >= 2.0 { + while x >= 10.0 { x = x * 0.1; n = n + 1.0; } From 1fdf50331701345ffce018bbe47a17b31518d774 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 01:11:00 +0900 Subject: [PATCH 29/48] fix the pow() function --- lib/math.mxc | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 9389a9ce..e0baabcf 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -378,22 +378,6 @@ def exp(x: float): float { return exp_s * exp_t; } -def pow(x: int, n: int): int { - let result = 1; - while n > 1 { - let { - n2 = n / 2; - i = n - (2 * n2); - } - if i > 0 { - result = result * x; - } - x = x * x; - n = n2; - } - return result * x; -} - def log10(x: float): float { if x <= 0.0 { return 0.0; @@ -431,7 +415,29 @@ def log(x: float): float { return 2.302585092994046 * log10(x); } -def ldexp(x: float, exp: int): float = x * tofloat(pow(2, exp)); +def pow(x: float, y: float): float { + if x > 0.0 { + return exp(y * log(x)); + } + else if x == 0.0 { + return 0.0; + } + else { + if y == tofloat(floor(y)) { + if floor(y) % 2 == 0 { + return exp(y * log(-x)); + } + else { + return -exp(y * log(-x)); + } + } + else { + return 0.0; + } + } +} + +def ldexp(x: float, exp: int): float = x * pow(2.0, tofloat(exp)); def logb(x: float): int { return floor(log2(x)); From bee6abe0016926115ced5fef12ef80c62204bb65 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 10:11:58 +0900 Subject: [PATCH 30/48] fix the logb() function --- lib/math.mxc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index e0baabcf..ef198571 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -439,8 +439,6 @@ def pow(x: float, y: float): float { def ldexp(x: float, exp: int): float = x * pow(2.0, tofloat(exp)); -def logb(x: float): int { - return floor(log2(x)); -} +def logb(x: float): int = floor(log2(x)); def lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From e6e712e41023bdb57c8f1753663b1d2b58001d76 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 12:59:52 +0900 Subject: [PATCH 31/48] fix the functions, ceil(), floor() and logb() --- lib/math.mxc | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index ef198571..79dcb4ee 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -29,25 +29,9 @@ def fabs(x: float): float = if x >= 0.0 x else -x; def abs(i: int) = if i >= 0 i else -i; -def ceil(x: float): int { - let n = toint(x); - if tofloat(n) < x { - return n + 1; - } - else { - return n; - } -} +def ceil(x: float): int = if x <= 0.0 toint(x) else -toint(-x - 1.0); -def floor(x: float): int { - let n = toint(x); - if tofloat(n) > x { - return n - 1; - } - else { - return n; - } -} +def floor(x: float): int = if x >= 0.0 toint(x) else -toint(-x + 1.0); def round(x: float): int { return floor(x + 0.5); @@ -439,6 +423,6 @@ def pow(x: float, y: float): float { def ldexp(x: float, exp: int): float = x * pow(2.0, tofloat(exp)); -def logb(x: float): int = floor(log2(x)); +def logb(x: float): int = if x == 0.0 0 else floor(log2(x)); def lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From 95d3bffde62b1a7201c259c372e159a7f828fee7 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 13:02:09 +0900 Subject: [PATCH 32/48] fix fabs(), ceil(), floor() and round() --- lib/math.mxc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 79dcb4ee..9c4892ca 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -25,17 +25,15 @@ let { // 線形補間 // lerp() -def fabs(x: float): float = if x >= 0.0 x else -x; +def fabs(x: float) = if x >= 0.0 x else -x; def abs(i: int) = if i >= 0 i else -i; -def ceil(x: float): int = if x <= 0.0 toint(x) else -toint(-x - 1.0); +def ceil(x: float) = if x <= 0.0 toint(x) else -toint(-x - 1.0); -def floor(x: float): int = if x >= 0.0 toint(x) else -toint(-x + 1.0); +def floor(x: float) = if x >= 0.0 toint(x) else -toint(-x + 1.0); -def round(x: float): int { - return floor(x + 0.5); -} +def round(x: float) = floor(x + 0.5); def __cos(x: float): float { let { From d89d52f79e70529eb1d71db560ec31ec930c554e Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 14:02:59 +0900 Subject: [PATCH 33/48] change the name of constant values --- lib/math.mxc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 9c4892ca..2a916c15 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -1,6 +1,6 @@ let { - PI = 3.1415926535897932385; - E = 2.7182818284590452353; + pi = 3.1415926535897932385; + e = 2.7182818284590452353; } // *は未実装 From 8e5a21e8a4ca9b354f135272532e647331e3a865 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 14:40:39 +0900 Subject: [PATCH 34/48] change the name of constant values --- lib/math.mxc | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 2a916c15..c3c02dd3 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -11,7 +11,7 @@ let { // *sinh(), *cosh(), *tanh(), *asinh(), *acosh(), *atanh() // 指数関数, 対数関数 -// *exp(), *exp2(), log(), log10(), log2() +// exp(), *exp2(), log(), log10(), log2() // 仮数, 指数 // ldexp(), *frexp(), *modf(), logb() @@ -67,20 +67,20 @@ def __cos(x: float): float { def sin(x: float): float { let sign = 1.0; - let pi2 = PI * 2.0; + let pi2 = pi * 2.0; if fabs(x) > pi2 { x = x % pi2; } - if fabs(x) > PI { + if fabs(x) > pi { sign = -sign; x = pi2 - x; } - let piHalf = PI * 0.5; + let piHalf = pi * 0.5; let rounding_error = 0.0000000000000001; if fabs(x) > piHalf { - x = PI - x + rounding_error; + x = pi - x + rounding_error; } - if fabs(x) > PI * 0.25 { + if fabs(x) > pi * 0.25 { return sign * __cos(piHalf - x + rounding_error); } let { @@ -114,20 +114,20 @@ def sin(x: float): float { def cos(x: float): float { let sign = 1.0; - let pi2 = PI * 2.0; + let pi2 = pi * 2.0; if fabs(x) > pi2 { x = x % pi2; } - if fabs(x) > PI { + if fabs(x) > pi { x = pi2 - x; } - let piHalf = PI * 0.5; + let piHalf = pi * 0.5; let rounding_error = 0.0000000000000001; if fabs(x) > piHalf { sign = -sign; - x = PI - x + rounding_error; + x = pi - x + rounding_error; } - if fabs(x) > PI * 0.25 { + if fabs(x) > pi * 0.25 { return sign * sin(piHalf - x + rounding_error); } let { @@ -186,7 +186,7 @@ def asin(x: float): float { assert x > 1.0; } if x > 0.7071067811865475 { - return PI * 0.5 - asin(sqrt(1.0 - x * x)); + return pi * 0.5 - asin(sqrt(1.0 - x * x)); } let s = [ 1.0, @@ -237,7 +237,7 @@ def asin(x: float): float { } def acos(x: float): float{ - return PI * 0.5 - asin(x); + return pi * 0.5 - asin(x); } def atan(x: float): float { @@ -246,10 +246,10 @@ def atan(x: float): float { } let rounding_error = 0.0000000000000001; if x > 2.414213562373095 { - return PI * 0.5 - atan(1.0 / x); + return pi * 0.5 - atan(1.0 / x); } if x > 0.414213562373095 { - return PI * 0.25 + atan((x - 1.0) / (x + 1.0)); + return pi * 0.25 + atan((x - 1.0) / (x + 1.0)); } let { before = x; From be002ff3ce408956c094c45e36f61df656647dc7 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 15:49:56 +0900 Subject: [PATCH 35/48] add the exp2() function --- lib/math.mxc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/math.mxc b/lib/math.mxc index c3c02dd3..a93e0478 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -419,6 +419,8 @@ def pow(x: float, y: float): float { } } +def exp2(x: float) = pow(2.0, x); + def ldexp(x: float, exp: int): float = x * pow(2.0, tofloat(exp)); def logb(x: float): int = if x == 0.0 0 else floor(log2(x)); From b9d958bec436dd66df3294e9b1ff5520cb9890c1 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 18:31:57 +0900 Subject: [PATCH 36/48] fix the sqrt() function --- lib/math.mxc | 225 ++++++++++++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 112 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index a93e0478..7ba8cedd 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -11,7 +11,7 @@ let { // *sinh(), *cosh(), *tanh(), *asinh(), *acosh(), *atanh() // 指数関数, 対数関数 -// exp(), *exp2(), log(), log10(), log2() +// exp(), exp2(), log(), log10(), log2() // 仮数, 指数 // ldexp(), *frexp(), *modf(), logb() @@ -163,117 +163,6 @@ def tan(x: float): float { return sin(x) / cos(x); } -def sqrt(x :float): float { - if x <= 0.0 { - return 0.0; - } - let { - before = x * 0.5; - after = (before + x / before) * 0.5; - } - while after != before { - before = after; - after = (before + x / before) * 0.5; - } - return after; -} - -def asin(x: float): float { - if x < 0.0 { - return -asin(-x); - } - if x > 1.0 { - assert x > 1.0; - } - if x > 0.7071067811865475 { - return pi * 0.5 - asin(sqrt(1.0 - x * x)); - } - let s = [ - 1.0, - 0.16666666666666666, - 0.075, - 0.044642857142857144, - 0.030381944444444444, - 0.022372159090909092, - 0.017352764423076924, - 0.01396484375, - 0.011551800896139705, - 0.009761609529194078, - 0.008390335809616815, - 0.0073125258735988454, - 0.006447210311889649, - 0.005740037670841924, - 0.005153309682319905, - 0.004660143486915096, - 0.004240907093679363, - 0.003880964558837669, - 0.0035692053938259347, - 0.003297059503473485, - 0.0030578216492580306, - 0.002846178401108942 - ]; - let { - s1 = s[1]; - s2 = s[2]; - s3 = s[3]; - x2 = x * x; - before = x * (1.0 + x2 * (s1 + x2 * s2)); - after = x * (1.0 + x2 * (s1 + x2 * (s2 + x2 * s3))); - d = after - before; - x4 = x2 * x2; - xx = x * x4 * x4; - } - - let n = 4; - - while d != 0.0 && n < 22 { - before = after; - after = after + xx * s[n]; - n = n + 1; - xx = xx * x2; - d = after - before; - } - return after; -} - -def acos(x: float): float{ - return pi * 0.5 - asin(x); -} - -def atan(x: float): float { - if x < 0.0 { - return -atan(-x); - } - let rounding_error = 0.0000000000000001; - if x > 2.414213562373095 { - return pi * 0.5 - atan(1.0 / x); - } - if x > 0.414213562373095 { - return pi * 0.25 + atan((x - 1.0) / (x + 1.0)); - } - let { - before = x; - after = x - x * x * x * 0.3333333333333333; - sign = 1.0; - d = before - after; - x2 = x * x; - xx = x * x * x * x2; - n = 2.0; - } - while(d > 0.0000000000000001) { - before = after; - after = after + sign * xx / (2.0 * n + 1.0); - xx = xx * x2; - sign = -sign; - n = n + 1.0; - d = before - after; - if(d < 0.0) { - d = -d; - } - } - return after; -} - def exp(x: float): float { let coeff = [ 1.0, @@ -425,4 +314,116 @@ def ldexp(x: float, exp: int): float = x * pow(2.0, tofloat(exp)); def logb(x: float): int = if x == 0.0 0 else floor(log2(x)); +def sqrt(x :float): float { + if x <= 0.0 { + return 0.0; + } + let e = logb(x) + 1; + let a = pow(2.0, -tofloat(e) * 0.5); + let error = 1.0; + let h = 1.0 - x * a * a; + while fabs(h) < fabs(error) { + a = a * (1.0 + h * (0.5 + h * 0.375)); + error = h; + h = 1.0 - x * a * a; + } + return a * x; +} + +def asin(x: float): float { + if x < 0.0 { + return -asin(-x); + } + if x > 1.0 { + assert x > 1.0; + } + if x > 0.7071067811865475 { + return pi * 0.5 - asin(sqrt(1.0 - x * x)); + } + let s = [ + 1.0, + 0.16666666666666666, + 0.075, + 0.044642857142857144, + 0.030381944444444444, + 0.022372159090909092, + 0.017352764423076924, + 0.01396484375, + 0.011551800896139705, + 0.009761609529194078, + 0.008390335809616815, + 0.0073125258735988454, + 0.006447210311889649, + 0.005740037670841924, + 0.005153309682319905, + 0.004660143486915096, + 0.004240907093679363, + 0.003880964558837669, + 0.0035692053938259347, + 0.003297059503473485, + 0.0030578216492580306, + 0.002846178401108942 + ]; + let { + s1 = s[1]; + s2 = s[2]; + s3 = s[3]; + x2 = x * x; + before = x * (1.0 + x2 * (s1 + x2 * s2)); + after = x * (1.0 + x2 * (s1 + x2 * (s2 + x2 * s3))); + d = after - before; + x4 = x2 * x2; + xx = x * x4 * x4; + } + + let n = 4; + + while d != 0.0 && n < 22 { + before = after; + after = after + xx * s[n]; + n = n + 1; + xx = xx * x2; + d = after - before; + } + return after; +} + +def acos(x: float): float{ + return pi * 0.5 - asin(x); +} + +def atan(x: float): float { + if x < 0.0 { + return -atan(-x); + } + let rounding_error = 0.0000000000000001; + if x > 2.414213562373095 { + return pi * 0.5 - atan(1.0 / x); + } + if x > 0.414213562373095 { + return pi * 0.25 + atan((x - 1.0) / (x + 1.0)); + } + let { + before = x; + after = x - x * x * x * 0.3333333333333333; + sign = 1.0; + d = before - after; + x2 = x * x; + xx = x * x * x * x2; + n = 2.0; + } + while(d > 0.0000000000000001) { + before = after; + after = after + sign * xx / (2.0 * n + 1.0); + xx = xx * x2; + sign = -sign; + n = n + 1.0; + d = before - after; + if(d < 0.0) { + d = -d; + } + } + return after; +} + def lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From ba5b85e12eb8ef7cee3ce19aa16a75b8bee15798 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 19:46:32 +0900 Subject: [PATCH 37/48] add the cbrt() function --- lib/math.mxc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/math.mxc b/lib/math.mxc index 7ba8cedd..d9b936f2 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -330,6 +330,32 @@ def sqrt(x :float): float { return a * x; } +def cbrt(x: float): float { + if x <= 0.0 { + return 0.0; + } + let e = logb(x) + 1; + let a = pow(2.0, -tofloat(e) * 0.5); + let error = 1.0; + let h = 1.0 - x * a * a * a; + while fabs(h) < fabs(error) { + a = a * (1.0 + h * (0.33333333333333333333 + h * 0.22222222222222222222)); + error = h; + h = 1.0 - x * a * a * a; + } + x = x * a; + e = logb(x) + 1; + a = pow(2.0, -tofloat(e) * 0.5); + error = 1.0; + h = 1.0 - x * a * a; + while fabs(h) < fabs(error) { + a = a * (1.0 + h * (0.5 + h * 0.375)); + error = h; + h = 1.0 - x * a * a; + } + return a * x; +} + def asin(x: float): float { if x < 0.0 { return -asin(-x); From 4d857d1983755a9277a47a21f4dbcc4c580da282 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 21:34:20 +0900 Subject: [PATCH 38/48] add the functions, min() and max() --- lib/math.mxc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index d9b936f2..afc5a66c 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -17,7 +17,7 @@ let { // ldexp(), *frexp(), *modf(), logb() // 累乗, 冪根, 絶対値 -// pow(), sqrt(), *cbrt(), *hypot(), abs(), fabs() +// pow(), sqrt(), cbrt(), *hypot(), abs(), fabs() // 最近傍整数 // ceil(), floor(), round() @@ -25,6 +25,9 @@ let { // 線形補間 // lerp() +// そのうちalgorithmライブラリに移行しそう +// min(), max() + def fabs(x: float) = if x >= 0.0 x else -x; def abs(i: int) = if i >= 0 i else -i; @@ -35,6 +38,10 @@ def floor(x: float) = if x >= 0.0 toint(x) else -toint(-x + 1.0); def round(x: float) = floor(x + 0.5); +def max(x: float, y: float) = if x > y x else y; + +def min(x: float, y: float) = if x < y x else y; + def __cos(x: float): float { let { c2 = 0.5; @@ -310,9 +317,9 @@ def pow(x: float, y: float): float { def exp2(x: float) = pow(2.0, x); -def ldexp(x: float, exp: int): float = x * pow(2.0, tofloat(exp)); +def ldexp(x: float, exp: int) = x * pow(2.0, tofloat(exp)); -def logb(x: float): int = if x == 0.0 0 else floor(log2(x)); +def logb(x: float) = if x == 0.0 0 else floor(log2(x)); def sqrt(x :float): float { if x <= 0.0 { From 9fb121255d9580e3552e52307862f276fcb1e3f9 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Fri, 17 Apr 2020 22:24:08 +0900 Subject: [PATCH 39/48] add the hypot() function --- lib/math.mxc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/math.mxc b/lib/math.mxc index afc5a66c..73ded3fe 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -363,6 +363,12 @@ def cbrt(x: float): float { return a * x; } +def hypot(x: float, y: float): float { + let u = max(fabs(x), fabs(y)); + let v = min(fabs(x), fabs(y)); + return fabs(u) * sqrt(1.0 + (v / u) * (v / u)); +} + def asin(x: float): float { if x < 0.0 { return -asin(-x); From 61483ed0392c0fdef133d788dc37320f08ff3ebd Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 18 Apr 2020 00:24:57 +0900 Subject: [PATCH 40/48] fix the exp() function --- lib/math.mxc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 73ded3fe..f0ac7c49 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -17,7 +17,7 @@ let { // ldexp(), *frexp(), *modf(), logb() // 累乗, 冪根, 絶対値 -// pow(), sqrt(), cbrt(), *hypot(), abs(), fabs() +// pow(), sqrt(), cbrt(), hypot(), abs(), fabs() // 最近傍整数 // ceil(), floor(), round() @@ -212,7 +212,8 @@ def exp(x: float): float { 0.002478752176666358490730868169293898972682654857635498046875, 0.000911881965554516243747940063002488386700861155986785888671875, 0.0003354626279025118532235716362066568763111717998981475830078125, - 0.00012340980408667953410924156276706753487815149128437042236328125 + 0.00012340980408667953410924156276706753487815149128437042236328125, + 0.000045399929762484847396881992853678866595146246254444122314453125 ]; let exp_s = 1.0; let u = 0; From d4f0429f59a2aa075d554cc8666cd646be9aae21 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 18 Apr 2020 00:30:50 +0900 Subject: [PATCH 41/48] add the sinh() function --- lib/math.mxc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/math.mxc b/lib/math.mxc index f0ac7c49..4fc6fea1 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -257,6 +257,8 @@ def exp(x: float): float { return exp_s * exp_t; } +def sinh(x: float) = (exp(x) - exp(-x)) * 0.5; + def log10(x: float): float { if x <= 0.0 { return 0.0; From 1a07b70b17ff9d688d5ab060292398a5fe28fa65 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 18 Apr 2020 00:37:26 +0900 Subject: [PATCH 42/48] add the cosh() function --- lib/math.mxc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index 4fc6fea1..d752bf14 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -8,7 +8,7 @@ let { // sin(), cos(), tan(), asin(), acos(), atan() // 双曲線関数 -// *sinh(), *cosh(), *tanh(), *asinh(), *acosh(), *atanh() +// sinh(), cosh(), *tanh(), *asinh(), *acosh(), *atanh() // 指数関数, 対数関数 // exp(), exp2(), log(), log10(), log2() @@ -259,6 +259,8 @@ def exp(x: float): float { def sinh(x: float) = (exp(x) - exp(-x)) * 0.5; +def cosh(x: float) = (exp(x) + exp(-x)) * 0.5; + def log10(x: float): float { if x <= 0.0 { return 0.0; From 77d6be6a2950148ff657d6dceb8aa701913afb2d Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 18 Apr 2020 00:43:16 +0900 Subject: [PATCH 43/48] add the tanh() function --- lib/math.mxc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/math.mxc b/lib/math.mxc index d752bf14..eef777df 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -261,6 +261,8 @@ def sinh(x: float) = (exp(x) - exp(-x)) * 0.5; def cosh(x: float) = (exp(x) + exp(-x)) * 0.5; +def tanh(x: float) = sinh(x) / cosh(x); + def log10(x: float): float { if x <= 0.0 { return 0.0; From 92d7a49d40011b9896580f2e6708782eeec11a0f Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 18 Apr 2020 00:46:29 +0900 Subject: [PATCH 44/48] add the asinh() function --- lib/math.mxc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index eef777df..8d5a921b 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -8,7 +8,7 @@ let { // sin(), cos(), tan(), asin(), acos(), atan() // 双曲線関数 -// sinh(), cosh(), *tanh(), *asinh(), *acosh(), *atanh() +// sinh(), cosh(), tanh(), *asinh(), *acosh(), *atanh() // 指数関数, 対数関数 // exp(), exp2(), log(), log10(), log2() @@ -376,6 +376,8 @@ def hypot(x: float, y: float): float { return fabs(u) * sqrt(1.0 + (v / u) * (v / u)); } +def asinh(x: float) = log(x + sqrt(x * x + 1.0)); + def asin(x: float): float { if x < 0.0 { return -asin(-x); From bfb26ff484fac4ebe781f8e1bec70c3ff146f24c Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 18 Apr 2020 00:48:27 +0900 Subject: [PATCH 45/48] add the acosh() function --- lib/math.mxc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/math.mxc b/lib/math.mxc index 8d5a921b..1ebd47ed 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -378,6 +378,8 @@ def hypot(x: float, y: float): float { def asinh(x: float) = log(x + sqrt(x * x + 1.0)); +def acosh(x: float) = if x <= 1.0 0.0 else log(x + sqrt(x * x + 1.0)); + def asin(x: float): float { if x < 0.0 { return -asin(-x); From 267ae23c52c164ec29be11b1be351e32b64d56e0 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 18 Apr 2020 01:10:15 +0900 Subject: [PATCH 46/48] add the functions, acosh() and atanh() --- lib/math.mxc | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index 1ebd47ed..a88827c5 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -264,9 +264,7 @@ def cosh(x: float) = (exp(x) + exp(-x)) * 0.5; def tanh(x: float) = sinh(x) / cosh(x); def log10(x: float): float { - if x <= 0.0 { - return 0.0; - } + assert x > 0.0; let n = 0.0; while x < 1.0 { x = x * 10.0; @@ -293,14 +291,17 @@ def log10(x: float): float { } def log2(x: float): float { + assert x > 0.0; return 3.321928094887362 * log10(x); } def log(x: float): float { + assert x > 0.0; return 2.302585092994046 * log10(x); } def pow(x: float, y: float): float { + assert x > 0.0 || (x == 0.0 && y >= 0.0) || (x < 0.0 && y == tofloat(floor(y))); if x > 0.0 { return exp(y * log(x)); } @@ -308,16 +309,11 @@ def pow(x: float, y: float): float { return 0.0; } else { - if y == tofloat(floor(y)) { - if floor(y) % 2 == 0 { - return exp(y * log(-x)); - } - else { - return -exp(y * log(-x)); - } + if floor(y) % 2 == 0 { + return exp(y * log(-x)); } else { - return 0.0; + return -exp(y * log(-x)); } } } @@ -329,7 +325,8 @@ def ldexp(x: float, exp: int) = x * pow(2.0, tofloat(exp)); def logb(x: float) = if x == 0.0 0 else floor(log2(x)); def sqrt(x :float): float { - if x <= 0.0 { + assert x >= 0.0; + if x == 0.0 { return 0.0; } let e = logb(x) + 1; @@ -345,7 +342,8 @@ def sqrt(x :float): float { } def cbrt(x: float): float { - if x <= 0.0 { + assert x >= 0.0; + if x == 0.0 { return 0.0; } let e = logb(x) + 1; @@ -378,15 +376,21 @@ def hypot(x: float, y: float): float { def asinh(x: float) = log(x + sqrt(x * x + 1.0)); -def acosh(x: float) = if x <= 1.0 0.0 else log(x + sqrt(x * x + 1.0)); +def acosh(x: float): float { + assert fabs(x) >= 1.0; + return log(x + sqrt(x * x - 1.0)); +} + +def atanh(x: float): float { + assert fabs(x) < 1.0; + return 0.5 * log((1.0 + x) / (1.0 - x)); +} def asin(x: float): float { if x < 0.0 { return -asin(-x); } - if x > 1.0 { - assert x > 1.0; - } + assert x <= 1.0; if x > 0.7071067811865475 { return pi * 0.5 - asin(sqrt(1.0 - x * x)); } @@ -439,6 +443,7 @@ def asin(x: float): float { } def acos(x: float): float{ + assert fabs(x) <= 1.0; return pi * 0.5 - asin(x); } From 9a72030a48e459819508858d47445b2d06e966c7 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 18 Apr 2020 11:06:30 +0900 Subject: [PATCH 47/48] add the atan2() function --- lib/math.mxc | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/math.mxc b/lib/math.mxc index a88827c5..86963aac 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -5,10 +5,10 @@ let { // *は未実装 // 三角関数 -// sin(), cos(), tan(), asin(), acos(), atan() +// sin(), cos(), tan(), asin(), acos(), atan(), atan2() // 双曲線関数 -// sinh(), cosh(), tanh(), *asinh(), *acosh(), *atanh() +// sinh(), cosh(), tanh(), asinh(), acosh(), atanh() // 指数関数, 対数関数 // exp(), exp2(), log(), log10(), log2() @@ -57,7 +57,7 @@ def __cos(x: float): float { x4 = x2 * x2; xx = x4 * x4; } - while d > 0.0000000000000001 { + while fabs(d) > 0.0000000000000001 { before = after; after = after + s * xx / f; s = -s; @@ -65,9 +65,6 @@ def __cos(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n - 1.0); d = before - after; - if(d < 0.0) { - d = -d; - } } return after; } @@ -104,7 +101,7 @@ def sin(x: float): float { x4 = x2 * x2; xx = x * x4 * x4; } - while d > 0.0000000000000001 { + while fabs(d) > 0.0000000000000001 { before = after; after = after + s * xx / f; s = -s; @@ -112,9 +109,6 @@ def sin(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n + 1.0); d = before - after; - if d < 0.0 { - d = -d; - } } return sign * after; } @@ -151,7 +145,7 @@ def cos(x: float): float { x4 = x2 * x2; xx = x4 * x4; } - while d > 0.0000000000000001 { + while fabs(d) > 0.0000000000000001 { before = after; after = after + s * xx / f; s = -s; @@ -159,9 +153,6 @@ def cos(x: float): float { n = n + 1.0; f = f * 2.0 * n * (2.0 * n - 1.0); d = before - after; - if d < 0.0 { - d = -d; - } } return sign * after; } @@ -467,18 +458,40 @@ def atan(x: float): float { xx = x * x * x * x2; n = 2.0; } - while(d > 0.0000000000000001) { + while(fabs(d) > 0.0000000000000001) { before = after; after = after + sign * xx / (2.0 * n + 1.0); xx = xx * x2; sign = -sign; n = n + 1.0; d = before - after; - if(d < 0.0) { - d = -d; - } } return after; } +def atan2(y: float, x: float): float { + if x == 0.0 { + if y == 0.0 { + return 0.0; + } + else if y > 0.0 { + return 1.570796326794896557999; + } + else { + return -1.570796326794896557999; + } + } + else if x > 0.0 { + return atan(y / x); + } + else { + if y >= 0.0 { + return atan(y / x) + pi; + } + else { + return atan(y / x) - pi; + } + } +} + def lerp(a: float, b: float, t: float) = a + t * (b - a); \ No newline at end of file From 8abad3fcb3002f999335bdfbe3f9812b4c47b2a2 Mon Sep 17 00:00:00 2001 From: rytaryu <53173674+rytar@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:44:28 +0900 Subject: [PATCH 48/48] add the trunc() function --- lib/math.mxc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/math.mxc b/lib/math.mxc index 86963aac..8c165d70 100644 --- a/lib/math.mxc +++ b/lib/math.mxc @@ -20,7 +20,7 @@ let { // pow(), sqrt(), cbrt(), hypot(), abs(), fabs() // 最近傍整数 -// ceil(), floor(), round() +// ceil(), floor(), round(), trunc() // 線形補間 // lerp() @@ -38,6 +38,8 @@ def floor(x: float) = if x >= 0.0 toint(x) else -toint(-x + 1.0); def round(x: float) = floor(x + 0.5); +def trunc(x: float) = if x >= 0.0 floor(x) else ceil(x); + def max(x: float, y: float) = if x > y x else y; def min(x: float, y: float) = if x < y x else y;