48
48
public final class NumberUtils {
49
49
50
50
/** Smallest positive number used in double comparisons (rounding). */
51
- public final static double EPSILON = 1e-6d ;
51
+ public final static double EPSILON = 1e-6 ;
52
+ /* PI and PI/2 constants */
53
+ public final static double PI = Math .PI ;
54
+ public final static double PI_HALF = PI / 2.0 ;
55
+ /** Typical scale precision in re/im comparisons (rounding). */
56
+ public static final double ARG_EPSILON = Math .ulp (1.0 );
52
57
/* shared Double instances */
53
58
/** shared Double = NaN instance */
54
59
public final static Double DBL_NAN = Double .valueOf (Double .NaN );
@@ -61,6 +66,33 @@ public final class NumberUtils {
61
66
/** scientific formatter */
62
67
private final static NumberFormat _fmtScience = new DecimalFormat ("0.0##E0" );
63
68
69
+ public static double getArgumentInDegrees (final double re , final double im ) {
70
+ return Math .toDegrees (getArgument (re , im ));
71
+ }
72
+
73
+ public static double getArgument (final double re , final double im ) {
74
+ // check |re| == 0
75
+ final double normRe = Math .abs (re );
76
+ if (normRe == 0.0 ) {
77
+ return (im >= 0.0 ) ? PI_HALF : -PI_HALF ;
78
+ }
79
+ // check |im| == 0
80
+ final double normIm = Math .abs (im );
81
+ if (normIm == 0.0 ) {
82
+ return (re >= 0.0 ) ? 0.0 : PI ;
83
+ }
84
+ final double epsilon = ARG_EPSILON * (normRe + normIm );
85
+ // check |re| << |im| or |im| << |re|
86
+ if (normIm <= epsilon ) {
87
+ return (re >= 0.0 ) ? 0.0 : PI ;
88
+ }
89
+ // check |im| << |re|
90
+ if (normRe <= epsilon ) {
91
+ return (im >= 0.0 ) ? PI_HALF : -PI_HALF ;
92
+ }
93
+ return Math .atan2 (im , re );
94
+ }
95
+
64
96
/**
65
97
* Private constructor
66
98
*/
0 commit comments