From 554cd971450dcb137dc8450cc42f8dfaa6cf0501 Mon Sep 17 00:00:00 2001 From: Landon Curt Noll Date: Tue, 7 Sep 2021 07:59:51 -0700 Subject: [PATCH] Initial code for g2d() & d2g() builtins We still need to add: help files for g2d & d2g regression tests for g2d & d2g notes in related trig help files note in unexpected help file note in CHANGES --- calcerr.tbl | 2 ++ func.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ qmath.c | 5 +++- qmath.h | 3 ++- 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/calcerr.tbl b/calcerr.tbl index d22a2f2..a6fbf89 100644 --- a/calcerr.tbl +++ b/calcerr.tbl @@ -504,3 +504,5 @@ E_G2R1 Bad epsilon for converting gradians to radians E_G2R2 Bad first argument converting gradians to radians E_R2G1 Bad epsilon for converting radians to gradians E_R2G2 Bad first argument converting radians to gradians +E_D2G1 Bad first argument converting degrees to gradians +E_G2D1 Bad first argument converting gradians to degrees diff --git a/func.c b/func.c index 93dca01..9210276 100644 --- a/func.c +++ b/func.c @@ -2349,6 +2349,72 @@ f_r2g(int count, VALUE **vals) } +/* + * f_d2g - convert degrees to gradians + * + * NOTE: The epsilon (vals[1]->v_num) argument is ignored. + */ +/*ARGSUSED*/ +S_FUNC VALUE +f_d2g(int UNUSED(count), VALUE **vals) +{ + VALUE result; + + /* initialize VALUE */ + result.v_subtype = V_NOSUBTYPE; + + /* NOTE: the epsilon (vals[1]->v_num) argument is ignored */ + + /* calculate argument * (10/9) */ + switch (vals[0]->v_type) { + case V_NUM: + result.v_num = qmul(vals[0]->v_num, &_qtendivnine_); + result.v_type = V_NUM; + break; + case V_COM: + result.v_com = c_mulq(vals[0]->v_com, &_qtendivnine_); + result.v_type = V_COM; + break; + default: + return error_value(E_D2G1); + } + return result; +} + + +/* + * f_g2d - convert gradians to degrees + * + * NOTE: The epsilon (vals[1]->v_num) argument is ignored. + */ +/*ARGSUSED*/ +S_FUNC VALUE +f_g2d(int UNUSED(count), VALUE **vals) +{ + VALUE result; + + /* initialize VALUE */ + result.v_subtype = V_NOSUBTYPE; + + /* NOTE: the epsilon (vals[1]->v_num) argument is ignored */ + + /* calculate argument * (9/10) */ + switch (vals[0]->v_type) { + case V_NUM: + result.v_num = qmul(vals[0]->v_num, &_qninedivten_); + result.v_type = V_NUM; + break; + case V_COM: + result.v_com = c_mulq(vals[0]->v_com, &_qninedivten_); + result.v_type = V_COM; + break; + default: + return error_value(E_G2D1); + } + return result; +} + + S_FUNC VALUE f_sin(int count, VALUE **vals) { @@ -8838,6 +8904,8 @@ STATIC CONST struct builtin builtins[] = { "date and time as string"}, {"custom", 0, IN, 0, OP_NOP, 0, f_custom, "custom builtin function interface"}, + {"d2g", 1, 2, 0, OP_NOP, 0, f_d2g, + "convert degrees to gradians"}, {"d2r", 1, 2, 0, OP_NOP, 0, f_d2r, "convert degrees to radians"}, {"delete", 2, 2, FA, OP_NOP, 0, f_listdelete, @@ -8950,6 +9018,8 @@ STATIC CONST struct builtin builtins[] = { "return the file position"}, {"frac", 1, 1, 0, OP_FRAC, qfrac, 0, "fractional part of value"}, + {"g2d", 1, 2, 0, OP_NOP, 0, f_g2d, + "convert gradians to degrees"}, {"g2r", 1, 2, 0, OP_NOP, 0, f_g2r, "convert gradians to radians"}, {"gcd", 1, IN, 0, OP_NOP, f_gcd, 0, diff --git a/qmath.c b/qmath.c index 4c95359..963d6f3 100644 --- a/qmath.c +++ b/qmath.c @@ -44,9 +44,12 @@ NUMBER _qnegone_ = { { _oneval_, 1, 1 }, { _oneval_, 1, 0 }, 1, NULL }; NUMBER _qonehalf_ = { { _oneval_, 1, 0 }, { _twoval_, 1, 0 }, 1, NULL }; NUMBER _qneghalf_ = { { _oneval_, 1, 1 }, { _twoval_, 1, 0 }, 1, NULL }; NUMBER _qonesqbase_ = { { _oneval_, 1, 0 }, { _sqbaseval_, 2, 0 }, 1, NULL }; +NUMBER _qtendivnine_ = { { _tenval_, 1, 0 }, { _nineval_, 1, 0 }, 1, NULL }; +NUMBER _qninedivten_ = { { _nineval_, 1, 0 }, { _tenval_, 1, 0 }, 1, NULL }; NUMBER * initnumbs[INITCONSTCOUNT] = {&_qzero_, &_qone_, &_qtwo_, &_qthree_, - &_qfour_, &_qten_, &_qnegone_, &_qonehalf_, &_qneghalf_}; + &_qfour_, &_qten_, &_qnegone_, &_qonehalf_, &_qneghalf_, + &_qonesqbase_, &_qtendivnine_, &_qninedivten_ }; /* diff --git a/qmath.h b/qmath.h index f19f4d6..1a33e33 100644 --- a/qmath.h +++ b/qmath.h @@ -35,7 +35,7 @@ #endif -#define INITCONSTCOUNT 9 /* number of initnumbs[] pre-defined constants */ +#define INITCONSTCOUNT 12 /* number of initnumbs[] pre-defined constants */ /* * Rational arithmetic definitions. @@ -275,6 +275,7 @@ static inline NUMBER* qlink(NUMBER* q) { if(q) { (q)->links++; } return q; } */ EXTERN NUMBER _qzero_, _qone_, _qnegone_, _qonehalf_, _qneghalf_, _qonesqbase_; EXTERN NUMBER _qtwo_, _qthree_, _qfour_, _qten_; +EXTERN NUMBER _qtendivnine_, _qninedivten_; EXTERN NUMBER * initnumbs[];