Skip to content

Commit 92cd18c

Browse files
Kimundibrson
authored andcommitted
---
yaml --- r: 41465 b: refs/heads/snap-stage3 c: bb9c3ed h: refs/heads/master i: 41463: 24b32cb v: v3
1 parent dc4caaa commit 92cd18c

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 09bb07bed9166105ea961a42b5fff7739ae0d2e9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 05d83017ec249d5a338001acba30f7b717c7507e
4+
refs/heads/snap-stage3: bb9c3ed8764077455b6e726ba49c9845e03f4f0c
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libcore/num/num.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,93 @@ pub trait ToStrRadix {
5959

6060
pub trait FromStrRadix {
6161
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 == 0u { return _1; }
139+
if radix == 0u { 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 > 0u) {
144+
if my_pow % 2u == 1u {
145+
total *= multiplier;
146+
}
147+
my_pow /= 2u;
148+
multiplier *= multiplier;
149+
}
150+
total
62151
}

0 commit comments

Comments
 (0)