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