Skip to content

Commit

Permalink
trigonometrics: Add conversion from non-standard angles
Browse files Browse the repository at this point in the history
Accept `cos(1/4_turn)` without emiting an `Inconsistent Unit` error.

Fixes: #1114

Signed-off-by: Christophe de Dinechin <[email protected]>
  • Loading branch information
c3d committed Aug 16, 2024
1 parent cbd165d commit 4524175
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/algebraic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<unit>())
{
Expand All @@ -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();
Expand All @@ -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;
}


Expand Down
9 changes: 5 additions & 4 deletions src/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 4524175

Please sign in to comment.