From 45241752e6dfc6bd4d09a3c2681043a9b0f1947f Mon Sep 17 00:00:00 2001 From: Christophe de Dinechin Date: Fri, 16 Aug 2024 19:45:19 +0200 Subject: [PATCH] trigonometrics: Add conversion from non-standard angles Accept `cos(1/4_turn)` without emiting an `Inconsistent Unit` error. Fixes: #1114 Signed-off-by: Christophe de Dinechin --- src/algebraic.cc | 17 +++++++++++++++-- src/tests.cc | 9 +++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/algebraic.cc b/src/algebraic.cc index 3a0e524b..2a45d617 100644 --- a/src/algebraic.cc +++ b/src/algebraic.cc @@ -549,6 +549,7 @@ algebraic::angle_unit algebraic::adjust_angle(algebraic_g &x) // If we have an angle unit, use it for the computation // ---------------------------------------------------------------------------- { +retry: angle_unit amode = ID_object; if (unit_p uobj = x->as()) { @@ -563,6 +564,14 @@ algebraic::angle_unit algebraic::adjust_angle(algebraic_g &x) amode = ID_PiRadians; else if (sym->matches("grad")) amode = ID_Grad; + + } + if (amode == ID_object) + { + algebraic_g aunit = integer::make(1); + if (add_angle(aunit)) + if (unit_p(+aunit)->convert(x)) + goto retry; } if (amode) x = uobj->value(); @@ -589,8 +598,12 @@ bool algebraic::add_angle(algebraic_g &x) } symbol_p uexpr = symbol::make(uname); - x = unit::make(x, uexpr); - return true; + if (algebraic_p angle = unit::make(x, uexpr)) + { + x = angle; + return true; + } + return false; } diff --git a/src/tests.cc b/src/tests.cc index e7588095..1e49fdd2 100644 --- a/src/tests.cc +++ b/src/tests.cc @@ -168,10 +168,7 @@ void tests::run(bool onlyCurrent) if (onlyCurrent) { here().begin("Current"); - // eqnlib_columns_and_beams(); - demo_ui(); - demo_math(); - demo_pgm(); + exact_trig_cases(); } else { @@ -3761,6 +3758,10 @@ void tests::exact_trig_cases() .expect("-1/2"); } + step("Conversion from non-standard units") + .test(CLEAR, "1/8_turn COS", ENTER) + .expect("0.70710 67811 87"); + step("Cleaning up") .test(CLEAR, "SmallFractions DEG", ENTER).noerror(); }