(index<- ) ./libcore/num/f32.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Operations and constants for 32-bits floats (`f32` type)
12
13 use default::Default;
14 use intrinsics;
15 use num::{Zero, One, Bounded, Signed, Num, Primitive};
16
17 #[cfg(not(test))] use cmp::{Eq, Ord};
18 #[cfg(not(test))] use ops::{Add, Sub, Mul, Div, Rem, Neg};
19
20 pub static RADIX: uint = 2u;
21
22 pub static MANTISSA_DIGITS: uint = 24u;
23 pub static DIGITS: uint = 6u;
24
25 pub static EPSILON: f32 = 1.19209290e-07_f32;
26
27 /// Smallest finite f32 value
28 pub static MIN_VALUE: f32 = -3.40282347e+38_f32;
29 /// Smallest positive, normalized f32 value
30 pub static MIN_POS_VALUE: f32 = 1.17549435e-38_f32;
31 /// Largest finite f32 value
32 pub static MAX_VALUE: f32 = 3.40282347e+38_f32;
33
34 pub static MIN_EXP: int = -125;
35 pub static MAX_EXP: int = 128;
36
37 pub static MIN_10_EXP: int = -37;
38 pub static MAX_10_EXP: int = 38;
39
40 pub static NAN: f32 = 0.0_f32/0.0_f32;
41 pub static INFINITY: f32 = 1.0_f32/0.0_f32;
42 pub static NEG_INFINITY: f32 = -1.0_f32/0.0_f32;
43
44 /// Various useful constants.
45 pub mod consts {
46 // FIXME: replace with mathematical constants from cmath.
47
48 // FIXME(#5527): These constants should be deprecated once associated
49 // constants are implemented in favour of referencing the respective members
50 // of `Float`.
51
52 /// Archimedes' constant
53 pub static PI: f32 = 3.14159265358979323846264338327950288_f32;
54
55 /// pi * 2.0
56 pub static PI_2: f32 = 6.28318530717958647692528676655900576_f32;
57
58 /// pi/2.0
59 pub static FRAC_PI_2: f32 = 1.57079632679489661923132169163975144_f32;
60
61 /// pi/3.0
62 pub static FRAC_PI_3: f32 = 1.04719755119659774615421446109316763_f32;
63
64 /// pi/4.0
65 pub static FRAC_PI_4: f32 = 0.785398163397448309615660845819875721_f32;
66
67 /// pi/6.0
68 pub static FRAC_PI_6: f32 = 0.52359877559829887307710723054658381_f32;
69
70 /// pi/8.0
71 pub static FRAC_PI_8: f32 = 0.39269908169872415480783042290993786_f32;
72
73 /// 1.0/pi
74 pub static FRAC_1_PI: f32 = 0.318309886183790671537767526745028724_f32;
75
76 /// 2.0/pi
77 pub static FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;
78
79 /// 2.0/sqrt(pi)
80 pub static FRAC_2_SQRTPI: f32 = 1.12837916709551257389615890312154517_f32;
81
82 /// sqrt(2.0)
83 pub static SQRT2: f32 = 1.41421356237309504880168872420969808_f32;
84
85 /// 1.0/sqrt(2.0)
86 pub static FRAC_1_SQRT2: f32 = 0.707106781186547524400844362104849039_f32;
87
88 /// Euler's number
89 pub static E: f32 = 2.71828182845904523536028747135266250_f32;
90
91 /// log2(e)
92 pub static LOG2_E: f32 = 1.44269504088896340735992468100189214_f32;
93
94 /// log10(e)
95 pub static LOG10_E: f32 = 0.434294481903251827651128918916605082_f32;
96
97 /// ln(2.0)
98 pub static LN_2: f32 = 0.693147180559945309417232121458176568_f32;
99
100 /// ln(10.0)
101 pub static LN_10: f32 = 2.30258509299404568401799145468436421_f32;
102 }
103
104 #[cfg(not(test))]
105 impl Ord for f32 {
106 #[inline]
107 fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
108 #[inline]
109 fn le(&self, other: &f32) -> bool { (*self) <= (*other) }
110 #[inline]
111 fn ge(&self, other: &f32) -> bool { (*self) >= (*other) }
112 #[inline]
113 fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
114 }
115 #[cfg(not(test))]
116 impl Eq for f32 {
117 #[inline]
118 fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
119 }
120
121 impl Num for f32 {}
122
123 impl Default for f32 {
124 #[inline]
125 fn default() -> f32 { 0.0 }
126 }
127
128 impl Primitive for f32 {}
129
130 impl Zero for f32 {
131 #[inline]
132 fn zero() -> f32 { 0.0 }
133
134 /// Returns true if the number is equal to either `0.0` or `-0.0`
135 #[inline]
136 fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
137 }
138
139 impl One for f32 {
140 #[inline]
141 fn one() -> f32 { 1.0 }
142 }
143
144 #[cfg(not(test))]
145 impl Add<f32,f32> for f32 {
146 #[inline]
147 fn add(&self, other: &f32) -> f32 { *self + *other }
148 }
149
150 #[cfg(not(test))]
151 impl Sub<f32,f32> for f32 {
152 #[inline]
153 fn sub(&self, other: &f32) -> f32 { *self - *other }
154 }
155
156 #[cfg(not(test))]
157 impl Mul<f32,f32> for f32 {
158 #[inline]
159 fn mul(&self, other: &f32) -> f32 { *self * *other }
160 }
161
162 #[cfg(not(test))]
163 impl Div<f32,f32> for f32 {
164 #[inline]
165 fn div(&self, other: &f32) -> f32 { *self / *other }
166 }
167
168 #[cfg(not(test))]
169 impl Rem<f32,f32> for f32 {
170 #[inline]
171 fn rem(&self, other: &f32) -> f32 {
172 extern { fn fmodf(a: f32, b: f32) -> f32; }
173 unsafe { fmodf(*self, *other) }
174 }
175 }
176
177 #[cfg(not(test))]
178 impl Neg<f32> for f32 {
179 #[inline]
180 fn neg(&self) -> f32 { -*self }
181 }
182
183 impl Signed for f32 {
184 /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
185 #[inline]
186 fn abs(&self) -> f32 {
187 unsafe { intrinsics::fabsf32(*self) }
188 }
189
190 /// The positive difference of two numbers. Returns `0.0` if the number is
191 /// less than or equal to `other`, otherwise the difference between`self`
192 /// and `other` is returned.
193 #[inline]
194 fn abs_sub(&self, other: &f32) -> f32 {
195 extern { fn fdimf(a: f32, b: f32) -> f32; }
196 unsafe { fdimf(*self, *other) }
197 }
198
199 /// # Returns
200 ///
201 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
202 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
203 /// - `NAN` if the number is NaN
204 #[inline]
205 fn signum(&self) -> f32 {
206 if self != self { NAN } else {
207 unsafe { intrinsics::copysignf32(1.0, *self) }
208 }
209 }
210
211 /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
212 #[inline]
213 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == INFINITY }
214
215 /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
216 #[inline]
217 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
218 }
219
220 impl Bounded for f32 {
221 // NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
222 #[inline]
223 fn min_value() -> f32 { -MAX_VALUE }
224
225 #[inline]
226 fn max_value() -> f32 { MAX_VALUE }
227 }