Skip to content

Commit

Permalink
Correct conv. of polar to Cartesian form with mixed exp (fixes #774)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwright committed Jan 21, 2025
1 parent 76d2b76 commit 62240c7
Show file tree
Hide file tree
Showing 6 changed files with 557 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to

## [Unreleased]

### Fixed
- Handling of mixed exponents in converting complex values from polar to
Cartesian form (see issue
[\#774](https://github.com/josephwright/siunitx/issues/774))

## [v3.4.3] - 2025-01-20

### Fixed
Expand Down
43 changes: 36 additions & 7 deletions siunitx-complex.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -1107,8 +1107,15 @@
% \@@_convert_cartesian_aux:ee
% }
% \begin{macro}[EXP]{\@@_convert_cartesian_aux:w}
% \begin{macro}[EXP]
% {
% \@@_convert_cartesian_aux:nnn ,
% \@@_convert_cartesian_aux:nen ,
% \@@_convert_cartesian_aux:enn
% }
% Conversion to Cartesian form is easy as we have two inputs and need to
% do no parsing here at all.
% do no parsing here at all. In most cases the exponents for the two
% output parts will be the same, but that is not certain.
% \begin{macrocode}
\cs_new:Npn \@@_convert_cartesian:nn #1#2
{
Expand All @@ -1124,25 +1131,47 @@
}
\cs_new:Npn \@@_convert_cartesian_aux:nn #1#2
{
\@@_convert_cartesian_aux:w #1 e e \q_mark #2 e e \q_stop
\@@_convert_cartesian_aux:w #1 e 0 e \q_mark #2 e 0 e \q_stop
}
\cs_generate_variant:Nn \@@_convert_cartesian_aux:nn { ee }
\cs_new:Npn \@@_convert_cartesian_aux:w
#1 e #2 e #3 \q_mark #4 e #5 e #6 \q_stop
{
\int_compare:nNnTF {#2} = {#5}
{ \@@_convert_cartesian_aux:nnn {#1} {#4} {#2} }
{
\int_compare:nNnTF {#2} > {#5}
{
\@@_convert_cartesian_aux:nen
{#1}
{ \fp_to_decimal:n { #4 * 10 ** (#5 - #2) } }
{#2}
}
{
\@@_convert_cartesian_aux:enn
{ \fp_to_decimal:n { #1 * 10 ** (#2 - #5) } }
{#4}
{#5}
}
}
}
\cs_new:Npn \@@_convert_cartesian_aux:nnn #1#2#3
{
\fp_compare:nNnF {#1} = \c_zero_fp
{#1}
\fp_compare:nNnF {#4} = \c_zero_fp
\fp_compare:nNnF {#2} = \c_zero_fp
{
\fp_compare:nNnF {#4} < \c_zero_fp { + }
#4 i
\fp_compare:nNnF {#2} < \c_zero_fp { + }
#2 i
}
\tl_if_blank:nF {#2}
{ e #2 }
\int_compare:nNnF {#3} = 0
{ e #3 }
}
\cs_generate_variant:Nn \@@_convert_cartesian_aux:nnn { e , ne }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_convert_polar:n}
% \begin{macro}[EXP]{\@@_convert_polar_auxi:w}
Expand Down
171 changes: 169 additions & 2 deletions testfiles/siunitx-complex.luatex.tlg
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,174 @@ l. ... }
l. ... }
============================================================
============================================================
TEST 12: Option settings "complex-mode" and "parse-numbers"
TEST 12: Option settings "complex-mode" and "comples angle unit = radians"
============================================================
> \box...=
\hbox(6.67859+0.83333)x207.22227, direction TLT
.\mathon
.\OMS/cmsy/m/n/10 ^^@
.\OT1/cmr/m/n/10 1
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OT1/cmr/m/n/10 +
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OT1/cmr/m/n/10 0
.\OML/cmm/m/it/10 :
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 2
.\OT1/cmr/m/n/10 3
.\OT1/cmr/m/n/10 8
.\glue 1.66663
.\OT1/cmr/m/n/10 4
.\OT1/cmr/m/n/10 6
.\OT1/cmr/m/n/10 2
.\glue 1.66663
.\OT1/cmr/m/n/10 6
.\OT1/cmr/m/n/10 4
.\OT1/cmr/m/n/10 3
.\glue 1.66663
.\OT1/cmr/m/n/10 3
.\OT1/cmr/m/n/10 8
.\OT1/cmr/m/n/10 3
.\glue 1.66663
.\OT1/cmr/m/n/10 2
.\OT1/cmr/m/n/10 7
.\OT1/cmr/m/n/10 9
.\glue 1.66663
.\OT1/cmr/m/n/10 5
.\OT1/cmr/m/n/10 i
.\mathoff
! OK.
<argument> \l_tmpa_box
l. ... }
> \box...=
\hbox(6.67859+0.83333)x194.44446, direction TLT
.\mathon
.\OT1/cmr/m/n/10 0
.\OML/cmm/m/it/10 :
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 6
.\OT1/cmr/m/n/10 1
.\OT1/cmr/m/n/10 9
.\glue 1.66663
.\OT1/cmr/m/n/10 2
.\OT1/cmr/m/n/10 3
.\OT1/cmr/m/n/10 1
.\glue 1.66663
.\OT1/cmr/m/n/10 3
.\OT1/cmr/m/n/10 2
.\OT1/cmr/m/n/10 1
.\glue 1.66663
.\OT1/cmr/m/n/10 6
.\OT1/cmr/m/n/10 9
.\OT1/cmr/m/n/10 1
.\glue 1.66663
.\OT1/cmr/m/n/10 6
.\OT1/cmr/m/n/10 3
.\OT1/cmr/m/n/10 9
.\glue 1.66663
.\OT1/cmr/m/n/10 8
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OT1/cmr/m/n/10 +
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OT1/cmr/m/n/10 i
.\mathoff
! OK.
<argument> \l_tmpa_box
l. ... }
> \box...=
\hbox(6.67859+0.83333)x193.88899, direction TLT
.\mathon
.\OT1/cmr/m/n/10 5
.\OT1/cmr/m/n/10 4
.\OT1/cmr/m/n/10 0
.\OML/cmm/m/it/10 :
.\OT1/cmr/m/n/10 3
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 2
.\glue 1.66663
.\OT1/cmr/m/n/10 3
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 5
.\glue 1.66663
.\OT1/cmr/m/n/10 8
.\OT1/cmr/m/n/10 6
.\OT1/cmr/m/n/10 8
.\glue 1.66663
.\OT1/cmr/m/n/10 1
.\OT1/cmr/m/n/10 3
.\OT1/cmr/m/n/10 9
.\glue 1.66663
.\OT1/cmr/m/n/10 7
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OT1/cmr/m/n/10 +
.\glue(\medmuskip) 2.22217 plus 1.11108 minus 2.22217
.\OT1/cmr/m/n/10 8
.\OT1/cmr/m/n/10 4
.\OT1/cmr/m/n/10 1
.\OML/cmm/m/it/10 :
.\OT1/cmr/m/n/10 4
.\OT1/cmr/m/n/10 7
.\OT1/cmr/m/n/10 0
.\glue 1.66663
.\OT1/cmr/m/n/10 9
.\OT1/cmr/m/n/10 8
.\OT1/cmr/m/n/10 4
.\glue 1.66663
.\OT1/cmr/m/n/10 8
.\OT1/cmr/m/n/10 0
.\OT1/cmr/m/n/10 7
.\glue 1.66663
.\OT1/cmr/m/n/10 8
.\OT1/cmr/m/n/10 9
.\OT1/cmr/m/n/10 6
.\glue 1.66663
.\OT1/cmr/m/n/10 5
.\OT1/cmr/m/n/10 i
.\mathoff
! OK.
<argument> \l_tmpa_box
l. ... }
============================================================
============================================================
TEST 13: Option settings "complex-mode" and "parse-numbers"
============================================================
> \box...=
\hbox(6.59525+0.83333)x20.66728, direction TLT
Expand Down Expand Up @@ -2427,7 +2594,7 @@ l. ... }
l. ... }
============================================================
============================================================
TEST 13: Deprecated option setting "complex-symbol-angle"
TEST 14: Deprecated option setting "complex-symbol-angle"
============================================================
> \box...=
\hbox(6.88586+0.0)x30.97073, direction TLT
Expand Down
9 changes: 9 additions & 0 deletions testfiles/siunitx-complex.lvt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@
}
\TIMO

\TEST { Option~settings~"complex-mode"~and~"comples~angle~unit~=~radians" }
{
\keys_set:nn { siunitx }
{ complex-mode = cartesian , complex-angle-unit = radians }
\test:n { \siunitx_complex_number:nn { 1 } { pi } }
\test:n { \siunitx_complex_number:nn { 1 } { pi / 2 } }
\test:n { \siunitx_complex_number:nn { 1e3 } { 1 } }
}

\TEST { Option~settings~"complex-mode"~and~"parse-numbers" }
{
\keys_set:nn { siunitx } { parse-numbers = false }
Expand Down
Loading

0 comments on commit 62240c7

Please sign in to comment.