summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/siunitx/siunitx-complex.dtx
blob: fe235989503640761a64954d26a37eb1c25da7e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
% \iffalse meta-comment
%
% File: siunitx-complex.dtx Copyright (C) 2021-2023 Joseph Wright
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "siunitx bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% The released version of this bundle is available from CTAN.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/josephwright/siunitx
%
% for those people who are interested.
%
% -----------------------------------------------------------------------
%
%<*driver>
\documentclass{l3doc}
% Additional commands needed in this source
\ProvideDocumentCommand\email{m}{\href{mailto:#1}{\nolinkurl{#1}}}
\ProvideDocumentCommand\foreign{m}{\textit{#1}}
% The next line is needed so that \GetFileInfo will be able to pick up
% version data
\usepackage{siunitx}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{siunitx.sty}
%
% \title{^^A
%   \pkg{siunitx-complex} -- Complex numbers^^A
%   \thanks{This file describes \fileversion,
%     last revised \filedate.}^^A
% }
%
% \author{^^A
%  Joseph Wright^^A
%  \thanks{^^A
%    E-mail:
%    \email{joseph.wright@morningstar2.co.uk}^^A
%   }^^A
% }
%
% \date{Released \filedate}
%
% \maketitle
%
% \begin{documentation}
%
% This submodule is concerned with formatting complex numbers. It augments the
% standard functions \cs{siunitx_number_format:nN} and \cs{siunitx_quantity:nn}
% by allowing parsing of numbers with a complex part. There are no additional
% assumptions concerning \LaTeXe{} commands in the submodule beyond those in the
% core number and unit submodules.
%
% \begin{function}{\siunitx_complex_number:n}
%   \begin{syntax}
%     \cs{siunitx_complex_number:n} \Arg{number}
%   \end{syntax}
%   Parses the \meta{number} and splits into real and complex parts, which are
%   then formatted as described for \cs{siunitx_number_format:nN}. The results
%   are combined and printed using the standard functions in the module. If
%   the setting \opt{complex-mode} is set to \meta{polar}, the input is
%   parsed, converted to polar form and then passed to
%   \cs{siunitx_complex_number:nn}. This parsing requires that the complex root
%   is given as \texttt{i} at the \emph{end} of the value.
% \end{function}
%
% \begin{function}{\siunitx_complex_number:nn}
%   \begin{syntax}
%     \cs{siunitx_complex_number:nn} \Arg{magnitude} \Arg{angle}
%   \end{syntax}
%   Parses the \meta{magnitude} and \meta{angle} and then formats each as
%   described for \cs{siunitx_number_format:nN}. The two are separated by the
%   angle symbol, which is treated as a numerical part. If
%   \opt{complex-angle-unit} is set to \opt{degrees} then the unit symbol is
%   added: this is printed as a unit in the usual way. If
%   the setting \opt{complex-mode} is set to \meta{cartesian}, the input is
%   parsed, converted to Cartesian form and then passed to
%   \cs{siunitx_complex_number:n}.
% \end{function}
%
% \begin{function}{\siunitx_complex_quantity:nn, \siunitx_complex_quantity:nnn}
%   \begin{syntax}
%     \cs{siunitx_complex_quantity:nn} \Arg{number} \Arg{units}
%     \cs{siunitx_complex_quantity:nnn} \Arg{magnitude} \Arg{angle} \Arg{units}
%   \end{syntax}
%   These functions treat their numerical argument(s) as described
%   for the corresponding \texttt{number} functions. They then typeset the
%   entire numerical part and the unit as described for \cs{siunitx_quantity:nn}.
% \end{function}
%
% \begin{function}{complex-angle-unit}
%   \begin{syntax}
%     |complex-angle-unit| = |degrees|\verb"|"|radians|
%   \end{syntax}
%   Sets how the unit for polar complex numbers is treated. This setting
%   is used to determine how polar \emph{input} is interpreted, how
%   conversion to polar form works and how output in polar form is typeset.
%   The standard setting is |degrees|.
% \end{function}
%
% \begin{function}{complex-mode}
%   \begin{syntax}
%     |complex-mode| = |cartesian|\verb"|"|input|\verb"|"|polar|
%   \end{syntax}
%   Selects how complex values are formatted: a choice from the options
%   |cartesian|, |input| and |polar|. The option |cartesian| means that
%   complex values will always be typeset in cartesian ($x + y\mathrm{i}$)
%   format, whilst |polar| means that complex are typeset as a magnitude
%   and angle. Finally, |input| setting means that the input format
%   (\foreign{i.e.}~difference between \cs{siunitx_complex_number:n} and
%   \cs{siunitx_complex_number:nn}) is maintained. The standard setting is
%   |input|.
% \end{function}
%
% \begin{function}{complex-root-position}
%   \begin{syntax}
%     |complex-root-position| = |after-number|\verb"|"|before-number|
%   \end{syntax}
%   Choice which determines where the complex root symbol is printed relative
%   to the numbers. The standard setting is |after-number|.
% \end{function}
%
% \begin{function}{complex-symbol-angle}
%   \begin{syntax}
%     |complex-symbol-angle| = \meta{symbol}
%   \end{syntax}
%   Sets the symbol used before the polar angle.
% \end{function}
%
% \begin{function}{complex-symbol-degree}
%   \begin{syntax}
%     |complex-symbol-degree| = \meta{symbol}
%   \end{syntax}
%   Sets the symbol used for polar degrees.
% \end{function}
%
% \begin{function}{input-complex-root}
%   \begin{syntax}
%     |input-complex-root| = \meta{tokens}
%   \end{syntax}
%   The token(s) considered as complexes roots for number parsing.
%   The standard setting is |ij|.
% \end{function}
%
% \begin{function}{output-complex-root}
%   \begin{syntax}
%     |output-complex-root| = \meta{tokens}
%   \end{syntax}
%   The token(s) used to show the complex root in output. The standard setting
%   is |\mathrm{i}|.
% \end{function}
%
% \begin{function}{print-complex-unity}
%   \begin{syntax}
%     |print-complex-unity| = |true|\verb"|"|false|
%   \end{syntax}
%   Switch to determine if the number \num{1} is printed for a complex
%   part which is exactly unity.
% \end{function}
%
% \end{documentation}
%
% \begin{implementation}
%
% Start the \pkg{DocStrip} guards.
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% \section{\pkg{siunitx-complex} implementation}
%
% Identify the internal prefix (\LaTeX3 \pkg{DocStrip} convention): only
% internal material in this \emph{submodule} should be used directly.
%    \begin{macrocode}
%<@@=siunitx_complex>
%    \end{macrocode}
%
% \subsection{General setup}
%
% \begin{variable}{\l_@@_tmp_fp, \l_@@_tmp_tl}
%    \begin{macrocode}
\fp_new:N \l_@@_tmp_fp
\tl_new:N \l_@@_tmp_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_input_tl}
%   The numerical input exactly as given by the user.
%    \begin{macrocode}
\tl_new:N \l_@@_input_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_comparator_tl}
%   A comparator, if found, is held here.
%    \begin{macrocode}
\tl_new:N \l_@@_comparator_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_exp_tl}
%   The exponent part of a parsed number.
%    \begin{macrocode}
\tl_new:N \l_@@_exp_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_real_tl, \l_@@_img_tl}
%   The real and imaginary parts of cartesian form, respectively.
%    \begin{macrocode}
\tl_new:N \l_@@_real_tl
\tl_new:N \l_@@_img_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_mag_tl, \l_@@_angle_tl}
%   The magnitude and angle of polar form, respectively.
%    \begin{macrocode}
\tl_new:N \l_@@_mag_tl
\tl_new:N \l_@@_angle_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_join_tl, \l_@@_sign_tl}
%   Staging posts for a joining and leading sign, respectively.
%    \begin{macrocode}
\tl_new:N \l_@@_join_tl
\tl_new:N \l_@@_sign_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}
%   {
%     \l_@@_root_after_bool      ,
%     \l_@@_force_cartesian_bool ,
%     \l_@@_force_polar_bool     ,
%     \l_@@_polar_degree_bool    ,
%     \l_@@_symbol_angle_tl      ,
%     \l_@@_symbol_degree_tl     ,
%     \l_@@_input_root_tl        ,
%     \l_@@_output_root_tl
%   }
%    \begin{macrocode}
\bool_new:N \l_@@_root_after_bool
\bool_new:N \l_@@_force_cartesian_bool
\bool_new:N \l_@@_force_polar_bool
\bool_new:N \l_@@_polar_degree_bool
\keys_define:nn { siunitx }
  {
    complex-mode .choice: ,
    complex-mode / cartesian .code:n =
      {
        \bool_set_true:N \l_@@_force_cartesian_bool
        \bool_set_false:N \l_@@_force_polar_bool
      } ,
    complex-mode / polar .code:n =
      {
        \bool_set_false:N \l_@@_force_cartesian_bool
        \bool_set_true:N \l_@@_force_polar_bool
      } ,
    complex-mode / input .code:n =
      {
        \bool_set_false:N \l_@@_force_cartesian_bool
        \bool_set_false:N \l_@@_force_polar_bool
      } ,
    complex-angle-unit .choice: ,
    complex-angle-unit / degrees .code:n =
      { \bool_set_true:N \l_@@_polar_degree_bool } ,
    complex-angle-unit / radians .code:n =
      { \bool_set_false:N \l_@@_polar_degree_bool } ,
    complex-root-position .choice: ,
    complex-root-position / after-number .code:n =
      { \bool_set_true:N \l_@@_root_after_bool } ,
    complex-root-position / before-number .code:n =
      { \bool_set_false:N \l_@@_root_after_bool } ,
    complex-symbol-angle .tl_set:N =
      \l_@@_symbol_angle_tl ,
    complex-symbol-degree .tl_set:N =
      \l_@@_symbol_degree_tl ,
    input-complex-root .tl_set:N =
      \l_@@_input_root_tl ,
    output-complex-root .tl_set:N =
      \l_@@_output_root_tl ,
    print-complex-unity .bool_set:N =
      \l_@@_print_unity_bool
  }
%    \end{macrocode}
% \end{variable}
%
% \subsection{Parsing}
%
% \begin{macro}{\@@_parse:nNN}
% \begin{macro}{\@@_parse_end:}
% \begin{macro}{\@@_parse_clear:}
%   Parsing for complex numbers needs some of the same approaches as the
%   general parser. However, as the aim here is to do only enough to split
%   the real and imaginary parts before handing off the the usual code,
%   it's not a full repeat. Instead, we shortcut where we can. The |clear|
%   function here is not only there to make this function shorter: it
%   also allows a single way to zap any stored data if a parse error occurs.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse:nNN #1#2#3
  {
    \group_begin:
      \@@_parse_clear:
      \protected@edef \l_@@_arg_tl {#1}
      \tl_set_eq:NN \l_@@_input_tl \l_@@_arg_tl
      \siunitx_number_normalize_symbols:N \l_@@_arg_tl
      \tl_if_empty:NF \l_@@_arg_tl
        { \@@_parse_comparator: }
      \@@_parse_check:
      \cs_set_protected:Npx \@@_parse_end:
        {
          \tl_set:Nn \exp_not:N #2 { \exp_not:V \l_@@_real_tl }
          \tl_set:Nn \exp_not:N #3 { \exp_not:V \l_@@_img_tl }
        }
    \exp_after:wN \group_end:
    \@@_parse_end:
  }
\cs_new_protected:Npn \@@_parse_end: { }
\cs_new_protected:Npn \@@_parse_clear:
  {
    \tl_clear:N \l_@@_real_tl
    \tl_clear:N \l_@@_img_tl
    \tl_clear:N \l_@@_exp_tl
    \tl_clear:N \l_@@_sign_tl
    \tl_clear:N \l_@@_join_tl
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_check:}
% \begin{macro}{\@@_parse_check:N}
%   Now we tidy up and do the main work: passing to the standard formatter for
%   final parsing.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_check:
  {
    \tl_if_empty:NTF \l_@@_img_tl
      { \@@_parse_check:N \l_@@_real_tl }
      {
        \tl_if_empty:NTF \l_@@_real_tl
          { \@@_parse_check:N \l_@@_img_tl }
          {
            \@@_parse_check:N \l_@@_real_tl
            \tl_set_eq:NN \l_@@_sign_tl \l_@@_join_tl
            \@@_parse_check:N \l_@@_img_tl
          }
      }
  }
\cs_new_protected:Npn \@@_parse_check:N #1
  {
    \tl_set:Nx #1
      {
        \exp_not:V \l_@@_comparator_tl
        \exp_not:V \l_@@_sign_tl
        \exp_not:V #1
        \exp_not:V \l_@@_exp_tl
      }
    \tl_clear:N \l_@@_comparator_tl
    \tl_clear:N \l_@@_sign_tl
    \siunitx_number_parse:VN #1 #1
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_comparator:}
% \begin{macro}{\@@_parse_comparator_aux:Nw}
%   The first step is to extract any comparator: this is the same as
%   for a full number parse.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_comparator:
  {
    \exp_after:wN \@@_parse_comparator_aux:Nw
      \l_@@_arg_tl \q_stop
  }
\cs_new_protected:Npn \@@_parse_comparator_aux:Nw #1#2 \q_stop
  {
    \tl_if_in:NnTF \l_siunitx_number_input_comparator_tl {#1}
      {
        \tl_set:Nn \l_@@_comparator_tl {#1}
        \tl_set:Nn \l_@@_arg_tl {#2}
      }
      { \tl_clear:N \l_@@_comparator_tl }
    \tl_if_empty:NF \l_@@_arg_tl
      { \@@_parse_sign: }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_exponent:}
% \begin{macro}{\@@_parse_exponent_auxi:w}
% \begin{macro}{\@@_parse_exponent_auxii:nn}
%   An exponent part of a number has to come at the end and can only occur
%   once. Thus it is relatively easy to parse. The code here is a simplified
%   version of that in \pkg{siunitx-number}: we only need to find \emph{some}
%   exponent, not check on the detail. Notice that we need to retain the
%   exponent marker here: that is done using the short-lived temporary
%   variable.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_exponent:
  {
    \tl_if_empty:NTF \l_siunitx_number_input_exponent_tl
      { \@@_parse_root: }
      {
        \tl_set:Nx \l_@@_tmp_tl
          { \tl_head:V \l_siunitx_number_input_exponent_tl }
        \tl_map_inline:Nn \l_siunitx_number_input_exponent_tl
          {
            \tl_replace_all:NnV \l_@@_arg_tl
              {##1} \l_@@_tmp_tl
          }
        \use:x
          {
            \cs_set_protected:Npn
              \exp_not:N \@@_parse_exponent_auxi:w
              ####1 \exp_not:V \l_@@_tmp_tl
              ####2 \exp_not:V \l_@@_tmp_tl
              ####3 \exp_not:N \q_stop
          }
            { \@@_parse_exponent_auxii:nn {##1} {##2} }
        \use:x
          {
            \@@_parse_exponent_auxi:w
              \exp_not:V \l_@@_arg_tl
              \exp_not:V \l_@@_tmp_tl \exp_not:N \q_nil
              \exp_not:V \l_@@_tmp_tl \exp_not:N \q_stop
          }
      }
  }
\cs_new_protected:Npn \@@_parse_exponent_auxi:w  { }
\cs_new_protected:Npn \@@_parse_exponent_auxii:nn #1#2
  {
    \quark_if_nil:nF {#2}
      {
        \tl_set:Nn \l_@@_arg_tl {#1}
        \tl_set:Nx \l_@@_exp_tl
          { \exp_not:V \l_@@_tmp_tl \exp_not:n {#2} }
      }
    \@@_parse_root:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_root:}
% \begin{macro}{\@@_parse_root_auxi:w}
% \begin{macro}{\@@_parse_root_auxii:nn}
%   Splitting at the complex root is much like splitting the exponent.
%   After dealing with the case where there is no complex root allowed,
%   use the first possible symbol to do the work.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_root:
  {
    \tl_if_empty:NTF \l_@@_input_root_tl
      { \tl_set_eq:NN \l_@@_real_tl \l_@@_arg_tl }
      {
        \tl_set:Nx \l_@@_tmp_tl
          { \tl_head:V \l_@@_input_root_tl }
        \tl_map_inline:Nn \l_@@_input_root_tl
          {
            \tl_replace_all:NnV \l_@@_arg_tl
              {##1} \l_@@_tmp_tl
          }
        \use:x
          {
            \cs_set_protected:Npn
              \exp_not:N \@@_parse_root_auxi:w
              ####1 \exp_not:V \l_@@_tmp_tl
              ####2 \exp_not:V \l_@@_tmp_tl
              ####3 \exp_not:N \q_stop
          }
            { \@@_parse_root_auxii:nn {##1} {##2} }
        \use:x
          {
            \@@_parse_root_auxi:w
              \exp_not:V \l_@@_arg_tl
              \exp_not:V \l_@@_tmp_tl \exp_not:N \q_nil
              \exp_not:V \l_@@_tmp_tl \exp_not:N \q_stop
          }
      }
  }
\cs_new_protected:Npn \@@_parse_root_auxi:w  { }
%    \end{macrocode}
%   This is where the business end lies. We have four possibilities:
%   \begin{itemize}
%     \item There was no complex root at all: |#2| will be |\q_nil|
%     \item All of the number is in the complex part with a leading
%       root: |#1| will be empty. This includes the case where
%       the input was \emph{just} a root symbol (plus possibly sign,
%       exponent): we need to cover that.
%     \item All of the number was before the complex root: |#2| will
%       be empty and we need to check |#1| fully to split out the two
%       parts
%     \item The input has a real part with the complex part starting
%       with the root symbol: just the last token needs to be separated.
%   \end{itemize}
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_root_auxii:nn #1#2
  {
    \quark_if_nil:nTF {#2}
      { \tl_set:Nn \l_@@_real_tl {#1} }
      {
        \tl_set:Nn \l_@@_img_tl {#2}
        \tl_if_blank:nTF {#1}
          {
            \tl_if_blank:nT {#2}
              { \tl_set:Nn \l_@@_img_tl { 1 } }
          }
          {
            \tl_if_blank:nTF {#2}
              { \@@_parse_split:n {#1} }
              { \@@_parse_sign_check:n {#1} }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_sign:}
% \begin{macro}{\@@_parse_sign_aux:Nw}
%   The first token of a number after a comparator could be a sign. A quick
%   check is made and if found stored. There is no need to worry about the
%   nature of the sign: we keep them regardless.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_sign:
  {
    \exp_after:wN \@@_parse_sign_aux:Nw
      \l_@@_arg_tl \q_stop
  }
\cs_new_protected:Npn \@@_parse_sign_aux:Nw #1#2 \q_stop
  {
    \tl_if_in:NnT \l_siunitx_number_input_sign_tl {#1}
      {
        \tl_set:Nn \l_@@_sign_tl {#1}
        \tl_set:Nn \l_@@_arg_tl {#2}
      }
    \tl_if_empty:NF \l_@@_arg_tl
      { \@@_parse_exponent: }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_sign_check:n}
% \begin{macro}{\@@_parse_sign_check:nN}
% \begin{macro}{\@@_parse_sign_check:nNw}
%   Here, we want to check that the last token in the input is a sign.
%   There cannot be anything after the sign, and there has to be at one
%   token before the sign: we can therefore signal a parsing error if we
%   need to.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_sign_check:n #1
  {
    \@@_parse_sign_check:nN { } #1 \q_recursion_tail \q_recursion_stop
  }
\cs_new_protected:Npn \@@_parse_sign_check:nN #1#2
  {
    \quark_if_recursion_tail_stop_do:Nn #2
      { \@@_parse_clear: }
    \tl_if_in:NnTF \l_siunitx_number_input_sign_tl {#2}
      { \@@_parse_sign_check:nNw {#1} #2 }
      { \@@_parse_sign_check:nN {#1#2} }
  }
\cs_new_protected:Npn \@@_parse_sign_check:nNw
  #1#2 #3 \q_recursion_tail \q_recursion_stop
  {
    \tl_if_blank:nTF {#3}
      {
        \tl_if_blank:nTF {#1}
          { \@@_parse_clear: }
          {
            \tl_set:Nn \l_@@_real_tl {#1}
            \tl_set:Nn \l_@@_join_tl {#2}
          }
      }
      { \@@_parse_clear: }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_split:n}
% \begin{macro}{\@@_parse_split:nN}
% \begin{macro}{\@@_parse_split:w}
%   Checking for a sign inside the leading part of the number is a simple loop.
%   There is the possibility that there is no number in the imaginary part
%   needs to be allowed for. Notice that we do a check that there is some
%   real part: this covers for example an original input |++1i|, which
%   otherwise would not be trapped.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_split:n #1
  {
    \@@_parse_split:nN { } #1 \q_recursion_tail \q_recursion_stop
  }
\cs_new_protected:Npn \@@_parse_split:nN #1#2
  {
    \quark_if_recursion_tail_stop_do:Nn #2
      { \tl_set:Nn \l_@@_img_tl {#1} }
    \tl_if_in:NnTF \l_siunitx_number_input_sign_tl {#2}
      {
        \tl_set:Nn \l_@@_real_tl {#1}
        \tl_set:Nn \l_@@_join_tl {#2}
        \@@_parse_split:w
      }
      { \@@_parse_split:nN {#1#2} }
  }
\cs_new_protected:Npn \@@_parse_split:w #1 \q_recursion_tail \q_recursion_stop
  {
    \tl_set:Nx \l_@@_img_tl
      {
        \tl_if_blank:nTF {#1}
          { 1 }
          { \exp_not:n {#1} }
     }
    \tl_if_empty:NT \l_@@_real_tl
      { \@@_parse_clear: }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_parse_polar:nn}
%   Almost trivial but repeated in a couple of places so worth an auxiliary.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_parse_polar:nn #1#2
  {
    \siunitx_number_parse:nN {#1} \l_@@_mag_tl
    \group_begin:
      \keys_set:nn { siunitx }
        {
          input-comparators       = ,
          input-exponent-markers  = ,
          input-open-uncertainty  = ,
          input-close-uncertainty =
        }
      \siunitx_number_format:nN {#2} \l_@@_angle_tl
    \exp_args:NNNV \group_end:
    \tl_set:Nn \l_@@_angle_tl \l_@@_angle_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \section{Formatting}
%
% \begin{variable}{\l_@@_bracket_close_tl, \l_@@_bracket_open_tl}
%   Purely internal for the present.
%    \begin{macrocode}
\tl_new:N \l_@@_bracket_close_tl
\tl_new:N \l_@@_bracket_open_tl
\tl_set:Nn \l_@@_bracket_open_tl { ( }
\tl_set:Nn \l_@@_bracket_close_tl { ) }
%    \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_unit_tl}
%    \begin{macrocode}
\tl_new:N \l_@@_unit_tl
%    \end{macrocode}
% \end{variable}
%
% \begin{macro}{\siunitx_complex_number:n}
% \begin{macro}{\siunitx_complex_number:nn, \@@_number:nn}
% \begin{macro}{\siunitx_complex_quantity:nn}
% \begin{macro}{\siunitx_complex_quantity:nnn, \@@_quantity:nnn}
%   The work here is pretty trivial: only conversion between forms makes
%   things a bit more intricate.
%    \begin{macrocode}
\cs_new_protected:Npn \siunitx_complex_number:n #1
  {
    \bool_if:NTF \l_@@_force_polar_bool
      {
        \use:e
          {
            \siunitx_complex_number:nn
               \@@_convert_polar:n {#1}
          }
      }
      {
        \bool_if:NTF \l_siunitx_number_parse_bool
          {
            \@@_parse:nNN {#1} \l_@@_real_tl \l_@@_img_tl
            \@@_format_cartesian:n { }
          }
          {
            \siunitx_number_format:nN {#1} \l_@@_tmp_tl
            \siunitx_print_number:V \l_@@_tmp_tl
          }
      }
  }
\cs_new_protected:Npn \siunitx_complex_number:nn #1#2
  {
    \bool_lazy_or:nnTF
      { \tl_if_blank_p:n {#1} }
      { \tl_if_blank_p:n {#2} }
      {
        \msg_error:nnnn { siunitx } { invalid-polar-form }
          {#1} {#2}
      }
      { \@@_number:nn {#1} {#2} }
  }
\cs_new_protected:Npn \@@_number:nn #1#2
  {
    \bool_if:NTF \l_@@_force_cartesian_bool
      {
        \exp_args:Ne \siunitx_complex_number:n
          { \@@_convert_cartesian:nn {#1} {#2} }
      }
      {
        \@@_parse_polar:nn {#1} {#2}
        \@@_format_polar:n { }
      }
  }
\cs_new_protected:Npn \siunitx_complex_quantity:nn #1#2
  {
    \bool_if:NTF \l_@@_force_polar_bool
      {
        \use:e
          {
            \siunitx_complex_quantity:nnn
               \@@_convert_polar:n {#1}
          }
            {#2}
      }
      {
        \bool_if:NTF \l_siunitx_number_parse_bool
          {
            \@@_parse:nNN {#1} \l_@@_real_tl \l_@@_img_tl
            \@@_format_cartesian:n {#2}
          }
          { \siunitx_quantity:nn {#1} {#2} }
      }
  }
\cs_new_protected:Npn \siunitx_complex_quantity:nnn #1#2#3
  {
    \bool_lazy_or:nnTF
      { \tl_if_blank_p:n {#1} }
      { \tl_if_blank_p:n {#2} }
      {
        \msg_error:nnnn { siunitx } { invalid-polar-form }
          {#1} {#2}
      }
      { \@@_quantity:nnn {#1} {#2} {#3} }
  }
\cs_new_protected:Npn \@@_quantity:nnn #1#2#3
  {
    \bool_if:NTF \l_@@_force_cartesian_bool
      {
        \exp_args:Ne \siunitx_complex_quantity:nn
          { \@@_convert_cartesian:nn {#1} {#2} }
          {#3}
      }
      {
        \@@_parse_polar:nn {#1} {#2}
        \@@_format_polar:n {#3}
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_format_cartesian:n}
% \begin{macro}{\@@_format_cartesian_auxi:n}
% \begin{macro}{\@@_format_cartesian_auxii:n}
% \begin{macro}{\@@_drop_exponent:nnnnnnn}
% \begin{macro}{\@@_format_sign:nnnnnnn}
% \begin{macro}{\@@_extract_exponent:nw}
% \begin{macro}{\@@_extract_exponent_aux:w}
% \begin{macro}[EXP]{\@@_format_bracket:n}
%   We start here checking that there is something to do.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_cartesian:n #1
  {
    \bool_lazy_and:nnF
      { \tl_if_empty_p:N \l_@@_real_tl }
      { \tl_if_empty_p:N \l_@@_img_tl }
      { \@@_format_cartesian_auxi:n {#1} }
  }
%    \end{macrocode}
%  We split based on whether the number has a complex part at all,
%  then print the result.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_cartesian_auxi:n #1
  {
    \tl_clear:N \l_@@_tmp_tl
    \tl_if_empty:NTF \l_@@_img_tl
      {
        \siunitx_number_process:NN \l_@@_real_tl \l_@@_real_tl
        \tl_set:Nx \l_@@_tmp_tl
          { \siunitx_number_output:N \l_@@_real_tl }
      }
      { \@@_format_cartesian_auxii:n {#1} }
    \tl_if_blank:nTF {#1}
      { \siunitx_print_number:V \l_@@_tmp_tl }
      { \siunitx_quantity_print:VV \l_@@_tmp_tl \l_@@_unit_tl }
  }
%    \end{macrocode}
%   If we get to this stage we have both parts to a complex number. We
%   need to process both and do some massaging, then it's just a question
%   of reassembly with the right parts in the right places.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_cartesian_auxii:n #1
  {
    \@@_format_cartesian_units:n {#1}
    \tl_if_empty:NF \l_@@_real_tl
      { \exp_after:wN \@@_drop_exponent:nnnnnnn \l_@@_real_tl }
    \exp_after:wN \@@_format_sign:nnnnnnn \l_@@_img_tl
    \tl_set:Nx \l_@@_tmp_tl
      { \siunitx_number_output:NN \l_@@_img_tl \q_nil }
    \exp_after:wN \@@_extract_exponent:w \l_@@_tmp_tl \q_stop
    \tl_set:Nx \l_@@_tmp_tl
      {
        \bool_lazy_or:nnTF
          {
            \bool_lazy_and_p:nn
              { \l_siunitx_number_bracket_ambiguous_bool }
              { ! \tl_if_empty_p:N \l_@@_exp_tl }
          }
          {
            ! \bool_lazy_any_p:n
              {
                { \tl_if_blank_p:n {#1} }
                { \tl_if_empty_p:N \l_@@_real_tl }
                { \tl_if_empty_p:N \l_@@_img_tl }
              }
          }
          { \@@_format_bracket:n }
          { \use:n }
          {
            \siunitx_number_output:N \l_@@_real_tl
            \exp_not:V \l_@@_sign_tl
            \bool_if:NF \l_@@_root_after_bool
              { \exp_not:V \l_@@_output_root_tl }
             \exp_not:V \l_@@_tmp_tl
            \bool_if:NT \l_@@_root_after_bool
              { \exp_not:V \l_@@_output_root_tl }
           }
         \exp_not:V \l_@@_exp_tl
      }
  }
%    \end{macrocode}
%   No exponent for the real part. 
%    \begin{macrocode}
\cs_new_protected:Npn \@@_drop_exponent:nnnnnnn #1#2#3#4#5#6#7
  { \tl_set:Nn \l_@@_real_tl { {#1} {#2} {#3} {#4} {#5} { } { 0 } } }
%    \end{macrocode}
%   Ensure the imaginary part has a sign.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_sign:nnnnnnn #1#2#3#4#5#6#7
  {
    \tl_set:Nx \l_@@_img_tl
      {
        { }
        {
          \tl_if_blank:nTF {#2}
            { \tl_if_empty:NF \l_@@_real_tl { + } }
            { \exp_not:n {#2} }
        }
        \exp_not:n { {#3} {#4} {#5} {#6} {#7} }
      }
  }
%    \end{macrocode}
%   Pull out the formatted exponent: we also need the sign.
%   An imaginary part that is exactly $1$ is omitted, with only the complex
%   root printed. That means checking and removing a lone $1$ here.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_extract_exponent:w
  #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_nil #5 \q_nil #6 \q_nil #7 \q_nil #8
  \q_nil #9 \q_stop
  {
    \tl_set:Nn \l_@@_sign_tl {#1#2}
    \bool_lazy_all:nTF
      {
        { ! \l_@@_print_unity_bool }
        { \str_if_eq_p:nn {#3} { 1 } }
        { \tl_if_blank_p:n {#5} }
      }
      { \@@_extract_exponent_aux:nw {#6#7#8} }
      { \@@_extract_exponent_aux:nw {#3#4#5#6#7#8} }
         #9 \q_stop
  }
\cs_new:Npn \@@_extract_exponent_aux:nw
  #1#2 \q_nil #3 \q_nil #4 \q_stop
  {
    \tl_set:Nn \l_@@_tmp_tl {#1#2}
    \tl_set:Nn \l_@@_exp_tl {#3#4}
  }
\cs_new_protected:Npn \@@_format_bracket:n #1
  {
    \exp_not:V \l_@@_bracket_open_tl
    #1
    \exp_not:V \l_@@_bracket_close_tl
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_format_cartesian_units:n}
% \begin{macro}
%   {
%     \@@_format_cartesian_combine-exponent:n ,
%     \@@_format_cartesian_extract-exponent:n ,
%     \@@_format_cartesian_input:n
%   }
% \begin{macro}{\@@_format_extract-exponent:N}
% \begin{macro}[EXP]{\@@_extract_exp:nnnnnnn}
% \begin{macro}{\@@_drop_exp:N}
% \begin{macro}[EXP]{\@@_drop_exp:nnnnnnnN}
%   Formatting units needs to know the settings from the main module, and
%   the flow is then much the same as in \pkg{siunitx-compound}. We only
%   have to watch the fact there are two numbers to format.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_cartesian_units:n #1
  {
    \tl_if_blank:nTF {#1}
      {
        \siunitx_number_process:NN \l_@@_real_tl \l_@@_real_tl
        \siunitx_number_process:NN \l_@@_img_tl \l_@@_img_tl
      }
      {
        \use:c
          { @@_format_cartesian_ \l_siunitx_quantity_prefix_mode_tl :n } {#1}
      }
  }
\cs_new_protected:cpn { @@_format_cartesian_combine-exponent:n } #1
  {
    \tl_if_empty:NF \l_@@_real_tl
      { \siunitx_number_process:NN \l_@@_real_tl \l_@@_real_tl }
    \siunitx_number_process:NN \l_@@_img_tl \l_@@_img_tl
    \fp_set:Nn \l_@@_tmp_fp
     { \exp_after:wN \@@_extract_exp:nnnnnnn \l_@@_img_tl }
    \@@_drop_exp:N \l_@@_real_tl
    \@@_drop_exp:N \l_@@_img_tl
    \siunitx_unit_format_combine_exponent:nnN {#1}
      \l_@@_tmp_fp \l_@@_unit_tl
  }
\cs_new_protected:cpx { @@_format_cartesian_extract-exponent:n } #1
  {
    \exp_not:N \siunitx_unit_format_extract_prefixes:nNN {#1}
      \exp_not:N \l_@@_unit_tl \exp_not:N \l_@@_tmp_fp
    \exp_not:c { @@_format_extract-exponent:N }
      \exp_not:N \l_@@_img_tl
    \exp_not:N \tl_if_empty:NF \exp_not:N \l_@@_real_tl
      {
        \exp_not:c { @@_format_extract-exponent:N }
          \exp_not:N \l_@@_real_tl
      }
  }
\cs_new_protected:Npn \@@_format_cartesian_input:n #1
  {
    \siunitx_number_process:NN \l_@@_real_tl \l_@@_real_tl
    \siunitx_number_process:NN \l_@@_img_tl \l_@@_img_tl
    \siunitx_unit_format:nN {#1} \l_@@_unit_tl
  }
\cs_new_protected:cpn { @@_format_extract-exponent:N } #1
  {
    \tl_set:Nx #1
      { \siunitx_number_adjust_exponent:Nn #1 \l_@@_tmp_fp }
    \siunitx_number_process:NN #1 #1
  }
\cs_new:Npn \@@_extract_exp:nnnnnnn #1#2#3#4#5#6#7 { #6#7 }
\cs_new_protected:Npn \@@_drop_exp:N #1
  { \exp_after:wN \@@_drop_exp:nnnnnnnN #1 #1 }
\cs_new_protected:Npn \@@_drop_exp:nnnnnnnN #1#2#3#4#5#6#7#8
  { \tl_set:Nn #8 { {#1} {#2} {#3} {#4} {#5} { } { 0 } } }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}
%   {
%     \@@_format_polar:n            ,
%     \@@_format_combine-exponent:n ,
%     \@@_format_extract-exponent:n ,
%     \@@_format_polar_input:n
%   }
%     We see similar ideas here to the Cartesian versions, but with only
%     the magnitude to adjust, things are rather simpler in the exponent
%     manipulations.
%    \begin{macrocode}
\cs_new_protected:Npn \@@_format_polar:n #1
  {
    \tl_if_blank:nTF {#1}
      { \siunitx_number_process:NN \l_@@_mag_tl \l_@@_mag_tl }
      {
        \use:c
          { @@_format_polar_ \l_siunitx_quantity_prefix_mode_tl :n } {#1}
      }
    \tl_set:Nx \l_@@_tmp_tl
      {
        \siunitx_number_output:N \l_@@_mag_tl
        \exp_not:V \l_@@_symbol_angle_tl
        \mathord % TEMP
        \exp_not:V \l_@@_angle_tl
      }
    \siunitx_print_number:V \l_@@_tmp_tl
    \bool_if:NT \l_@@_polar_degree_bool
      {
        \exp_args:NV \siunitx_unit_format:nN \l_@@_symbol_degree_tl \l_@@_tmp_tl
        \nobreak
        \siunitx_print_unit:V \l_@@_tmp_tl
      }
    \siunitx_quantity_print:nV { } \l_@@_unit_tl
  }
\cs_new_protected:cpn { @@_format_polar_combine-exponent:n } #1
  {
    \siunitx_number_process:NN \l_@@_mag_tl \l_@@_mag_tl
    \fp_set:Nn \l_@@_tmp_fp
     { \exp_after:wN \@@_extract_exp:nnnnnnn \l_@@_mag_tl }
    \@@_drop_exp:N \l_@@_mag_tl
    \siunitx_unit_format_combine_exponent:nnN {#1}
      \l_@@_tmp_fp \l_@@_unit_tl
  }
\cs_new_protected:cpx { @@_format_polar_extract-exponent:n } #1
  {
    \exp_not:N \siunitx_unit_format_extract_prefixes:nNN {#1}
      \exp_not:N \l_@@_unit_tl \exp_not:N \l_@@_tmp_fp
    \exp_not:c { @@_format_extract-exponent:N }
      \exp_not:N \l_@@_mag_tl
  }
\cs_new_protected:Npn \@@_format_polar_input:n #1
  {
    \siunitx_number_process:NN \l_@@_mag_tl \l_@@_mag_tl
    \siunitx_unit_format:nN {#1} \l_@@_unit_tl
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Conversion}
%
% \begin{macro}[EXP]
%   {\@@_convert_cartesian:nn, \@@_convert_cartesian_aux:nn}
% \begin{macro}[EXP]{\@@_convert_cartesian_aux:w}
%   Conversion to Cartesian form is easy as we have two inputs and need to
%   do no parsing here at all.
%    \begin{macrocode}
\cs_new:Npn \@@_convert_cartesian:nn #1#2
  {
    \exp_args:Nee \@@_convert_cartesian_aux:nn
      {
        \fp_to_tl:n
          { (#1) * cos \bool_if:NT \l_@@_polar_degree_bool { d } (#2) }
      }
      {
        \fp_to_tl:n
          { (#1) * sin \bool_if:NT \l_@@_polar_degree_bool { d } (#2) }
      }
  }
\cs_new:Npn \@@_convert_cartesian_aux:nn #1#2
  {
    \@@_convert_cartesian_aux:w #1 e e \q_mark #2 e e \q_stop
  }
\cs_new:Npn \@@_convert_cartesian_aux:w
  #1 e #2 e #3 \q_mark #4 e #5 e #6 \q_stop
  {
    \fp_compare:nNnF {#1} = \c_zero_fp
      {#1}
    \fp_compare:nNnF {#4} = \c_zero_fp
      {
        \fp_compare:nNnF {#4} < \c_zero_fp { + }
        #4 i
      }
    \tl_if_blank:nF {#2}
      { e #2 }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_convert_polar:n}
% \begin{macro}[EXP]{\@@_convert_polar_auxi:w}
% \begin{macro}[EXP]{\@@_convert_polar_auxii:nw}
% \begin{macro}[EXP]{\@@_convert_polar_auxiii:nnw}
% \begin{macro}[EXP]{\@@_convert_polar_auxiv:nnw}
% \begin{macro}[EXP]{\@@_convert_polar_auxv:nnw}
% \begin{macro}[EXP]{\@@_convert_polar_auxvi:nnn}
% \begin{macro}[EXP]{\@@_convert_polar_auxvii:nnn}
% \begin{macro}[EXP]{\@@_convert_polar_auxviii:nn}
%    A simplified parser for complex numbers which works by expansion,
%    then converts to polar form.
%    \begin{macrocode}
\cs_new:Npn \@@_convert_polar:n #1
  { \@@_convert_polar_auxi:w #1 e e \q_stop }
\cs_new:Npn \@@_convert_polar_auxi:w #1 e #2 e #3 \q_stop
  { \@@_convert_polar_auxii:nw {#2} #1 \q_stop }
\cs_new:Npn \@@_convert_polar_auxii:nw #1#2#3 \q_stop
  {
    \bool_lazy_or:nnTF
      { \str_if_eq_p:nn {#2} { i } }
      { \str_if_eq_p:nn {#2#3} { +i } }
      { \@@_convert_polar_auxvi:nnn { } { 1 } {#1} }
      {
        \str_if_eq:nnTF {#2#3} { -i }
          { \@@_convert_polar_auxvi:nnn { } { -1 } {#1} }
          { \@@_convert_polar_auxiii:nnw {#1} {#2} #3 + + \q_stop }
      }
  }
\cs_new:Npn \@@_convert_polar_auxiii:nnw #1#2#3 + #4 + #5 \q_stop
  {
    \tl_if_blank:nTF {#4}
      { \@@_convert_polar_auxiv:nnw {#1} {#2} #3 - - \q_stop }
      {
        \str_if_eq:nnTF {#4} { i }
          { \@@_convert_polar_auxvi:nnn {#2#3} { 1 } {#1} }
          { \@@_convert_polar_auxv:nnw {#2#3} {#1} #4 i \q_nil i \q_stop }
      }
  }
\cs_new:Npn \@@_convert_polar_auxiv:nnw #1#2#3 - #4 - #5 \q_stop
  {
    \tl_if_blank:nTF {#4}
      { \@@_convert_polar_auxv:nnw { } {#1} #2#3 i \q_nil i \q_stop }
      {
        \str_if_eq:nnTF {#4} { i }
          { \@@_convert_polar_auxvi:nnn { } { -1 } {#1} }
          { \@@_convert_polar_auxv:nnw {#2#3} {#1} -#4 i \q_nil i \q_stop }
      }
  }
\cs_new:Npn \@@_convert_polar_auxv:nnw #1#2#3 i #4 i #5 \q_stop
  {
    \quark_if_nil:nTF {#4}
      { { #3 \tl_if_blank:nF {#2} { e#2 } } { 0 } }
      { \@@_convert_polar_auxvi:nnn {#1} {#3} {#2} }
  }
\cs_new:Npn \@@_convert_polar_auxvi:nnn #1#2#3
  {
    \exp_args:Neee \@@_convert_polar_auxvii:nnn
      { \tl_if_blank:nTF {#1} { 0 } {#1} }
      { \tl_if_blank:nTF {#2} { 0 } {#2} }
      { \tl_if_blank:nF {#3} { e#3 } }
  }
\cs_new:Npn \@@_convert_polar_auxvii:nnn #1#2#3
  {
    \exp_args:Nee \@@_format_polar_auxviii:nn
      { \fp_eval:n { sqrt ( (#1#3)^2 + (#2#3)^2 ) } }
      {
        \fp_eval:n
          { atan \bool_if:NT \l_@@_polar_degree_bool { d } (#2 , #1) }
      }
  }
\cs_new:Npn \@@_format_polar_auxviii:nn #1#2 { {#1} {#2} }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Messages}
%
%    \begin{macrocode}
\msg_new:nnnn { siunitx } { invalid-polar-form }
  { Invalid~polar~form~"#1:#2". }
  {
    Complex~numbers~in~polar~form~must~have~both~a~magnitude~and~and~
    angle.
  }
%    \end{macrocode}
%
% \subsection{Standard settings for module options}
%
% Some of these follow naturally from the point of definition
% (\foreign{e.g.}~boolean variables are always |false| to begin with),
% but for clarity everything is set here.
%    \begin{macrocode}
\keys_set:nn { siunitx }
  {
    complex-angle-unit    = degrees       ,
    complex-mode          = input         ,
    complex-root-position = after-number  ,
    complex-symbol-angle  = \angle        ,
    complex-symbol-degree = \degree       ,
    input-complex-root    = ij            ,
    output-complex-root   = \mathrm { i } ,
    print-complex-unity   = false
  }
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex