]> git.lizzy.rs Git - rust.git/blob - src/libstd/f32.rs
Revert "Add NAN examples"
[rust.git] / src / libstd / f32.rs
1 // Copyright 2012-2015 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 //! This module provides constants which are specific to the implementation
12 //! of the `f32` floating point data type. Mathematically significant
13 //! numbers are provided in the `consts` sub-module.
14 //!
15 //! *[See also the `f32` primitive type](../primitive.f32.html).*
16
17 #![stable(feature = "rust1", since = "1.0.0")]
18 #![allow(missing_docs)]
19
20 #[cfg(not(test))]
21 use core::num;
22 #[cfg(not(test))]
23 use intrinsics;
24 #[cfg(not(test))]
25 use num::FpCategory;
26
27
28 #[stable(feature = "rust1", since = "1.0.0")]
29 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub use core::f32::consts;
38
39 #[allow(dead_code)]
40 mod cmath {
41     use libc::{c_float, c_int};
42
43     extern {
44         pub fn cbrtf(n: c_float) -> c_float;
45         pub fn erff(n: c_float) -> c_float;
46         pub fn erfcf(n: c_float) -> c_float;
47         pub fn expm1f(n: c_float) -> c_float;
48         pub fn fdimf(a: c_float, b: c_float) -> c_float;
49         pub fn fmodf(a: c_float, b: c_float) -> c_float;
50         pub fn ilogbf(n: c_float) -> c_int;
51         pub fn logbf(n: c_float) -> c_float;
52         pub fn log1pf(n: c_float) -> c_float;
53         pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
54         pub fn nextafterf(x: c_float, y: c_float) -> c_float;
55         pub fn tgammaf(n: c_float) -> c_float;
56
57         #[cfg_attr(all(windows, target_env = "msvc"), link_name = "__lgammaf_r")]
58         pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
59         #[cfg_attr(all(windows, target_env = "msvc"), link_name = "_hypotf")]
60         pub fn hypotf(x: c_float, y: c_float) -> c_float;
61     }
62
63     // See the comments in the `floor` function for why MSVC is special
64     // here.
65     #[cfg(not(target_env = "msvc"))]
66     extern {
67         pub fn acosf(n: c_float) -> c_float;
68         pub fn asinf(n: c_float) -> c_float;
69         pub fn atan2f(a: c_float, b: c_float) -> c_float;
70         pub fn atanf(n: c_float) -> c_float;
71         pub fn coshf(n: c_float) -> c_float;
72         pub fn sinhf(n: c_float) -> c_float;
73         pub fn tanf(n: c_float) -> c_float;
74         pub fn tanhf(n: c_float) -> c_float;
75     }
76
77     #[cfg(target_env = "msvc")]
78     pub use self::shims::*;
79     #[cfg(target_env = "msvc")]
80     mod shims {
81         use libc::c_float;
82
83         #[inline]
84         pub unsafe fn acosf(n: c_float) -> c_float {
85             f64::acos(n as f64) as c_float
86         }
87
88         #[inline]
89         pub unsafe fn asinf(n: c_float) -> c_float {
90             f64::asin(n as f64) as c_float
91         }
92
93         #[inline]
94         pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float {
95             f64::atan2(n as f64, b as f64) as c_float
96         }
97
98         #[inline]
99         pub unsafe fn atanf(n: c_float) -> c_float {
100             f64::atan(n as f64) as c_float
101         }
102
103         #[inline]
104         pub unsafe fn coshf(n: c_float) -> c_float {
105             f64::cosh(n as f64) as c_float
106         }
107
108         #[inline]
109         pub unsafe fn sinhf(n: c_float) -> c_float {
110             f64::sinh(n as f64) as c_float
111         }
112
113         #[inline]
114         pub unsafe fn tanf(n: c_float) -> c_float {
115             f64::tan(n as f64) as c_float
116         }
117
118         #[inline]
119         pub unsafe fn tanhf(n: c_float) -> c_float {
120             f64::tanh(n as f64) as c_float
121         }
122     }
123 }
124
125 #[cfg(not(test))]
126 #[lang = "f32"]
127 impl f32 {
128     /// Returns `true` if this value is `NaN` and false otherwise.
129     ///
130     /// ```
131     /// use std::f32;
132     ///
133     /// let nan = f32::NAN;
134     /// let f = 7.0_f32;
135     ///
136     /// assert!(nan.is_nan());
137     /// assert!(!f.is_nan());
138     /// ```
139     #[stable(feature = "rust1", since = "1.0.0")]
140     #[inline]
141     pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
142
143     /// Returns `true` if this value is positive infinity or negative infinity and
144     /// false otherwise.
145     ///
146     /// ```
147     /// use std::f32;
148     ///
149     /// let f = 7.0f32;
150     /// let inf = f32::INFINITY;
151     /// let neg_inf = f32::NEG_INFINITY;
152     /// let nan = f32::NAN;
153     ///
154     /// assert!(!f.is_infinite());
155     /// assert!(!nan.is_infinite());
156     ///
157     /// assert!(inf.is_infinite());
158     /// assert!(neg_inf.is_infinite());
159     /// ```
160     #[stable(feature = "rust1", since = "1.0.0")]
161     #[inline]
162     pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
163
164     /// Returns `true` if this number is neither infinite nor `NaN`.
165     ///
166     /// ```
167     /// use std::f32;
168     ///
169     /// let f = 7.0f32;
170     /// let inf = f32::INFINITY;
171     /// let neg_inf = f32::NEG_INFINITY;
172     /// let nan = f32::NAN;
173     ///
174     /// assert!(f.is_finite());
175     ///
176     /// assert!(!nan.is_finite());
177     /// assert!(!inf.is_finite());
178     /// assert!(!neg_inf.is_finite());
179     /// ```
180     #[stable(feature = "rust1", since = "1.0.0")]
181     #[inline]
182     pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
183
184     /// Returns `true` if the number is neither zero, infinite,
185     /// [subnormal][subnormal], or `NaN`.
186     ///
187     /// ```
188     /// use std::f32;
189     ///
190     /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
191     /// let max = f32::MAX;
192     /// let lower_than_min = 1.0e-40_f32;
193     /// let zero = 0.0_f32;
194     ///
195     /// assert!(min.is_normal());
196     /// assert!(max.is_normal());
197     ///
198     /// assert!(!zero.is_normal());
199     /// assert!(!f32::NAN.is_normal());
200     /// assert!(!f32::INFINITY.is_normal());
201     /// // Values between `0` and `min` are Subnormal.
202     /// assert!(!lower_than_min.is_normal());
203     /// ```
204     /// [subnormal]: https://en.wikipedia.org/wiki/Denormal_number
205     #[stable(feature = "rust1", since = "1.0.0")]
206     #[inline]
207     pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
208
209     /// Returns the floating point category of the number. If only one property
210     /// is going to be tested, it is generally faster to use the specific
211     /// predicate instead.
212     ///
213     /// ```
214     /// use std::num::FpCategory;
215     /// use std::f32;
216     ///
217     /// let num = 12.4_f32;
218     /// let inf = f32::INFINITY;
219     ///
220     /// assert_eq!(num.classify(), FpCategory::Normal);
221     /// assert_eq!(inf.classify(), FpCategory::Infinite);
222     /// ```
223     #[stable(feature = "rust1", since = "1.0.0")]
224     #[inline]
225     pub fn classify(self) -> FpCategory { num::Float::classify(self) }
226
227     /// Returns the largest integer less than or equal to a number.
228     ///
229     /// ```
230     /// let f = 3.99_f32;
231     /// let g = 3.0_f32;
232     ///
233     /// assert_eq!(f.floor(), 3.0);
234     /// assert_eq!(g.floor(), 3.0);
235     /// ```
236     #[stable(feature = "rust1", since = "1.0.0")]
237     #[inline]
238     pub fn floor(self) -> f32 {
239         // On MSVC LLVM will lower many math intrinsics to a call to the
240         // corresponding function. On MSVC, however, many of these functions
241         // aren't actually available as symbols to call, but rather they are all
242         // `static inline` functions in header files. This means that from a C
243         // perspective it's "compatible", but not so much from an ABI
244         // perspective (which we're worried about).
245         //
246         // The inline header functions always just cast to a f64 and do their
247         // operation, so we do that here as well, but only for MSVC targets.
248         //
249         // Note that there are many MSVC-specific float operations which
250         // redirect to this comment, so `floorf` is just one case of a missing
251         // function on MSVC, but there are many others elsewhere.
252         #[cfg(target_env = "msvc")]
253         return (self as f64).floor() as f32;
254         #[cfg(not(target_env = "msvc"))]
255         return unsafe { intrinsics::floorf32(self) };
256     }
257
258     /// Returns the smallest integer greater than or equal to a number.
259     ///
260     /// ```
261     /// let f = 3.01_f32;
262     /// let g = 4.0_f32;
263     ///
264     /// assert_eq!(f.ceil(), 4.0);
265     /// assert_eq!(g.ceil(), 4.0);
266     /// ```
267     #[stable(feature = "rust1", since = "1.0.0")]
268     #[inline]
269     pub fn ceil(self) -> f32 {
270         // see notes above in `floor`
271         #[cfg(target_env = "msvc")]
272         return (self as f64).ceil() as f32;
273         #[cfg(not(target_env = "msvc"))]
274         return unsafe { intrinsics::ceilf32(self) };
275     }
276
277     /// Returns the nearest integer to a number. Round half-way cases away from
278     /// `0.0`.
279     ///
280     /// ```
281     /// let f = 3.3_f32;
282     /// let g = -3.3_f32;
283     ///
284     /// assert_eq!(f.round(), 3.0);
285     /// assert_eq!(g.round(), -3.0);
286     /// ```
287     #[stable(feature = "rust1", since = "1.0.0")]
288     #[inline]
289     pub fn round(self) -> f32 {
290         unsafe { intrinsics::roundf32(self) }
291     }
292
293     /// Returns the integer part of a number.
294     ///
295     /// ```
296     /// let f = 3.3_f32;
297     /// let g = -3.7_f32;
298     ///
299     /// assert_eq!(f.trunc(), 3.0);
300     /// assert_eq!(g.trunc(), -3.0);
301     /// ```
302     #[stable(feature = "rust1", since = "1.0.0")]
303     #[inline]
304     pub fn trunc(self) -> f32 {
305         unsafe { intrinsics::truncf32(self) }
306     }
307
308     /// Returns the fractional part of a number.
309     ///
310     /// ```
311     /// use std::f32;
312     ///
313     /// let x = 3.5_f32;
314     /// let y = -3.5_f32;
315     /// let abs_difference_x = (x.fract() - 0.5).abs();
316     /// let abs_difference_y = (y.fract() - (-0.5)).abs();
317     ///
318     /// assert!(abs_difference_x <= f32::EPSILON);
319     /// assert!(abs_difference_y <= f32::EPSILON);
320     /// ```
321     #[stable(feature = "rust1", since = "1.0.0")]
322     #[inline]
323     pub fn fract(self) -> f32 { self - self.trunc() }
324
325     /// Computes the absolute value of `self`. Returns `NAN` if the
326     /// number is `NAN`.
327     ///
328     /// ```
329     /// use std::f32;
330     ///
331     /// let x = 3.5_f32;
332     /// let y = -3.5_f32;
333     ///
334     /// let abs_difference_x = (x.abs() - x).abs();
335     /// let abs_difference_y = (y.abs() - (-y)).abs();
336     ///
337     /// assert!(abs_difference_x <= f32::EPSILON);
338     /// assert!(abs_difference_y <= f32::EPSILON);
339     ///
340     /// assert!(f32::NAN.abs().is_nan());
341     /// ```
342     #[stable(feature = "rust1", since = "1.0.0")]
343     #[inline]
344     pub fn abs(self) -> f32 { num::Float::abs(self) }
345
346     /// Returns a number that represents the sign of `self`.
347     ///
348     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
349     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
350     /// - `NAN` if the number is `NAN`
351     ///
352     /// ```
353     /// use std::f32;
354     ///
355     /// let f = 3.5_f32;
356     ///
357     /// assert_eq!(f.signum(), 1.0);
358     /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
359     ///
360     /// assert!(f32::NAN.signum().is_nan());
361     /// ```
362     #[stable(feature = "rust1", since = "1.0.0")]
363     #[inline]
364     pub fn signum(self) -> f32 { num::Float::signum(self) }
365
366     /// Returns `true` if and only if `self` has a positive sign, including `+0.0`, `NaN`s with
367     /// positive sign bit and positive infinity.
368     ///
369     /// ```
370     /// let f = 7.0_f32;
371     /// let g = -7.0_f32;
372     ///
373     /// assert!(f.is_sign_positive());
374     /// assert!(!g.is_sign_positive());
375     /// ```
376     #[stable(feature = "rust1", since = "1.0.0")]
377     #[inline]
378     pub fn is_sign_positive(self) -> bool { num::Float::is_sign_positive(self) }
379
380     /// Returns `true` if and only if `self` has a negative sign, including `-0.0`, `NaN`s with
381     /// negative sign bit and negative infinity.
382     ///
383     /// ```
384     /// let f = 7.0f32;
385     /// let g = -7.0f32;
386     ///
387     /// assert!(!f.is_sign_negative());
388     /// assert!(g.is_sign_negative());
389     /// ```
390     #[stable(feature = "rust1", since = "1.0.0")]
391     #[inline]
392     pub fn is_sign_negative(self) -> bool { num::Float::is_sign_negative(self) }
393
394     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
395     /// error. This produces a more accurate result with better performance than
396     /// a separate multiplication operation followed by an add.
397     ///
398     /// ```
399     /// use std::f32;
400     ///
401     /// let m = 10.0_f32;
402     /// let x = 4.0_f32;
403     /// let b = 60.0_f32;
404     ///
405     /// // 100.0
406     /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
407     ///
408     /// assert!(abs_difference <= f32::EPSILON);
409     /// ```
410     #[stable(feature = "rust1", since = "1.0.0")]
411     #[inline]
412     pub fn mul_add(self, a: f32, b: f32) -> f32 {
413         unsafe { intrinsics::fmaf32(self, a, b) }
414     }
415
416     /// Takes the reciprocal (inverse) of a number, `1/x`.
417     ///
418     /// ```
419     /// use std::f32;
420     ///
421     /// let x = 2.0_f32;
422     /// let abs_difference = (x.recip() - (1.0/x)).abs();
423     ///
424     /// assert!(abs_difference <= f32::EPSILON);
425     /// ```
426     #[stable(feature = "rust1", since = "1.0.0")]
427     #[inline]
428     pub fn recip(self) -> f32 { num::Float::recip(self) }
429
430     /// Raises a number to an integer power.
431     ///
432     /// Using this function is generally faster than using `powf`
433     ///
434     /// ```
435     /// use std::f32;
436     ///
437     /// let x = 2.0_f32;
438     /// let abs_difference = (x.powi(2) - x*x).abs();
439     ///
440     /// assert!(abs_difference <= f32::EPSILON);
441     /// ```
442     #[stable(feature = "rust1", since = "1.0.0")]
443     #[inline]
444     pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
445
446     /// Raises a number to a floating point power.
447     ///
448     /// ```
449     /// use std::f32;
450     ///
451     /// let x = 2.0_f32;
452     /// let abs_difference = (x.powf(2.0) - x*x).abs();
453     ///
454     /// assert!(abs_difference <= f32::EPSILON);
455     /// ```
456     #[stable(feature = "rust1", since = "1.0.0")]
457     #[inline]
458     pub fn powf(self, n: f32) -> f32 {
459         // see notes above in `floor`
460         #[cfg(target_env = "msvc")]
461         return (self as f64).powf(n as f64) as f32;
462         #[cfg(not(target_env = "msvc"))]
463         return unsafe { intrinsics::powf32(self, n) };
464     }
465
466     /// Takes the square root of a number.
467     ///
468     /// Returns NaN if `self` is a negative number.
469     ///
470     /// ```
471     /// use std::f32;
472     ///
473     /// let positive = 4.0_f32;
474     /// let negative = -4.0_f32;
475     ///
476     /// let abs_difference = (positive.sqrt() - 2.0).abs();
477     ///
478     /// assert!(abs_difference <= f32::EPSILON);
479     /// assert!(negative.sqrt().is_nan());
480     /// ```
481     #[stable(feature = "rust1", since = "1.0.0")]
482     #[inline]
483     pub fn sqrt(self) -> f32 {
484         if self < 0.0 {
485             NAN
486         } else {
487             unsafe { intrinsics::sqrtf32(self) }
488         }
489     }
490
491     /// Returns `e^(self)`, (the exponential function).
492     ///
493     /// ```
494     /// use std::f32;
495     ///
496     /// let one = 1.0f32;
497     /// // e^1
498     /// let e = one.exp();
499     ///
500     /// // ln(e) - 1 == 0
501     /// let abs_difference = (e.ln() - 1.0).abs();
502     ///
503     /// assert!(abs_difference <= f32::EPSILON);
504     /// ```
505     #[stable(feature = "rust1", since = "1.0.0")]
506     #[inline]
507     pub fn exp(self) -> f32 {
508         // see notes above in `floor`
509         #[cfg(target_env = "msvc")]
510         return (self as f64).exp() as f32;
511         #[cfg(not(target_env = "msvc"))]
512         return unsafe { intrinsics::expf32(self) };
513     }
514
515     /// Returns `2^(self)`.
516     ///
517     /// ```
518     /// use std::f32;
519     ///
520     /// let f = 2.0f32;
521     ///
522     /// // 2^2 - 4 == 0
523     /// let abs_difference = (f.exp2() - 4.0).abs();
524     ///
525     /// assert!(abs_difference <= f32::EPSILON);
526     /// ```
527     #[stable(feature = "rust1", since = "1.0.0")]
528     #[inline]
529     pub fn exp2(self) -> f32 {
530         unsafe { intrinsics::exp2f32(self) }
531     }
532
533     /// Returns the natural logarithm of the number.
534     ///
535     /// ```
536     /// use std::f32;
537     ///
538     /// let one = 1.0f32;
539     /// // e^1
540     /// let e = one.exp();
541     ///
542     /// // ln(e) - 1 == 0
543     /// let abs_difference = (e.ln() - 1.0).abs();
544     ///
545     /// assert!(abs_difference <= f32::EPSILON);
546     /// ```
547     #[stable(feature = "rust1", since = "1.0.0")]
548     #[inline]
549     pub fn ln(self) -> f32 {
550         // see notes above in `floor`
551         #[cfg(target_env = "msvc")]
552         return (self as f64).ln() as f32;
553         #[cfg(not(target_env = "msvc"))]
554         return unsafe { intrinsics::logf32(self) };
555     }
556
557     /// Returns the logarithm of the number with respect to an arbitrary base.
558     ///
559     /// ```
560     /// use std::f32;
561     ///
562     /// let ten = 10.0f32;
563     /// let two = 2.0f32;
564     ///
565     /// // log10(10) - 1 == 0
566     /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
567     ///
568     /// // log2(2) - 1 == 0
569     /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
570     ///
571     /// assert!(abs_difference_10 <= f32::EPSILON);
572     /// assert!(abs_difference_2 <= f32::EPSILON);
573     /// ```
574     #[stable(feature = "rust1", since = "1.0.0")]
575     #[inline]
576     pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
577
578     /// Returns the base 2 logarithm of the number.
579     ///
580     /// ```
581     /// use std::f32;
582     ///
583     /// let two = 2.0f32;
584     ///
585     /// // log2(2) - 1 == 0
586     /// let abs_difference = (two.log2() - 1.0).abs();
587     ///
588     /// assert!(abs_difference <= f32::EPSILON);
589     /// ```
590     #[stable(feature = "rust1", since = "1.0.0")]
591     #[inline]
592     pub fn log2(self) -> f32 {
593         #[cfg(target_os = "android")]
594         return ::sys::android::log2f32(self);
595         #[cfg(not(target_os = "android"))]
596         return unsafe { intrinsics::log2f32(self) };
597     }
598
599     /// Returns the base 10 logarithm of the number.
600     ///
601     /// ```
602     /// use std::f32;
603     ///
604     /// let ten = 10.0f32;
605     ///
606     /// // log10(10) - 1 == 0
607     /// let abs_difference = (ten.log10() - 1.0).abs();
608     ///
609     /// assert!(abs_difference <= f32::EPSILON);
610     /// ```
611     #[stable(feature = "rust1", since = "1.0.0")]
612     #[inline]
613     pub fn log10(self) -> f32 {
614         // see notes above in `floor`
615         #[cfg(target_env = "msvc")]
616         return (self as f64).log10() as f32;
617         #[cfg(not(target_env = "msvc"))]
618         return unsafe { intrinsics::log10f32(self) };
619     }
620
621     /// Converts radians to degrees.
622     ///
623     /// ```
624     /// use std::f32::{self, consts};
625     ///
626     /// let angle = consts::PI;
627     ///
628     /// let abs_difference = (angle.to_degrees() - 180.0).abs();
629     ///
630     /// assert!(abs_difference <= f32::EPSILON);
631     /// ```
632     #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
633     #[inline]
634     pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
635
636     /// Converts degrees to radians.
637     ///
638     /// ```
639     /// use std::f32::{self, consts};
640     ///
641     /// let angle = 180.0f32;
642     ///
643     /// let abs_difference = (angle.to_radians() - consts::PI).abs();
644     ///
645     /// assert!(abs_difference <= f32::EPSILON);
646     /// ```
647     #[stable(feature = "f32_deg_rad_conversions", since="1.7.0")]
648     #[inline]
649     pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
650
651     /// Returns the maximum of the two numbers.
652     ///
653     /// ```
654     /// let x = 1.0f32;
655     /// let y = 2.0f32;
656     ///
657     /// assert_eq!(x.max(y), y);
658     /// ```
659     ///
660     /// If one of the arguments is NaN, then the other argument is returned.
661     #[stable(feature = "rust1", since = "1.0.0")]
662     #[inline]
663     pub fn max(self, other: f32) -> f32 {
664         num::Float::max(self, other)
665     }
666
667     /// Returns the minimum of the two numbers.
668     ///
669     /// ```
670     /// let x = 1.0f32;
671     /// let y = 2.0f32;
672     ///
673     /// assert_eq!(x.min(y), x);
674     /// ```
675     ///
676     /// If one of the arguments is NaN, then the other argument is returned.
677     #[stable(feature = "rust1", since = "1.0.0")]
678     #[inline]
679     pub fn min(self, other: f32) -> f32 {
680         num::Float::min(self, other)
681     }
682
683     /// The positive difference of two numbers.
684     ///
685     /// * If `self <= other`: `0:0`
686     /// * Else: `self - other`
687     ///
688     /// ```
689     /// use std::f32;
690     ///
691     /// let x = 3.0f32;
692     /// let y = -3.0f32;
693     ///
694     /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
695     /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
696     ///
697     /// assert!(abs_difference_x <= f32::EPSILON);
698     /// assert!(abs_difference_y <= f32::EPSILON);
699     /// ```
700     #[stable(feature = "rust1", since = "1.0.0")]
701     #[inline]
702     #[rustc_deprecated(since = "1.10.0",
703                        reason = "you probably meant `(self - other).abs()`: \
704                                  this operation is `(self - other).max(0.0)` (also \
705                                  known as `fdimf` in C). If you truly need the positive \
706                                  difference, consider using that expression or the C function \
707                                  `fdimf`, depending on how you wish to handle NaN (please consider \
708                                  filing an issue describing your use-case too).")]
709     pub fn abs_sub(self, other: f32) -> f32 {
710         unsafe { cmath::fdimf(self, other) }
711     }
712
713     /// Takes the cubic root of a number.
714     ///
715     /// ```
716     /// use std::f32;
717     ///
718     /// let x = 8.0f32;
719     ///
720     /// // x^(1/3) - 2 == 0
721     /// let abs_difference = (x.cbrt() - 2.0).abs();
722     ///
723     /// assert!(abs_difference <= f32::EPSILON);
724     /// ```
725     #[stable(feature = "rust1", since = "1.0.0")]
726     #[inline]
727     pub fn cbrt(self) -> f32 {
728         unsafe { cmath::cbrtf(self) }
729     }
730
731     /// Calculates the length of the hypotenuse of a right-angle triangle given
732     /// legs of length `x` and `y`.
733     ///
734     /// ```
735     /// use std::f32;
736     ///
737     /// let x = 2.0f32;
738     /// let y = 3.0f32;
739     ///
740     /// // sqrt(x^2 + y^2)
741     /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
742     ///
743     /// assert!(abs_difference <= f32::EPSILON);
744     /// ```
745     #[stable(feature = "rust1", since = "1.0.0")]
746     #[inline]
747     pub fn hypot(self, other: f32) -> f32 {
748         unsafe { cmath::hypotf(self, other) }
749     }
750
751     /// Computes the sine of a number (in radians).
752     ///
753     /// ```
754     /// use std::f32;
755     ///
756     /// let x = f32::consts::PI/2.0;
757     ///
758     /// let abs_difference = (x.sin() - 1.0).abs();
759     ///
760     /// assert!(abs_difference <= f32::EPSILON);
761     /// ```
762     #[stable(feature = "rust1", since = "1.0.0")]
763     #[inline]
764     pub fn sin(self) -> f32 {
765         // see notes in `core::f32::Float::floor`
766         #[cfg(target_env = "msvc")]
767         return (self as f64).sin() as f32;
768         #[cfg(not(target_env = "msvc"))]
769         return unsafe { intrinsics::sinf32(self) };
770     }
771
772     /// Computes the cosine of a number (in radians).
773     ///
774     /// ```
775     /// use std::f32;
776     ///
777     /// let x = 2.0*f32::consts::PI;
778     ///
779     /// let abs_difference = (x.cos() - 1.0).abs();
780     ///
781     /// assert!(abs_difference <= f32::EPSILON);
782     /// ```
783     #[stable(feature = "rust1", since = "1.0.0")]
784     #[inline]
785     pub fn cos(self) -> f32 {
786         // see notes in `core::f32::Float::floor`
787         #[cfg(target_env = "msvc")]
788         return (self as f64).cos() as f32;
789         #[cfg(not(target_env = "msvc"))]
790         return unsafe { intrinsics::cosf32(self) };
791     }
792
793     /// Computes the tangent of a number (in radians).
794     ///
795     /// ```
796     /// use std::f32;
797     ///
798     /// let x = f32::consts::PI / 4.0;
799     /// let abs_difference = (x.tan() - 1.0).abs();
800     ///
801     /// assert!(abs_difference <= f32::EPSILON);
802     /// ```
803     #[stable(feature = "rust1", since = "1.0.0")]
804     #[inline]
805     pub fn tan(self) -> f32 {
806         unsafe { cmath::tanf(self) }
807     }
808
809     /// Computes the arcsine of a number. Return value is in radians in
810     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
811     /// [-1, 1].
812     ///
813     /// ```
814     /// use std::f32;
815     ///
816     /// let f = f32::consts::PI / 2.0;
817     ///
818     /// // asin(sin(pi/2))
819     /// let abs_difference = (f.sin().asin() - f32::consts::PI / 2.0).abs();
820     ///
821     /// assert!(abs_difference <= f32::EPSILON);
822     /// ```
823     #[stable(feature = "rust1", since = "1.0.0")]
824     #[inline]
825     pub fn asin(self) -> f32 {
826         unsafe { cmath::asinf(self) }
827     }
828
829     /// Computes the arccosine of a number. Return value is in radians in
830     /// the range [0, pi] or NaN if the number is outside the range
831     /// [-1, 1].
832     ///
833     /// ```
834     /// use std::f32;
835     ///
836     /// let f = f32::consts::PI / 4.0;
837     ///
838     /// // acos(cos(pi/4))
839     /// let abs_difference = (f.cos().acos() - f32::consts::PI / 4.0).abs();
840     ///
841     /// assert!(abs_difference <= f32::EPSILON);
842     /// ```
843     #[stable(feature = "rust1", since = "1.0.0")]
844     #[inline]
845     pub fn acos(self) -> f32 {
846         unsafe { cmath::acosf(self) }
847     }
848
849     /// Computes the arctangent of a number. Return value is in radians in the
850     /// range [-pi/2, pi/2];
851     ///
852     /// ```
853     /// use std::f32;
854     ///
855     /// let f = 1.0f32;
856     ///
857     /// // atan(tan(1))
858     /// let abs_difference = (f.tan().atan() - 1.0).abs();
859     ///
860     /// assert!(abs_difference <= f32::EPSILON);
861     /// ```
862     #[stable(feature = "rust1", since = "1.0.0")]
863     #[inline]
864     pub fn atan(self) -> f32 {
865         unsafe { cmath::atanf(self) }
866     }
867
868     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
869     ///
870     /// * `x = 0`, `y = 0`: `0`
871     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
872     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
873     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
874     ///
875     /// ```
876     /// use std::f32;
877     ///
878     /// let pi = f32::consts::PI;
879     /// // All angles from horizontal right (+x)
880     /// // 45 deg counter-clockwise
881     /// let x1 = 3.0f32;
882     /// let y1 = -3.0f32;
883     ///
884     /// // 135 deg clockwise
885     /// let x2 = -3.0f32;
886     /// let y2 = 3.0f32;
887     ///
888     /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
889     /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
890     ///
891     /// assert!(abs_difference_1 <= f32::EPSILON);
892     /// assert!(abs_difference_2 <= f32::EPSILON);
893     /// ```
894     #[stable(feature = "rust1", since = "1.0.0")]
895     #[inline]
896     pub fn atan2(self, other: f32) -> f32 {
897         unsafe { cmath::atan2f(self, other) }
898     }
899
900     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
901     /// `(sin(x), cos(x))`.
902     ///
903     /// ```
904     /// use std::f32;
905     ///
906     /// let x = f32::consts::PI/4.0;
907     /// let f = x.sin_cos();
908     ///
909     /// let abs_difference_0 = (f.0 - x.sin()).abs();
910     /// let abs_difference_1 = (f.1 - x.cos()).abs();
911     ///
912     /// assert!(abs_difference_0 <= f32::EPSILON);
913     /// assert!(abs_difference_1 <= f32::EPSILON);
914     /// ```
915     #[stable(feature = "rust1", since = "1.0.0")]
916     #[inline]
917     pub fn sin_cos(self) -> (f32, f32) {
918         (self.sin(), self.cos())
919     }
920
921     /// Returns `e^(self) - 1` in a way that is accurate even if the
922     /// number is close to zero.
923     ///
924     /// ```
925     /// use std::f32;
926     ///
927     /// let x = 6.0f32;
928     ///
929     /// // e^(ln(6)) - 1
930     /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
931     ///
932     /// assert!(abs_difference <= f32::EPSILON);
933     /// ```
934     #[stable(feature = "rust1", since = "1.0.0")]
935     #[inline]
936     pub fn exp_m1(self) -> f32 {
937         unsafe { cmath::expm1f(self) }
938     }
939
940     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
941     /// the operations were performed separately.
942     ///
943     /// ```
944     /// use std::f32;
945     ///
946     /// let x = f32::consts::E - 1.0;
947     ///
948     /// // ln(1 + (e - 1)) == ln(e) == 1
949     /// let abs_difference = (x.ln_1p() - 1.0).abs();
950     ///
951     /// assert!(abs_difference <= f32::EPSILON);
952     /// ```
953     #[stable(feature = "rust1", since = "1.0.0")]
954     #[inline]
955     pub fn ln_1p(self) -> f32 {
956         unsafe { cmath::log1pf(self) }
957     }
958
959     /// Hyperbolic sine function.
960     ///
961     /// ```
962     /// use std::f32;
963     ///
964     /// let e = f32::consts::E;
965     /// let x = 1.0f32;
966     ///
967     /// let f = x.sinh();
968     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
969     /// let g = (e*e - 1.0)/(2.0*e);
970     /// let abs_difference = (f - g).abs();
971     ///
972     /// assert!(abs_difference <= f32::EPSILON);
973     /// ```
974     #[stable(feature = "rust1", since = "1.0.0")]
975     #[inline]
976     pub fn sinh(self) -> f32 {
977         unsafe { cmath::sinhf(self) }
978     }
979
980     /// Hyperbolic cosine function.
981     ///
982     /// ```
983     /// use std::f32;
984     ///
985     /// let e = f32::consts::E;
986     /// let x = 1.0f32;
987     /// let f = x.cosh();
988     /// // Solving cosh() at 1 gives this result
989     /// let g = (e*e + 1.0)/(2.0*e);
990     /// let abs_difference = (f - g).abs();
991     ///
992     /// // Same result
993     /// assert!(abs_difference <= f32::EPSILON);
994     /// ```
995     #[stable(feature = "rust1", since = "1.0.0")]
996     #[inline]
997     pub fn cosh(self) -> f32 {
998         unsafe { cmath::coshf(self) }
999     }
1000
1001     /// Hyperbolic tangent function.
1002     ///
1003     /// ```
1004     /// use std::f32;
1005     ///
1006     /// let e = f32::consts::E;
1007     /// let x = 1.0f32;
1008     ///
1009     /// let f = x.tanh();
1010     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1011     /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1012     /// let abs_difference = (f - g).abs();
1013     ///
1014     /// assert!(abs_difference <= f32::EPSILON);
1015     /// ```
1016     #[stable(feature = "rust1", since = "1.0.0")]
1017     #[inline]
1018     pub fn tanh(self) -> f32 {
1019         unsafe { cmath::tanhf(self) }
1020     }
1021
1022     /// Inverse hyperbolic sine function.
1023     ///
1024     /// ```
1025     /// use std::f32;
1026     ///
1027     /// let x = 1.0f32;
1028     /// let f = x.sinh().asinh();
1029     ///
1030     /// let abs_difference = (f - x).abs();
1031     ///
1032     /// assert!(abs_difference <= f32::EPSILON);
1033     /// ```
1034     #[stable(feature = "rust1", since = "1.0.0")]
1035     #[inline]
1036     pub fn asinh(self) -> f32 {
1037         if self == NEG_INFINITY {
1038             NEG_INFINITY
1039         } else {
1040             (self + ((self * self) + 1.0).sqrt()).ln()
1041         }
1042     }
1043
1044     /// Inverse hyperbolic cosine function.
1045     ///
1046     /// ```
1047     /// use std::f32;
1048     ///
1049     /// let x = 1.0f32;
1050     /// let f = x.cosh().acosh();
1051     ///
1052     /// let abs_difference = (f - x).abs();
1053     ///
1054     /// assert!(abs_difference <= f32::EPSILON);
1055     /// ```
1056     #[stable(feature = "rust1", since = "1.0.0")]
1057     #[inline]
1058     pub fn acosh(self) -> f32 {
1059         match self {
1060             x if x < 1.0 => ::f32::NAN,
1061             x => (x + ((x * x) - 1.0).sqrt()).ln(),
1062         }
1063     }
1064
1065     /// Inverse hyperbolic tangent function.
1066     ///
1067     /// ```
1068     /// use std::f32;
1069     ///
1070     /// let e = f32::consts::E;
1071     /// let f = e.tanh().atanh();
1072     ///
1073     /// let abs_difference = (f - e).abs();
1074     ///
1075     /// assert!(abs_difference <= 1e-5);
1076     /// ```
1077     #[stable(feature = "rust1", since = "1.0.0")]
1078     #[inline]
1079     pub fn atanh(self) -> f32 {
1080         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1081     }
1082
1083     /// Returns max if self is greater than max, and min if self is less than min.
1084     /// Otherwise this returns self.  Panics if min > max, min equals NaN, or max equals NaN.
1085     ///
1086     /// # Examples
1087     ///
1088     /// ```
1089     /// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
1090     /// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
1091     /// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
1092     /// ```
1093     #[unstable(feature = "clamp", issue = "44095")]
1094     #[inline]
1095     pub fn clamp(self, min: f32, max: f32) -> f32 {
1096         assert!(min <= max);
1097         let mut x = self;
1098         if x < min { x = min; }
1099         if x > max { x = max; }
1100         x
1101     }
1102
1103     /// Raw transmutation to `u32`.
1104     ///
1105     /// Converts the `f32` into its raw memory representation,
1106     /// similar to the `transmute` function.
1107     ///
1108     /// Note that this function is distinct from casting.
1109     ///
1110     /// # Examples
1111     ///
1112     /// ```
1113     /// assert_ne!((1f32).to_bits(), 1f32 as u32); // to_bits() is not casting!
1114     /// assert_eq!((12.5f32).to_bits(), 0x41480000);
1115     ///
1116     /// ```
1117     #[stable(feature = "float_bits_conv", since = "1.20.0")]
1118     #[inline]
1119     pub fn to_bits(self) -> u32 {
1120         unsafe { ::mem::transmute(self) }
1121     }
1122
1123     /// Raw transmutation from `u32`.
1124     ///
1125     /// Converts the given `u32` containing the float's raw memory
1126     /// representation into the `f32` type, similar to the
1127     /// `transmute` function.
1128     ///
1129     /// There is only one difference to a bare `transmute`:
1130     /// Due to the implications onto Rust's safety promises being
1131     /// uncertain, if the representation of a signaling NaN "sNaN" float
1132     /// is passed to the function, the implementation is allowed to
1133     /// return a quiet NaN instead.
1134     ///
1135     /// Note that this function is distinct from casting.
1136     ///
1137     /// # Examples
1138     ///
1139     /// ```
1140     /// use std::f32;
1141     /// let v = f32::from_bits(0x41480000);
1142     /// let difference = (v - 12.5).abs();
1143     /// assert!(difference <= 1e-5);
1144     /// // Example for a signaling NaN value:
1145     /// let snan = 0x7F800001;
1146     /// assert_ne!(f32::from_bits(snan).to_bits(), snan);
1147     /// ```
1148     #[stable(feature = "float_bits_conv", since = "1.20.0")]
1149     #[inline]
1150     pub fn from_bits(mut v: u32) -> Self {
1151         const EXP_MASK: u32   = 0x7F800000;
1152         const FRACT_MASK: u32 = 0x007FFFFF;
1153         if v & EXP_MASK == EXP_MASK && v & FRACT_MASK != 0 {
1154             // While IEEE 754-2008 specifies encodings for quiet NaNs
1155             // and signaling ones, certain MIPS and PA-RISC
1156             // CPUs treat signaling NaNs differently.
1157             // Therefore to be safe, we pass a known quiet NaN
1158             // if v is any kind of NaN.
1159             // The check above only assumes IEEE 754-1985 to be
1160             // valid.
1161             v = unsafe { ::mem::transmute(NAN) };
1162         }
1163         unsafe { ::mem::transmute(v) }
1164     }
1165 }
1166
1167 #[cfg(test)]
1168 mod tests {
1169     use f32;
1170     use f32::*;
1171     use num::*;
1172     use num::FpCategory as Fp;
1173
1174     #[test]
1175     fn test_num_f32() {
1176         test_num(10f32, 2f32);
1177     }
1178
1179     #[test]
1180     fn test_min_nan() {
1181         assert_eq!(NAN.min(2.0), 2.0);
1182         assert_eq!(2.0f32.min(NAN), 2.0);
1183     }
1184
1185     #[test]
1186     fn test_max_nan() {
1187         assert_eq!(NAN.max(2.0), 2.0);
1188         assert_eq!(2.0f32.max(NAN), 2.0);
1189     }
1190
1191     #[test]
1192     fn test_nan() {
1193         let nan: f32 = f32::NAN;
1194         assert!(nan.is_nan());
1195         assert!(!nan.is_infinite());
1196         assert!(!nan.is_finite());
1197         assert!(!nan.is_normal());
1198         assert!(nan.is_sign_positive());
1199         assert!(!nan.is_sign_negative());
1200         assert_eq!(Fp::Nan, nan.classify());
1201     }
1202
1203     #[test]
1204     fn test_infinity() {
1205         let inf: f32 = f32::INFINITY;
1206         assert!(inf.is_infinite());
1207         assert!(!inf.is_finite());
1208         assert!(inf.is_sign_positive());
1209         assert!(!inf.is_sign_negative());
1210         assert!(!inf.is_nan());
1211         assert!(!inf.is_normal());
1212         assert_eq!(Fp::Infinite, inf.classify());
1213     }
1214
1215     #[test]
1216     fn test_neg_infinity() {
1217         let neg_inf: f32 = f32::NEG_INFINITY;
1218         assert!(neg_inf.is_infinite());
1219         assert!(!neg_inf.is_finite());
1220         assert!(!neg_inf.is_sign_positive());
1221         assert!(neg_inf.is_sign_negative());
1222         assert!(!neg_inf.is_nan());
1223         assert!(!neg_inf.is_normal());
1224         assert_eq!(Fp::Infinite, neg_inf.classify());
1225     }
1226
1227     #[test]
1228     fn test_zero() {
1229         let zero: f32 = 0.0f32;
1230         assert_eq!(0.0, zero);
1231         assert!(!zero.is_infinite());
1232         assert!(zero.is_finite());
1233         assert!(zero.is_sign_positive());
1234         assert!(!zero.is_sign_negative());
1235         assert!(!zero.is_nan());
1236         assert!(!zero.is_normal());
1237         assert_eq!(Fp::Zero, zero.classify());
1238     }
1239
1240     #[test]
1241     fn test_neg_zero() {
1242         let neg_zero: f32 = -0.0;
1243         assert_eq!(0.0, neg_zero);
1244         assert!(!neg_zero.is_infinite());
1245         assert!(neg_zero.is_finite());
1246         assert!(!neg_zero.is_sign_positive());
1247         assert!(neg_zero.is_sign_negative());
1248         assert!(!neg_zero.is_nan());
1249         assert!(!neg_zero.is_normal());
1250         assert_eq!(Fp::Zero, neg_zero.classify());
1251     }
1252
1253     #[test]
1254     fn test_one() {
1255         let one: f32 = 1.0f32;
1256         assert_eq!(1.0, one);
1257         assert!(!one.is_infinite());
1258         assert!(one.is_finite());
1259         assert!(one.is_sign_positive());
1260         assert!(!one.is_sign_negative());
1261         assert!(!one.is_nan());
1262         assert!(one.is_normal());
1263         assert_eq!(Fp::Normal, one.classify());
1264     }
1265
1266     #[test]
1267     fn test_is_nan() {
1268         let nan: f32 = f32::NAN;
1269         let inf: f32 = f32::INFINITY;
1270         let neg_inf: f32 = f32::NEG_INFINITY;
1271         assert!(nan.is_nan());
1272         assert!(!0.0f32.is_nan());
1273         assert!(!5.3f32.is_nan());
1274         assert!(!(-10.732f32).is_nan());
1275         assert!(!inf.is_nan());
1276         assert!(!neg_inf.is_nan());
1277     }
1278
1279     #[test]
1280     fn test_is_infinite() {
1281         let nan: f32 = f32::NAN;
1282         let inf: f32 = f32::INFINITY;
1283         let neg_inf: f32 = f32::NEG_INFINITY;
1284         assert!(!nan.is_infinite());
1285         assert!(inf.is_infinite());
1286         assert!(neg_inf.is_infinite());
1287         assert!(!0.0f32.is_infinite());
1288         assert!(!42.8f32.is_infinite());
1289         assert!(!(-109.2f32).is_infinite());
1290     }
1291
1292     #[test]
1293     fn test_is_finite() {
1294         let nan: f32 = f32::NAN;
1295         let inf: f32 = f32::INFINITY;
1296         let neg_inf: f32 = f32::NEG_INFINITY;
1297         assert!(!nan.is_finite());
1298         assert!(!inf.is_finite());
1299         assert!(!neg_inf.is_finite());
1300         assert!(0.0f32.is_finite());
1301         assert!(42.8f32.is_finite());
1302         assert!((-109.2f32).is_finite());
1303     }
1304
1305     #[test]
1306     fn test_is_normal() {
1307         let nan: f32 = f32::NAN;
1308         let inf: f32 = f32::INFINITY;
1309         let neg_inf: f32 = f32::NEG_INFINITY;
1310         let zero: f32 = 0.0f32;
1311         let neg_zero: f32 = -0.0;
1312         assert!(!nan.is_normal());
1313         assert!(!inf.is_normal());
1314         assert!(!neg_inf.is_normal());
1315         assert!(!zero.is_normal());
1316         assert!(!neg_zero.is_normal());
1317         assert!(1f32.is_normal());
1318         assert!(1e-37f32.is_normal());
1319         assert!(!1e-38f32.is_normal());
1320     }
1321
1322     #[test]
1323     fn test_classify() {
1324         let nan: f32 = f32::NAN;
1325         let inf: f32 = f32::INFINITY;
1326         let neg_inf: f32 = f32::NEG_INFINITY;
1327         let zero: f32 = 0.0f32;
1328         let neg_zero: f32 = -0.0;
1329         assert_eq!(nan.classify(), Fp::Nan);
1330         assert_eq!(inf.classify(), Fp::Infinite);
1331         assert_eq!(neg_inf.classify(), Fp::Infinite);
1332         assert_eq!(zero.classify(), Fp::Zero);
1333         assert_eq!(neg_zero.classify(), Fp::Zero);
1334         assert_eq!(1f32.classify(), Fp::Normal);
1335         assert_eq!(1e-37f32.classify(), Fp::Normal);
1336         assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1337     }
1338
1339     #[test]
1340     fn test_floor() {
1341         assert_approx_eq!(1.0f32.floor(), 1.0f32);
1342         assert_approx_eq!(1.3f32.floor(), 1.0f32);
1343         assert_approx_eq!(1.5f32.floor(), 1.0f32);
1344         assert_approx_eq!(1.7f32.floor(), 1.0f32);
1345         assert_approx_eq!(0.0f32.floor(), 0.0f32);
1346         assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1347         assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1348         assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1349         assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1350         assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1351     }
1352
1353     #[test]
1354     fn test_ceil() {
1355         assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1356         assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1357         assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1358         assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1359         assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1360         assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1361         assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1362         assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1363         assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1364         assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1365     }
1366
1367     #[test]
1368     fn test_round() {
1369         assert_approx_eq!(1.0f32.round(), 1.0f32);
1370         assert_approx_eq!(1.3f32.round(), 1.0f32);
1371         assert_approx_eq!(1.5f32.round(), 2.0f32);
1372         assert_approx_eq!(1.7f32.round(), 2.0f32);
1373         assert_approx_eq!(0.0f32.round(), 0.0f32);
1374         assert_approx_eq!((-0.0f32).round(), -0.0f32);
1375         assert_approx_eq!((-1.0f32).round(), -1.0f32);
1376         assert_approx_eq!((-1.3f32).round(), -1.0f32);
1377         assert_approx_eq!((-1.5f32).round(), -2.0f32);
1378         assert_approx_eq!((-1.7f32).round(), -2.0f32);
1379     }
1380
1381     #[test]
1382     fn test_trunc() {
1383         assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1384         assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1385         assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1386         assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1387         assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1388         assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1389         assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1390         assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1391         assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1392         assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1393     }
1394
1395     #[test]
1396     fn test_fract() {
1397         assert_approx_eq!(1.0f32.fract(), 0.0f32);
1398         assert_approx_eq!(1.3f32.fract(), 0.3f32);
1399         assert_approx_eq!(1.5f32.fract(), 0.5f32);
1400         assert_approx_eq!(1.7f32.fract(), 0.7f32);
1401         assert_approx_eq!(0.0f32.fract(), 0.0f32);
1402         assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1403         assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1404         assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1405         assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1406         assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1407     }
1408
1409     #[test]
1410     fn test_abs() {
1411         assert_eq!(INFINITY.abs(), INFINITY);
1412         assert_eq!(1f32.abs(), 1f32);
1413         assert_eq!(0f32.abs(), 0f32);
1414         assert_eq!((-0f32).abs(), 0f32);
1415         assert_eq!((-1f32).abs(), 1f32);
1416         assert_eq!(NEG_INFINITY.abs(), INFINITY);
1417         assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1418         assert!(NAN.abs().is_nan());
1419     }
1420
1421     #[test]
1422     fn test_signum() {
1423         assert_eq!(INFINITY.signum(), 1f32);
1424         assert_eq!(1f32.signum(), 1f32);
1425         assert_eq!(0f32.signum(), 1f32);
1426         assert_eq!((-0f32).signum(), -1f32);
1427         assert_eq!((-1f32).signum(), -1f32);
1428         assert_eq!(NEG_INFINITY.signum(), -1f32);
1429         assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1430         assert!(NAN.signum().is_nan());
1431     }
1432
1433     #[test]
1434     fn test_is_sign_positive() {
1435         assert!(INFINITY.is_sign_positive());
1436         assert!(1f32.is_sign_positive());
1437         assert!(0f32.is_sign_positive());
1438         assert!(!(-0f32).is_sign_positive());
1439         assert!(!(-1f32).is_sign_positive());
1440         assert!(!NEG_INFINITY.is_sign_positive());
1441         assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1442         assert!(NAN.is_sign_positive());
1443         assert!(!(-NAN).is_sign_positive());
1444     }
1445
1446     #[test]
1447     fn test_is_sign_negative() {
1448         assert!(!INFINITY.is_sign_negative());
1449         assert!(!1f32.is_sign_negative());
1450         assert!(!0f32.is_sign_negative());
1451         assert!((-0f32).is_sign_negative());
1452         assert!((-1f32).is_sign_negative());
1453         assert!(NEG_INFINITY.is_sign_negative());
1454         assert!((1f32/NEG_INFINITY).is_sign_negative());
1455         assert!(!NAN.is_sign_negative());
1456         assert!((-NAN).is_sign_negative());
1457     }
1458
1459     #[test]
1460     fn test_mul_add() {
1461         let nan: f32 = f32::NAN;
1462         let inf: f32 = f32::INFINITY;
1463         let neg_inf: f32 = f32::NEG_INFINITY;
1464         assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1465         assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1466         assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1467         assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1468         assert!(nan.mul_add(7.8, 9.0).is_nan());
1469         assert_eq!(inf.mul_add(7.8, 9.0), inf);
1470         assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1471         assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1472         assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1473     }
1474
1475     #[test]
1476     fn test_recip() {
1477         let nan: f32 = f32::NAN;
1478         let inf: f32 = f32::INFINITY;
1479         let neg_inf: f32 = f32::NEG_INFINITY;
1480         assert_eq!(1.0f32.recip(), 1.0);
1481         assert_eq!(2.0f32.recip(), 0.5);
1482         assert_eq!((-0.4f32).recip(), -2.5);
1483         assert_eq!(0.0f32.recip(), inf);
1484         assert!(nan.recip().is_nan());
1485         assert_eq!(inf.recip(), 0.0);
1486         assert_eq!(neg_inf.recip(), 0.0);
1487     }
1488
1489     #[test]
1490     fn test_powi() {
1491         let nan: f32 = f32::NAN;
1492         let inf: f32 = f32::INFINITY;
1493         let neg_inf: f32 = f32::NEG_INFINITY;
1494         assert_eq!(1.0f32.powi(1), 1.0);
1495         assert_approx_eq!((-3.1f32).powi(2), 9.61);
1496         assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1497         assert_eq!(8.3f32.powi(0), 1.0);
1498         assert!(nan.powi(2).is_nan());
1499         assert_eq!(inf.powi(3), inf);
1500         assert_eq!(neg_inf.powi(2), inf);
1501     }
1502
1503     #[test]
1504     fn test_powf() {
1505         let nan: f32 = f32::NAN;
1506         let inf: f32 = f32::INFINITY;
1507         let neg_inf: f32 = f32::NEG_INFINITY;
1508         assert_eq!(1.0f32.powf(1.0), 1.0);
1509         assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1510         assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1511         assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1512         assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1513         assert_eq!(8.3f32.powf(0.0), 1.0);
1514         assert!(nan.powf(2.0).is_nan());
1515         assert_eq!(inf.powf(2.0), inf);
1516         assert_eq!(neg_inf.powf(3.0), neg_inf);
1517     }
1518
1519     #[test]
1520     fn test_sqrt_domain() {
1521         assert!(NAN.sqrt().is_nan());
1522         assert!(NEG_INFINITY.sqrt().is_nan());
1523         assert!((-1.0f32).sqrt().is_nan());
1524         assert_eq!((-0.0f32).sqrt(), -0.0);
1525         assert_eq!(0.0f32.sqrt(), 0.0);
1526         assert_eq!(1.0f32.sqrt(), 1.0);
1527         assert_eq!(INFINITY.sqrt(), INFINITY);
1528     }
1529
1530     #[test]
1531     fn test_exp() {
1532         assert_eq!(1.0, 0.0f32.exp());
1533         assert_approx_eq!(2.718282, 1.0f32.exp());
1534         assert_approx_eq!(148.413162, 5.0f32.exp());
1535
1536         let inf: f32 = f32::INFINITY;
1537         let neg_inf: f32 = f32::NEG_INFINITY;
1538         let nan: f32 = f32::NAN;
1539         assert_eq!(inf, inf.exp());
1540         assert_eq!(0.0, neg_inf.exp());
1541         assert!(nan.exp().is_nan());
1542     }
1543
1544     #[test]
1545     fn test_exp2() {
1546         assert_eq!(32.0, 5.0f32.exp2());
1547         assert_eq!(1.0, 0.0f32.exp2());
1548
1549         let inf: f32 = f32::INFINITY;
1550         let neg_inf: f32 = f32::NEG_INFINITY;
1551         let nan: f32 = f32::NAN;
1552         assert_eq!(inf, inf.exp2());
1553         assert_eq!(0.0, neg_inf.exp2());
1554         assert!(nan.exp2().is_nan());
1555     }
1556
1557     #[test]
1558     fn test_ln() {
1559         let nan: f32 = f32::NAN;
1560         let inf: f32 = f32::INFINITY;
1561         let neg_inf: f32 = f32::NEG_INFINITY;
1562         assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1563         assert!(nan.ln().is_nan());
1564         assert_eq!(inf.ln(), inf);
1565         assert!(neg_inf.ln().is_nan());
1566         assert!((-2.3f32).ln().is_nan());
1567         assert_eq!((-0.0f32).ln(), neg_inf);
1568         assert_eq!(0.0f32.ln(), neg_inf);
1569         assert_approx_eq!(4.0f32.ln(), 1.386294);
1570     }
1571
1572     #[test]
1573     fn test_log() {
1574         let nan: f32 = f32::NAN;
1575         let inf: f32 = f32::INFINITY;
1576         let neg_inf: f32 = f32::NEG_INFINITY;
1577         assert_eq!(10.0f32.log(10.0), 1.0);
1578         assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1579         assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1580         assert!(1.0f32.log(1.0).is_nan());
1581         assert!(1.0f32.log(-13.9).is_nan());
1582         assert!(nan.log(2.3).is_nan());
1583         assert_eq!(inf.log(10.0), inf);
1584         assert!(neg_inf.log(8.8).is_nan());
1585         assert!((-2.3f32).log(0.1).is_nan());
1586         assert_eq!((-0.0f32).log(2.0), neg_inf);
1587         assert_eq!(0.0f32.log(7.0), neg_inf);
1588     }
1589
1590     #[test]
1591     fn test_log2() {
1592         let nan: f32 = f32::NAN;
1593         let inf: f32 = f32::INFINITY;
1594         let neg_inf: f32 = f32::NEG_INFINITY;
1595         assert_approx_eq!(10.0f32.log2(), 3.321928);
1596         assert_approx_eq!(2.3f32.log2(), 1.201634);
1597         assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1598         assert!(nan.log2().is_nan());
1599         assert_eq!(inf.log2(), inf);
1600         assert!(neg_inf.log2().is_nan());
1601         assert!((-2.3f32).log2().is_nan());
1602         assert_eq!((-0.0f32).log2(), neg_inf);
1603         assert_eq!(0.0f32.log2(), neg_inf);
1604     }
1605
1606     #[test]
1607     fn test_log10() {
1608         let nan: f32 = f32::NAN;
1609         let inf: f32 = f32::INFINITY;
1610         let neg_inf: f32 = f32::NEG_INFINITY;
1611         assert_eq!(10.0f32.log10(), 1.0);
1612         assert_approx_eq!(2.3f32.log10(), 0.361728);
1613         assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1614         assert_eq!(1.0f32.log10(), 0.0);
1615         assert!(nan.log10().is_nan());
1616         assert_eq!(inf.log10(), inf);
1617         assert!(neg_inf.log10().is_nan());
1618         assert!((-2.3f32).log10().is_nan());
1619         assert_eq!((-0.0f32).log10(), neg_inf);
1620         assert_eq!(0.0f32.log10(), neg_inf);
1621     }
1622
1623     #[test]
1624     fn test_to_degrees() {
1625         let pi: f32 = consts::PI;
1626         let nan: f32 = f32::NAN;
1627         let inf: f32 = f32::INFINITY;
1628         let neg_inf: f32 = f32::NEG_INFINITY;
1629         assert_eq!(0.0f32.to_degrees(), 0.0);
1630         assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1631         assert_eq!(pi.to_degrees(), 180.0);
1632         assert!(nan.to_degrees().is_nan());
1633         assert_eq!(inf.to_degrees(), inf);
1634         assert_eq!(neg_inf.to_degrees(), neg_inf);
1635     }
1636
1637     #[test]
1638     fn test_to_radians() {
1639         let pi: f32 = consts::PI;
1640         let nan: f32 = f32::NAN;
1641         let inf: f32 = f32::INFINITY;
1642         let neg_inf: f32 = f32::NEG_INFINITY;
1643         assert_eq!(0.0f32.to_radians(), 0.0);
1644         assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1645         assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1646         assert_eq!(180.0f32.to_radians(), pi);
1647         assert!(nan.to_radians().is_nan());
1648         assert_eq!(inf.to_radians(), inf);
1649         assert_eq!(neg_inf.to_radians(), neg_inf);
1650     }
1651
1652     #[test]
1653     fn test_asinh() {
1654         assert_eq!(0.0f32.asinh(), 0.0f32);
1655         assert_eq!((-0.0f32).asinh(), -0.0f32);
1656
1657         let inf: f32 = f32::INFINITY;
1658         let neg_inf: f32 = f32::NEG_INFINITY;
1659         let nan: f32 = f32::NAN;
1660         assert_eq!(inf.asinh(), inf);
1661         assert_eq!(neg_inf.asinh(), neg_inf);
1662         assert!(nan.asinh().is_nan());
1663         assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1664         assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1665     }
1666
1667     #[test]
1668     fn test_acosh() {
1669         assert_eq!(1.0f32.acosh(), 0.0f32);
1670         assert!(0.999f32.acosh().is_nan());
1671
1672         let inf: f32 = f32::INFINITY;
1673         let neg_inf: f32 = f32::NEG_INFINITY;
1674         let nan: f32 = f32::NAN;
1675         assert_eq!(inf.acosh(), inf);
1676         assert!(neg_inf.acosh().is_nan());
1677         assert!(nan.acosh().is_nan());
1678         assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1679         assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1680     }
1681
1682     #[test]
1683     fn test_atanh() {
1684         assert_eq!(0.0f32.atanh(), 0.0f32);
1685         assert_eq!((-0.0f32).atanh(), -0.0f32);
1686
1687         let inf32: f32 = f32::INFINITY;
1688         let neg_inf32: f32 = f32::NEG_INFINITY;
1689         assert_eq!(1.0f32.atanh(), inf32);
1690         assert_eq!((-1.0f32).atanh(), neg_inf32);
1691
1692         assert!(2f64.atanh().atanh().is_nan());
1693         assert!((-2f64).atanh().atanh().is_nan());
1694
1695         let inf64: f32 = f32::INFINITY;
1696         let neg_inf64: f32 = f32::NEG_INFINITY;
1697         let nan32: f32 = f32::NAN;
1698         assert!(inf64.atanh().is_nan());
1699         assert!(neg_inf64.atanh().is_nan());
1700         assert!(nan32.atanh().is_nan());
1701
1702         assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1703         assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1704     }
1705
1706     #[test]
1707     fn test_real_consts() {
1708         use super::consts;
1709
1710         let pi: f32 = consts::PI;
1711         let frac_pi_2: f32 = consts::FRAC_PI_2;
1712         let frac_pi_3: f32 = consts::FRAC_PI_3;
1713         let frac_pi_4: f32 = consts::FRAC_PI_4;
1714         let frac_pi_6: f32 = consts::FRAC_PI_6;
1715         let frac_pi_8: f32 = consts::FRAC_PI_8;
1716         let frac_1_pi: f32 = consts::FRAC_1_PI;
1717         let frac_2_pi: f32 = consts::FRAC_2_PI;
1718         let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1719         let sqrt2: f32 = consts::SQRT_2;
1720         let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1721         let e: f32 = consts::E;
1722         let log2_e: f32 = consts::LOG2_E;
1723         let log10_e: f32 = consts::LOG10_E;
1724         let ln_2: f32 = consts::LN_2;
1725         let ln_10: f32 = consts::LN_10;
1726
1727         assert_approx_eq!(frac_pi_2, pi / 2f32);
1728         assert_approx_eq!(frac_pi_3, pi / 3f32);
1729         assert_approx_eq!(frac_pi_4, pi / 4f32);
1730         assert_approx_eq!(frac_pi_6, pi / 6f32);
1731         assert_approx_eq!(frac_pi_8, pi / 8f32);
1732         assert_approx_eq!(frac_1_pi, 1f32 / pi);
1733         assert_approx_eq!(frac_2_pi, 2f32 / pi);
1734         assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1735         assert_approx_eq!(sqrt2, 2f32.sqrt());
1736         assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1737         assert_approx_eq!(log2_e, e.log2());
1738         assert_approx_eq!(log10_e, e.log10());
1739         assert_approx_eq!(ln_2, 2f32.ln());
1740         assert_approx_eq!(ln_10, 10f32.ln());
1741     }
1742
1743     #[test]
1744     fn test_float_bits_conv() {
1745         assert_eq!((1f32).to_bits(), 0x3f800000);
1746         assert_eq!((12.5f32).to_bits(), 0x41480000);
1747         assert_eq!((1337f32).to_bits(), 0x44a72000);
1748         assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1749         assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1750         assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1751         assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1752         assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1753     }
1754     #[test]
1755     fn test_snan_masking() {
1756         // NOTE: this test assumes that our current platform
1757         // implements IEEE 754-2008 that specifies the difference
1758         // in encoding of quiet and signaling NaNs.
1759         // If you are porting Rust to a platform that does not
1760         // implement IEEE 754-2008 (but e.g. IEEE 754-1985, which
1761         // only says that "Signaling NaNs shall be reserved operands"
1762         // but doesn't specify the actual setup), feel free to
1763         // cfg out this test.
1764         let snan: u32 = 0x7F801337;
1765         const QNAN_MASK: u32  = 0x00400000;
1766         let nan_masked_fl = f32::from_bits(snan);
1767         let nan_masked = nan_masked_fl.to_bits();
1768         // Ensure that signaling NaNs don't stay the same
1769         assert_ne!(nan_masked, snan);
1770         // Ensure that we have a quiet NaN
1771         assert_ne!(nan_masked & QNAN_MASK, 0);
1772         assert!(nan_masked_fl.is_nan());
1773     }
1774 }