@@ -59,4 +59,93 @@ pub trait ToStrRadix {
59
59
60
60
pub trait FromStrRadix {
61
61
static pub pure fn from_str_radix ( str : & str , radix : uint ) -> Option <self >;
62
+ }
63
+
64
+ // Generic math functions:
65
+
66
+ /// Dynamically calculates the value `inf` (`1/0`).
67
+ /// Can fail on integer types.
68
+ #[ inline( always) ]
69
+ pub pure fn infinity < T : Num One Zero > ( ) -> T {
70
+ let _0: T = Zero :: zero ( ) ;
71
+ let _1: T = One :: one ( ) ;
72
+ _1 / _0
73
+ }
74
+
75
+ /// Dynamically calculates the value `-inf` (`-1/0`).
76
+ /// Can fail on integer types.
77
+ #[ inline( always) ]
78
+ pub pure fn neg_infinity < T : Num One Zero > ( ) -> T {
79
+ let _0: T = Zero :: zero ( ) ;
80
+ let _1: T = One :: one ( ) ;
81
+ - _1 / _0
82
+ }
83
+
84
+ /// Dynamically calculates the value `NaN` (`0/0`).
85
+ /// Can fail on integer types.
86
+ #[ inline( always) ]
87
+ pub pure fn NaN < T : Num Zero > ( ) -> T {
88
+ let _0: T = Zero :: zero ( ) ;
89
+ _0 / _0
90
+ }
91
+
92
+ /// Returns `true` if `num` has the value `inf` (`1/0`).
93
+ /// Can fail on integer types.
94
+ #[ inline( always) ]
95
+ pub pure fn is_infinity < T : Num One Zero Eq > ( num : & T ) -> bool {
96
+ ( * num) == ( infinity :: < T > ( ) )
97
+ }
98
+
99
+ /// Returns `true` if `num` has the value `-inf` (`-1/0`).
100
+ /// Can fail on integer types.
101
+ #[ inline( always) ]
102
+ pub pure fn is_neg_infinity < T : Num One Zero Eq > ( num : & T ) -> bool {
103
+ ( * num) == ( neg_infinity :: < T > ( ) )
104
+ }
105
+
106
+ /// Returns `true` if `num` has the value `NaN` (is not equal to itself).
107
+ #[ inline( always) ]
108
+ pub pure fn is_NaN < T : Num Eq > ( num : & T ) -> bool {
109
+ ( * num) != ( * num)
110
+ }
111
+
112
+ /// Returns `true` if `num` has the value `-0` (`1/num == -1/0`).
113
+ /// Can fail on integer types.
114
+ #[ inline( always) ]
115
+ pub pure fn is_neg_zero < T : Num One Zero Eq > ( num : & T ) -> bool {
116
+ let _1: T = One :: one ( ) ;
117
+ let _0: T = Zero :: zero ( ) ;
118
+ * num == _0 && is_neg_infinity ( & ( _1 / * num) )
119
+ }
120
+
121
+ /**
122
+ * Calculates a power to a given radix, optimized for uint `pow` and `radix`.
123
+ *
124
+ * Returns `radix^pow` as `T`.
125
+ *
126
+ * Note:
127
+ * Also returns `1` for `0^0`, despite that technically being an
128
+ * undefined number. The Reason for this is twofold:
129
+ * - If code written to use this function cares about that special case, it's
130
+ * probably going to catch it before making the call.
131
+ * - If code written to use this function doesn't care about it, it's
132
+ * probably assuming that `x^0` always equals `1`.
133
+ */
134
+ pub pure fn pow_with_uint < T : Num One Zero > ( radix : uint , pow : uint ) -> T {
135
+ let _0: T = Zero :: zero ( ) ;
136
+ let _1: T = One :: one ( ) ;
137
+
138
+ if pow == 0 u { return _1; }
139
+ if radix == 0 u { return _0; }
140
+ let mut my_pow = pow;
141
+ let mut total = _1;
142
+ let mut multiplier = Num :: from_int ( radix as int ) ;
143
+ while ( my_pow > 0 u) {
144
+ if my_pow % 2 u == 1 u {
145
+ total *= multiplier;
146
+ }
147
+ my_pow /= 2 u;
148
+ multiplier *= multiplier;
149
+ }
150
+ total
62
151
}
0 commit comments