complex: Return an angle from arg function

The `arg` function now returns an angle in current angle mode.

Fixes: #1149

Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
This commit is contained in:
Christophe de Dinechin 2024-09-01 00:05:16 +02:00
parent c6068d1c37
commit 985b8a4619
2 changed files with 45 additions and 18 deletions

View file

@ -556,11 +556,20 @@ FUNCTION_BODY(arg)
if (should_be_symbolic(xt))
return symbolic(ID_arg, x);
auto angle_mode = Settings.AngleMode();
algebraic_g a;
if (is_complex(xt))
return complex_p(algebraic_p(x))->arg(angle_mode);
algebraic_g zero = integer::make(0);
bool negative = x->is_negative(false);
return complex::convert_angle(zero, angle_mode, angle_mode, negative);
{
a = complex_p(algebraic_p(x))->arg(angle_mode);
}
else
{
bool negative = x->is_negative(false);
a = integer::make(0);
a = complex::convert_angle(a, angle_mode, angle_mode, negative);
}
if (a && Settings.SetAngleUnits() && a->is_real())
add_angle(a);
return a;
}

View file

@ -170,7 +170,7 @@ void tests::run(uint onlyCurrent)
{
here().begin("Current");
if (onlyCurrent & 1)
for_loops();
complex_functions();
if (onlyCurrent & 2)
demo_ui();
if (onlyCurrent & 4)
@ -4675,15 +4675,6 @@ void tests::complex_functions()
test(CLEAR, "3+4 modulus", ENTER).expect("5.");
test(CLEAR, "a+b modulus", ENTER).expect("'a⊿b'");
step("Complex argument");
test(CLEAR, "1+1 arg", ENTER).expect("0.78539816339744830962");
step("Symbolic complex argument");
test(CLEAR, "a+b arg", ENTER).expect("'b∠a'");
step("Complex argument on integers");
test(CLEAR, "31 arg", ENTER).expect("0");
step("Complex argument on decimals");
test(CLEAR, "31.234 arg", ENTER).expect("0");
step("Complex conjugate");
test(CLEAR, "3+4 conj", ENTER).expect("3-4");
step("Symbolic complex conjugate");
@ -4693,6 +4684,33 @@ void tests::complex_functions()
step("Complex conjugate on decimals");
test(CLEAR, "31.234 conj", ENTER).expect("31.234");
step("Complex argument");
test(CLEAR, "1+1 arg", ENTER).expect("0.78539816339744830962r");
step("Symbolic complex argument");
test(CLEAR, "a+b arg", ENTER).expect("'b∠a'");
step("Complex argument on integers");
test(CLEAR, "31 arg", ENTER).expect("0r");
step("Complex argument on decimals");
test(CLEAR, "31.234 arg", ENTER).expect("0r");
step("Complex argument on negative integers");
test(CLEAR, "-31 arg", ENTER).expect("3.1415926535897932385r");
step("Complex argument on negative decimals");
test(CLEAR, "-31.234 arg", ENTER).expect("3.1415926535897932385r");
step("Complex argument in degrees");
test(CLEAR, "DEG", ENTER);
test(CLEAR, "1+1 arg", ENTER).expect("45°");
step("Symbolic complex argument in degrees");
test(CLEAR, "a+b arg", ENTER).expect("'b∠a'");
step("Complex argument on integers in degrees");
test(CLEAR, "31 arg", ENTER).expect("0°");
step("Complex argument on decimals in degrees");
test(CLEAR, "31.234 arg", ENTER).expect("0°");
test(CLEAR, "-31 arg", ENTER).expect("180°");
step("Complex argument on decimals in degrees");
test(CLEAR, "-31.234 arg", ENTER).expect("180°");
test(CLEAR, "RAD", ENTER);
step("Restore default 24-digit precision");
test(CLEAR, "24 PRECISION 12 SIG", ENTER).noerror();
}
@ -8785,13 +8803,13 @@ void tests::regression_checks()
test(CLEAR, "'x' #2134AF AND", ENTER).error("Bad argument type");
step("Bug 277: 1+i should have positive arg");
test(CLEAR, "1+1 arg", ENTER).expect("45");
test(CLEAR, "1-1 arg", ENTER).expect("-45");
test(CLEAR, "1 1 atan2", ENTER).expect("45");
test(CLEAR, "1+1 arg", ENTER).expect("45 °");
test(CLEAR, "1-1 arg", ENTER).expect("-45 °");
test(CLEAR, "1 1 atan2", ENTER).expect("45 °");
test(CLEAR, "1+1 ToPolar", ENTER).match("1.414.*∡45°");
step("Bug 287: arg of negative number");
test(CLEAR, "-35 arg", ENTER).expect("180");
test(CLEAR, "-35 arg", ENTER).expect("180 °");
step("Bug 288: Abusive simplification of multiplication by -1");
test(CLEAR, "-1 3 *", ENTER).expect("-3");