]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/f32.rs
libstd: set baseline stability levels.
[rust.git] / src / libstd / num / f32.rs
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 #![experimental]
14 #![allow(missing_doc)]
15 #![allow(unsigned_negate)]
16 #![doc(primitive = "f32")]
17
18 use prelude::*;
19
20 use from_str::FromStr;
21 use intrinsics;
22 use libc::c_int;
23 use num::strconv;
24 use num;
25 use string::String;
26
27 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
28 pub use core::f32::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP};
29 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
30 pub use core::f32::consts;
31
32 #[allow(dead_code)]
33 mod cmath {
34     use libc::{c_float, c_int};
35
36     #[link_name = "m"]
37     extern {
38         pub fn acosf(n: c_float) -> c_float;
39         pub fn asinf(n: c_float) -> c_float;
40         pub fn atanf(n: c_float) -> c_float;
41         pub fn atan2f(a: c_float, b: c_float) -> c_float;
42         pub fn cbrtf(n: c_float) -> c_float;
43         pub fn coshf(n: c_float) -> c_float;
44         pub fn erff(n: c_float) -> c_float;
45         pub fn erfcf(n: c_float) -> c_float;
46         pub fn expm1f(n: c_float) -> c_float;
47         pub fn fdimf(a: c_float, b: c_float) -> c_float;
48         pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
49         pub fn fmaxf(a: c_float, b: c_float) -> c_float;
50         pub fn fminf(a: c_float, b: c_float) -> c_float;
51         pub fn fmodf(a: c_float, b: c_float) -> c_float;
52         pub fn nextafterf(x: c_float, y: c_float) -> c_float;
53         pub fn hypotf(x: c_float, y: c_float) -> c_float;
54         pub fn ldexpf(x: c_float, n: c_int) -> c_float;
55         pub fn logbf(n: c_float) -> c_float;
56         pub fn log1pf(n: c_float) -> c_float;
57         pub fn ilogbf(n: c_float) -> c_int;
58         pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
59         pub fn sinhf(n: c_float) -> c_float;
60         pub fn tanf(n: c_float) -> c_float;
61         pub fn tanhf(n: c_float) -> c_float;
62         pub fn tgammaf(n: c_float) -> c_float;
63
64         #[cfg(unix)]
65         pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
66
67         #[cfg(windows)]
68         #[link_name="__lgammaf_r"]
69         pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
70     }
71 }
72
73 impl FloatMath for f32 {
74     /// Constructs a floating point number by multiplying `x` by 2 raised to the
75     /// power of `exp`
76     #[inline]
77     fn ldexp(x: f32, exp: int) -> f32 {
78         unsafe { cmath::ldexpf(x, exp as c_int) }
79     }
80
81     /// Breaks the number into a normalized fraction and a base-2 exponent,
82     /// satisfying:
83     ///
84     /// - `self = x * pow(2, exp)`
85     /// - `0.5 <= abs(x) < 1.0`
86     #[inline]
87     fn frexp(self) -> (f32, int) {
88         unsafe {
89             let mut exp = 0;
90             let x = cmath::frexpf(self, &mut exp);
91             (x, exp as int)
92         }
93     }
94
95     /// Returns the next representable floating-point value in the direction of
96     /// `other`.
97     #[inline]
98     fn next_after(self, other: f32) -> f32 {
99         unsafe { cmath::nextafterf(self, other) }
100     }
101
102     #[inline]
103     fn max(self, other: f32) -> f32 {
104         unsafe { cmath::fmaxf(self, other) }
105     }
106
107     #[inline]
108     fn min(self, other: f32) -> f32 {
109         unsafe { cmath::fminf(self, other) }
110     }
111
112     #[inline]
113     fn cbrt(self) -> f32 {
114         unsafe { cmath::cbrtf(self) }
115     }
116
117     #[inline]
118     fn hypot(self, other: f32) -> f32 {
119         unsafe { cmath::hypotf(self, other) }
120     }
121
122     #[inline]
123     fn sin(self) -> f32 {
124         unsafe { intrinsics::sinf32(self) }
125     }
126
127     #[inline]
128     fn cos(self) -> f32 {
129         unsafe { intrinsics::cosf32(self) }
130     }
131
132     #[inline]
133     fn tan(self) -> f32 {
134         unsafe { cmath::tanf(self) }
135     }
136
137     #[inline]
138     fn asin(self) -> f32 {
139         unsafe { cmath::asinf(self) }
140     }
141
142     #[inline]
143     fn acos(self) -> f32 {
144         unsafe { cmath::acosf(self) }
145     }
146
147     #[inline]
148     fn atan(self) -> f32 {
149         unsafe { cmath::atanf(self) }
150     }
151
152     #[inline]
153     fn atan2(self, other: f32) -> f32 {
154         unsafe { cmath::atan2f(self, other) }
155     }
156
157     /// Simultaneously computes the sine and cosine of the number
158     #[inline]
159     fn sin_cos(self) -> (f32, f32) {
160         (self.sin(), self.cos())
161     }
162
163     /// Returns the exponential of the number, minus `1`, in a way that is
164     /// accurate even if the number is close to zero
165     #[inline]
166     fn exp_m1(self) -> f32 {
167         unsafe { cmath::expm1f(self) }
168     }
169
170     /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
171     /// accurately than if the operations were performed separately
172     #[inline]
173     fn ln_1p(self) -> f32 {
174         unsafe { cmath::log1pf(self) }
175     }
176
177     #[inline]
178     fn sinh(self) -> f32 {
179         unsafe { cmath::sinhf(self) }
180     }
181
182     #[inline]
183     fn cosh(self) -> f32 {
184         unsafe { cmath::coshf(self) }
185     }
186
187     #[inline]
188     fn tanh(self) -> f32 {
189         unsafe { cmath::tanhf(self) }
190     }
191
192     /// Inverse hyperbolic sine
193     ///
194     /// # Returns
195     ///
196     /// - on success, the inverse hyperbolic sine of `self` will be returned
197     /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
198     /// - `NAN` if `self` is `NAN`
199     #[inline]
200     fn asinh(self) -> f32 {
201         match self {
202             NEG_INFINITY => NEG_INFINITY,
203             x => (x + ((x * x) + 1.0).sqrt()).ln(),
204         }
205     }
206
207     /// Inverse hyperbolic cosine
208     ///
209     /// # Returns
210     ///
211     /// - on success, the inverse hyperbolic cosine of `self` will be returned
212     /// - `INFINITY` if `self` is `INFINITY`
213     /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
214     #[inline]
215     fn acosh(self) -> f32 {
216         match self {
217             x if x < 1.0 => Float::nan(),
218             x => (x + ((x * x) - 1.0).sqrt()).ln(),
219         }
220     }
221
222     /// Inverse hyperbolic tangent
223     ///
224     /// # Returns
225     ///
226     /// - on success, the inverse hyperbolic tangent of `self` will be returned
227     /// - `self` if `self` is `0.0` or `-0.0`
228     /// - `INFINITY` if `self` is `1.0`
229     /// - `NEG_INFINITY` if `self` is `-1.0`
230     /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
231     ///   (including `INFINITY` and `NEG_INFINITY`)
232     #[inline]
233     fn atanh(self) -> f32 {
234         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
235     }
236 }
237
238 //
239 // Section: String Conversions
240 //
241
242 /// Converts a float to a string
243 ///
244 /// # Arguments
245 ///
246 /// * num - The float value
247 #[inline]
248 pub fn to_str(num: f32) -> String {
249     let (r, _) = strconv::float_to_str_common(
250         num, 10u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
251     r
252 }
253
254 /// Converts a float to a string in hexadecimal format
255 ///
256 /// # Arguments
257 ///
258 /// * num - The float value
259 #[inline]
260 pub fn to_str_hex(num: f32) -> String {
261     let (r, _) = strconv::float_to_str_common(
262         num, 16u, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
263     r
264 }
265
266 /// Converts a float to a string in a given radix, and a flag indicating
267 /// whether it's a special value
268 ///
269 /// # Arguments
270 ///
271 /// * num - The float value
272 /// * radix - The base to use
273 #[inline]
274 pub fn to_str_radix_special(num: f32, rdx: uint) -> (String, bool) {
275     strconv::float_to_str_common(num, rdx, true,
276                            strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false)
277 }
278
279 /// Converts a float to a string with exactly the number of
280 /// provided significant digits
281 ///
282 /// # Arguments
283 ///
284 /// * num - The float value
285 /// * digits - The number of significant digits
286 #[inline]
287 pub fn to_str_exact(num: f32, dig: uint) -> String {
288     let (r, _) = strconv::float_to_str_common(
289         num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpNone, false);
290     r
291 }
292
293 /// Converts a float to a string with a maximum number of
294 /// significant digits
295 ///
296 /// # Arguments
297 ///
298 /// * num - The float value
299 /// * digits - The number of significant digits
300 #[inline]
301 pub fn to_str_digits(num: f32, dig: uint) -> String {
302     let (r, _) = strconv::float_to_str_common(
303         num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpNone, false);
304     r
305 }
306
307 /// Converts a float to a string using the exponential notation with exactly the number of
308 /// provided digits after the decimal point in the significand
309 ///
310 /// # Arguments
311 ///
312 /// * num - The float value
313 /// * digits - The number of digits after the decimal point
314 /// * upper - Use `E` instead of `e` for the exponent sign
315 #[inline]
316 pub fn to_str_exp_exact(num: f32, dig: uint, upper: bool) -> String {
317     let (r, _) = strconv::float_to_str_common(
318         num, 10u, true, strconv::SignNeg, strconv::DigExact(dig), strconv::ExpDec, upper);
319     r
320 }
321
322 /// Converts a float to a string using the exponential notation with the maximum number of
323 /// digits after the decimal point in the significand
324 ///
325 /// # Arguments
326 ///
327 /// * num - The float value
328 /// * digits - The number of digits after the decimal point
329 /// * upper - Use `E` instead of `e` for the exponent sign
330 #[inline]
331 pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> String {
332     let (r, _) = strconv::float_to_str_common(
333         num, 10u, true, strconv::SignNeg, strconv::DigMax(dig), strconv::ExpDec, upper);
334     r
335 }
336
337 impl num::ToStrRadix for f32 {
338     /// Converts a float to a string in a given radix
339     ///
340     /// # Arguments
341     ///
342     /// * num - The float value
343     /// * radix - The base to use
344     ///
345     /// # Failure
346     ///
347     /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
348     /// possible misinterpretation of the result at higher bases. If those values
349     /// are expected, use `to_str_radix_special()` instead.
350     #[inline]
351     fn to_str_radix(&self, rdx: uint) -> String {
352         let (r, special) = strconv::float_to_str_common(
353             *self, rdx, true, strconv::SignNeg, strconv::DigAll, strconv::ExpNone, false);
354         if special { fail!("number has a special value, \
355                             try to_str_radix_special() if those are expected") }
356         r
357     }
358 }
359
360 /// Convert a string in base 16 to a float.
361 /// Accepts an optional binary exponent.
362 ///
363 /// This function accepts strings such as
364 ///
365 /// * 'a4.fe'
366 /// * '+a4.fe', equivalent to 'a4.fe'
367 /// * '-a4.fe'
368 /// * '2b.aP128', or equivalently, '2b.ap128'
369 /// * '2b.aP-128'
370 /// * '.' (understood as 0)
371 /// * 'c.'
372 /// * '.c', or, equivalently,  '0.c'
373 /// * '+inf', 'inf', '-inf', 'NaN'
374 ///
375 /// Leading and trailing whitespace represent an error.
376 ///
377 /// # Arguments
378 ///
379 /// * num - A string
380 ///
381 /// # Return value
382 ///
383 /// `None` if the string did not represent a valid number.  Otherwise,
384 /// `Some(n)` where `n` is the floating-point number represented by `[num]`.
385 #[inline]
386 pub fn from_str_hex(num: &str) -> Option<f32> {
387     strconv::from_str_common(num, 16u, true, true, true,
388                              strconv::ExpBin, false, false)
389 }
390
391 impl FromStr for f32 {
392     /// Convert a string in base 10 to a float.
393     /// Accepts an optional decimal exponent.
394     ///
395     /// This function accepts strings such as
396     ///
397     /// * '3.14'
398     /// * '+3.14', equivalent to '3.14'
399     /// * '-3.14'
400     /// * '2.5E10', or equivalently, '2.5e10'
401     /// * '2.5E-10'
402     /// * '.' (understood as 0)
403     /// * '5.'
404     /// * '.5', or, equivalently,  '0.5'
405     /// * '+inf', 'inf', '-inf', 'NaN'
406     ///
407     /// Leading and trailing whitespace represent an error.
408     ///
409     /// # Arguments
410     ///
411     /// * num - A string
412     ///
413     /// # Return value
414     ///
415     /// `None` if the string did not represent a valid number.  Otherwise,
416     /// `Some(n)` where `n` is the floating-point number represented by `num`.
417     #[inline]
418     fn from_str(val: &str) -> Option<f32> {
419         strconv::from_str_common(val, 10u, true, true, true,
420                                  strconv::ExpDec, false, false)
421     }
422 }
423
424 impl num::FromStrRadix for f32 {
425     /// Convert a string in a given base to a float.
426     ///
427     /// Due to possible conflicts, this function does **not** accept
428     /// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
429     /// does it recognize exponents of any kind.
430     ///
431     /// Leading and trailing whitespace represent an error.
432     ///
433     /// # Arguments
434     ///
435     /// * num - A string
436     /// * radix - The base to use. Must lie in the range [2 .. 36]
437     ///
438     /// # Return value
439     ///
440     /// `None` if the string did not represent a valid number. Otherwise,
441     /// `Some(n)` where `n` is the floating-point number represented by `num`.
442     #[inline]
443     fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
444         strconv::from_str_common(val, rdx, true, true, false,
445                                  strconv::ExpNone, false, false)
446     }
447 }
448
449 #[cfg(test)]
450 mod tests {
451     use f32::*;
452     use num::*;
453     use num;
454
455     #[test]
456     fn test_min_nan() {
457         assert_eq!(NAN.min(2.0), 2.0);
458         assert_eq!(2.0f32.min(NAN), 2.0);
459     }
460
461     #[test]
462     fn test_max_nan() {
463         assert_eq!(NAN.max(2.0), 2.0);
464         assert_eq!(2.0f32.max(NAN), 2.0);
465     }
466
467     #[test]
468     fn test_num() {
469         num::test_num(10f32, 2f32);
470     }
471
472     #[test]
473     fn test_floor() {
474         assert_approx_eq!(1.0f32.floor(), 1.0f32);
475         assert_approx_eq!(1.3f32.floor(), 1.0f32);
476         assert_approx_eq!(1.5f32.floor(), 1.0f32);
477         assert_approx_eq!(1.7f32.floor(), 1.0f32);
478         assert_approx_eq!(0.0f32.floor(), 0.0f32);
479         assert_approx_eq!((-0.0f32).floor(), -0.0f32);
480         assert_approx_eq!((-1.0f32).floor(), -1.0f32);
481         assert_approx_eq!((-1.3f32).floor(), -2.0f32);
482         assert_approx_eq!((-1.5f32).floor(), -2.0f32);
483         assert_approx_eq!((-1.7f32).floor(), -2.0f32);
484     }
485
486     #[test]
487     fn test_ceil() {
488         assert_approx_eq!(1.0f32.ceil(), 1.0f32);
489         assert_approx_eq!(1.3f32.ceil(), 2.0f32);
490         assert_approx_eq!(1.5f32.ceil(), 2.0f32);
491         assert_approx_eq!(1.7f32.ceil(), 2.0f32);
492         assert_approx_eq!(0.0f32.ceil(), 0.0f32);
493         assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
494         assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
495         assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
496         assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
497         assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
498     }
499
500     #[test]
501     fn test_round() {
502         assert_approx_eq!(1.0f32.round(), 1.0f32);
503         assert_approx_eq!(1.3f32.round(), 1.0f32);
504         assert_approx_eq!(1.5f32.round(), 2.0f32);
505         assert_approx_eq!(1.7f32.round(), 2.0f32);
506         assert_approx_eq!(0.0f32.round(), 0.0f32);
507         assert_approx_eq!((-0.0f32).round(), -0.0f32);
508         assert_approx_eq!((-1.0f32).round(), -1.0f32);
509         assert_approx_eq!((-1.3f32).round(), -1.0f32);
510         assert_approx_eq!((-1.5f32).round(), -2.0f32);
511         assert_approx_eq!((-1.7f32).round(), -2.0f32);
512     }
513
514     #[test]
515     fn test_trunc() {
516         assert_approx_eq!(1.0f32.trunc(), 1.0f32);
517         assert_approx_eq!(1.3f32.trunc(), 1.0f32);
518         assert_approx_eq!(1.5f32.trunc(), 1.0f32);
519         assert_approx_eq!(1.7f32.trunc(), 1.0f32);
520         assert_approx_eq!(0.0f32.trunc(), 0.0f32);
521         assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
522         assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
523         assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
524         assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
525         assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
526     }
527
528     #[test]
529     fn test_fract() {
530         assert_approx_eq!(1.0f32.fract(), 0.0f32);
531         assert_approx_eq!(1.3f32.fract(), 0.3f32);
532         assert_approx_eq!(1.5f32.fract(), 0.5f32);
533         assert_approx_eq!(1.7f32.fract(), 0.7f32);
534         assert_approx_eq!(0.0f32.fract(), 0.0f32);
535         assert_approx_eq!((-0.0f32).fract(), -0.0f32);
536         assert_approx_eq!((-1.0f32).fract(), -0.0f32);
537         assert_approx_eq!((-1.3f32).fract(), -0.3f32);
538         assert_approx_eq!((-1.5f32).fract(), -0.5f32);
539         assert_approx_eq!((-1.7f32).fract(), -0.7f32);
540     }
541
542     #[test]
543     fn test_asinh() {
544         assert_eq!(0.0f32.asinh(), 0.0f32);
545         assert_eq!((-0.0f32).asinh(), -0.0f32);
546
547         let inf: f32 = Float::infinity();
548         let neg_inf: f32 = Float::neg_infinity();
549         let nan: f32 = Float::nan();
550         assert_eq!(inf.asinh(), inf);
551         assert_eq!(neg_inf.asinh(), neg_inf);
552         assert!(nan.asinh().is_nan());
553         assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
554         assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
555     }
556
557     #[test]
558     fn test_acosh() {
559         assert_eq!(1.0f32.acosh(), 0.0f32);
560         assert!(0.999f32.acosh().is_nan());
561
562         let inf: f32 = Float::infinity();
563         let neg_inf: f32 = Float::neg_infinity();
564         let nan: f32 = Float::nan();
565         assert_eq!(inf.acosh(), inf);
566         assert!(neg_inf.acosh().is_nan());
567         assert!(nan.acosh().is_nan());
568         assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
569         assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
570     }
571
572     #[test]
573     fn test_atanh() {
574         assert_eq!(0.0f32.atanh(), 0.0f32);
575         assert_eq!((-0.0f32).atanh(), -0.0f32);
576
577         let inf32: f32 = Float::infinity();
578         let neg_inf32: f32 = Float::neg_infinity();
579         assert_eq!(1.0f32.atanh(), inf32);
580         assert_eq!((-1.0f32).atanh(), neg_inf32);
581
582         assert!(2f64.atanh().atanh().is_nan());
583         assert!((-2f64).atanh().atanh().is_nan());
584
585         let inf64: f32 = Float::infinity();
586         let neg_inf64: f32 = Float::neg_infinity();
587         let nan32: f32 = Float::nan();
588         assert!(inf64.atanh().is_nan());
589         assert!(neg_inf64.atanh().is_nan());
590         assert!(nan32.atanh().is_nan());
591
592         assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
593         assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
594     }
595
596     #[test]
597     fn test_real_consts() {
598         let pi: f32 = Float::pi();
599         let two_pi: f32 = Float::two_pi();
600         let frac_pi_2: f32 = Float::frac_pi_2();
601         let frac_pi_3: f32 = Float::frac_pi_3();
602         let frac_pi_4: f32 = Float::frac_pi_4();
603         let frac_pi_6: f32 = Float::frac_pi_6();
604         let frac_pi_8: f32 = Float::frac_pi_8();
605         let frac_1_pi: f32 = Float::frac_1_pi();
606         let frac_2_pi: f32 = Float::frac_2_pi();
607         let frac_2_sqrtpi: f32 = Float::frac_2_sqrtpi();
608         let sqrt2: f32 = Float::sqrt2();
609         let frac_1_sqrt2: f32 = Float::frac_1_sqrt2();
610         let e: f32 = Float::e();
611         let log2_e: f32 = Float::log2_e();
612         let log10_e: f32 = Float::log10_e();
613         let ln_2: f32 = Float::ln_2();
614         let ln_10: f32 = Float::ln_10();
615
616         assert_approx_eq!(two_pi, 2f32 * pi);
617         assert_approx_eq!(frac_pi_2, pi / 2f32);
618         assert_approx_eq!(frac_pi_3, pi / 3f32);
619         assert_approx_eq!(frac_pi_4, pi / 4f32);
620         assert_approx_eq!(frac_pi_6, pi / 6f32);
621         assert_approx_eq!(frac_pi_8, pi / 8f32);
622         assert_approx_eq!(frac_1_pi, 1f32 / pi);
623         assert_approx_eq!(frac_2_pi, 2f32 / pi);
624         assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
625         assert_approx_eq!(sqrt2, 2f32.sqrt());
626         assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
627         assert_approx_eq!(log2_e, e.log2());
628         assert_approx_eq!(log10_e, e.log10());
629         assert_approx_eq!(ln_2, 2f32.ln());
630         assert_approx_eq!(ln_10, 10f32.ln());
631     }
632
633     #[test]
634     pub fn test_abs() {
635         assert_eq!(INFINITY.abs(), INFINITY);
636         assert_eq!(1f32.abs(), 1f32);
637         assert_eq!(0f32.abs(), 0f32);
638         assert_eq!((-0f32).abs(), 0f32);
639         assert_eq!((-1f32).abs(), 1f32);
640         assert_eq!(NEG_INFINITY.abs(), INFINITY);
641         assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
642         assert!(NAN.abs().is_nan());
643     }
644
645     #[test]
646     fn test_abs_sub() {
647         assert_eq!((-1f32).abs_sub(&1f32), 0f32);
648         assert_eq!(1f32.abs_sub(&1f32), 0f32);
649         assert_eq!(1f32.abs_sub(&0f32), 1f32);
650         assert_eq!(1f32.abs_sub(&-1f32), 2f32);
651         assert_eq!(NEG_INFINITY.abs_sub(&0f32), 0f32);
652         assert_eq!(INFINITY.abs_sub(&1f32), INFINITY);
653         assert_eq!(0f32.abs_sub(&NEG_INFINITY), INFINITY);
654         assert_eq!(0f32.abs_sub(&INFINITY), 0f32);
655     }
656
657     #[test]
658     fn test_abs_sub_nowin() {
659         assert!(NAN.abs_sub(&-1f32).is_nan());
660         assert!(1f32.abs_sub(&NAN).is_nan());
661     }
662
663     #[test]
664     fn test_signum() {
665         assert_eq!(INFINITY.signum(), 1f32);
666         assert_eq!(1f32.signum(), 1f32);
667         assert_eq!(0f32.signum(), 1f32);
668         assert_eq!((-0f32).signum(), -1f32);
669         assert_eq!((-1f32).signum(), -1f32);
670         assert_eq!(NEG_INFINITY.signum(), -1f32);
671         assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
672         assert!(NAN.signum().is_nan());
673     }
674
675     #[test]
676     fn test_is_positive() {
677         assert!(INFINITY.is_positive());
678         assert!(1f32.is_positive());
679         assert!(0f32.is_positive());
680         assert!(!(-0f32).is_positive());
681         assert!(!(-1f32).is_positive());
682         assert!(!NEG_INFINITY.is_positive());
683         assert!(!(1f32/NEG_INFINITY).is_positive());
684         assert!(!NAN.is_positive());
685     }
686
687     #[test]
688     fn test_is_negative() {
689         assert!(!INFINITY.is_negative());
690         assert!(!1f32.is_negative());
691         assert!(!0f32.is_negative());
692         assert!((-0f32).is_negative());
693         assert!((-1f32).is_negative());
694         assert!(NEG_INFINITY.is_negative());
695         assert!((1f32/NEG_INFINITY).is_negative());
696         assert!(!NAN.is_negative());
697     }
698
699     #[test]
700     fn test_is_normal() {
701         let nan: f32 = Float::nan();
702         let inf: f32 = Float::infinity();
703         let neg_inf: f32 = Float::neg_infinity();
704         let zero: f32 = Zero::zero();
705         let neg_zero: f32 = Float::neg_zero();
706         assert!(!nan.is_normal());
707         assert!(!inf.is_normal());
708         assert!(!neg_inf.is_normal());
709         assert!(!zero.is_normal());
710         assert!(!neg_zero.is_normal());
711         assert!(1f32.is_normal());
712         assert!(1e-37f32.is_normal());
713         assert!(!1e-38f32.is_normal());
714     }
715
716     #[test]
717     fn test_classify() {
718         let nan: f32 = Float::nan();
719         let inf: f32 = Float::infinity();
720         let neg_inf: f32 = Float::neg_infinity();
721         let zero: f32 = Zero::zero();
722         let neg_zero: f32 = Float::neg_zero();
723         assert_eq!(nan.classify(), FPNaN);
724         assert_eq!(inf.classify(), FPInfinite);
725         assert_eq!(neg_inf.classify(), FPInfinite);
726         assert_eq!(zero.classify(), FPZero);
727         assert_eq!(neg_zero.classify(), FPZero);
728         assert_eq!(1f32.classify(), FPNormal);
729         assert_eq!(1e-37f32.classify(), FPNormal);
730         assert_eq!(1e-38f32.classify(), FPSubnormal);
731     }
732
733     #[test]
734     fn test_ldexp() {
735         // We have to use from_str until base-2 exponents
736         // are supported in floating-point literals
737         let f1: f32 = from_str_hex("1p-123").unwrap();
738         let f2: f32 = from_str_hex("1p-111").unwrap();
739         assert_eq!(FloatMath::ldexp(1f32, -123), f1);
740         assert_eq!(FloatMath::ldexp(1f32, -111), f2);
741
742         assert_eq!(FloatMath::ldexp(0f32, -123), 0f32);
743         assert_eq!(FloatMath::ldexp(-0f32, -123), -0f32);
744
745         let inf: f32 = Float::infinity();
746         let neg_inf: f32 = Float::neg_infinity();
747         let nan: f32 = Float::nan();
748         assert_eq!(FloatMath::ldexp(inf, -123), inf);
749         assert_eq!(FloatMath::ldexp(neg_inf, -123), neg_inf);
750         assert!(FloatMath::ldexp(nan, -123).is_nan());
751     }
752
753     #[test]
754     fn test_frexp() {
755         // We have to use from_str until base-2 exponents
756         // are supported in floating-point literals
757         let f1: f32 = from_str_hex("1p-123").unwrap();
758         let f2: f32 = from_str_hex("1p-111").unwrap();
759         let (x1, exp1) = f1.frexp();
760         let (x2, exp2) = f2.frexp();
761         assert_eq!((x1, exp1), (0.5f32, -122));
762         assert_eq!((x2, exp2), (0.5f32, -110));
763         assert_eq!(FloatMath::ldexp(x1, exp1), f1);
764         assert_eq!(FloatMath::ldexp(x2, exp2), f2);
765
766         assert_eq!(0f32.frexp(), (0f32, 0));
767         assert_eq!((-0f32).frexp(), (-0f32, 0));
768     }
769
770     #[test] #[ignore(cfg(windows))] // FIXME #8755
771     fn test_frexp_nowin() {
772         let inf: f32 = Float::infinity();
773         let neg_inf: f32 = Float::neg_infinity();
774         let nan: f32 = Float::nan();
775         assert_eq!(match inf.frexp() { (x, _) => x }, inf)
776         assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf)
777         assert!(match nan.frexp() { (x, _) => x.is_nan() })
778     }
779
780     #[test]
781     fn test_integer_decode() {
782         assert_eq!(3.14159265359f32.integer_decode(), (13176795u64, -22i16, 1i8));
783         assert_eq!((-8573.5918555f32).integer_decode(), (8779358u64, -10i16, -1i8));
784         assert_eq!(2f32.powf(100.0).integer_decode(), (8388608u64, 77i16, 1i8));
785         assert_eq!(0f32.integer_decode(), (0u64, -150i16, 1i8));
786         assert_eq!((-0f32).integer_decode(), (0u64, -150i16, -1i8));
787         assert_eq!(INFINITY.integer_decode(), (8388608u64, 105i16, 1i8));
788         assert_eq!(NEG_INFINITY.integer_decode(), (8388608u64, 105i16, -1i8));
789         assert_eq!(NAN.integer_decode(), (12582912u64, 105i16, 1i8));
790     }
791 }