]> git.lizzy.rs Git - rust.git/blob - src/libstd/f32.rs
One must always remember to clean up after themselves
[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.
13 //!
14 //! *[See also the `f32` primitive type](../../std/primitive.f32.html).*
15 //!
16 //! Mathematically significant numbers are provided in the `consts` sub-module.
17
18 #![stable(feature = "rust1", since = "1.0.0")]
19 #![allow(missing_docs)]
20
21 #[cfg(not(test))]
22 #[cfg(stage0)]
23 use core::num::Float;
24 #[cfg(not(test))]
25 use intrinsics;
26 #[cfg(not(test))]
27 #[cfg(stage0)]
28 use num::FpCategory;
29 #[cfg(not(test))]
30 use sys::cmath;
31
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
34 #[stable(feature = "rust1", since = "1.0.0")]
35 pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
36 #[stable(feature = "rust1", since = "1.0.0")]
37 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
38 #[stable(feature = "rust1", since = "1.0.0")]
39 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
40 #[stable(feature = "rust1", since = "1.0.0")]
41 pub use core::f32::consts;
42
43 #[cfg(not(test))]
44 #[cfg_attr(stage0, lang = "f32")]
45 #[cfg_attr(not(stage0), lang = "f32_runtime")]
46 impl f32 {
47     #[cfg(stage0)]
48     f32_core_methods!();
49
50     /// Returns the largest integer less than or equal to a number.
51     ///
52     /// ```
53     /// let f = 3.99_f32;
54     /// let g = 3.0_f32;
55     ///
56     /// assert_eq!(f.floor(), 3.0);
57     /// assert_eq!(g.floor(), 3.0);
58     /// ```
59     #[stable(feature = "rust1", since = "1.0.0")]
60     #[inline]
61     pub fn floor(self) -> f32 {
62         // On MSVC LLVM will lower many math intrinsics to a call to the
63         // corresponding function. On MSVC, however, many of these functions
64         // aren't actually available as symbols to call, but rather they are all
65         // `static inline` functions in header files. This means that from a C
66         // perspective it's "compatible", but not so much from an ABI
67         // perspective (which we're worried about).
68         //
69         // The inline header functions always just cast to a f64 and do their
70         // operation, so we do that here as well, but only for MSVC targets.
71         //
72         // Note that there are many MSVC-specific float operations which
73         // redirect to this comment, so `floorf` is just one case of a missing
74         // function on MSVC, but there are many others elsewhere.
75         #[cfg(target_env = "msvc")]
76         return (self as f64).floor() as f32;
77         #[cfg(not(target_env = "msvc"))]
78         return unsafe { intrinsics::floorf32(self) };
79     }
80
81     /// Returns the smallest integer greater than or equal to a number.
82     ///
83     /// ```
84     /// let f = 3.01_f32;
85     /// let g = 4.0_f32;
86     ///
87     /// assert_eq!(f.ceil(), 4.0);
88     /// assert_eq!(g.ceil(), 4.0);
89     /// ```
90     #[stable(feature = "rust1", since = "1.0.0")]
91     #[inline]
92     pub fn ceil(self) -> f32 {
93         // see notes above in `floor`
94         #[cfg(target_env = "msvc")]
95         return (self as f64).ceil() as f32;
96         #[cfg(not(target_env = "msvc"))]
97         return unsafe { intrinsics::ceilf32(self) };
98     }
99
100     /// Returns the nearest integer to a number. Round half-way cases away from
101     /// `0.0`.
102     ///
103     /// ```
104     /// let f = 3.3_f32;
105     /// let g = -3.3_f32;
106     ///
107     /// assert_eq!(f.round(), 3.0);
108     /// assert_eq!(g.round(), -3.0);
109     /// ```
110     #[stable(feature = "rust1", since = "1.0.0")]
111     #[inline]
112     pub fn round(self) -> f32 {
113         unsafe { intrinsics::roundf32(self) }
114     }
115
116     /// Returns the integer part of a number.
117     ///
118     /// ```
119     /// let f = 3.3_f32;
120     /// let g = -3.7_f32;
121     ///
122     /// assert_eq!(f.trunc(), 3.0);
123     /// assert_eq!(g.trunc(), -3.0);
124     /// ```
125     #[stable(feature = "rust1", since = "1.0.0")]
126     #[inline]
127     pub fn trunc(self) -> f32 {
128         unsafe { intrinsics::truncf32(self) }
129     }
130
131     /// Returns the fractional part of a number.
132     ///
133     /// ```
134     /// use std::f32;
135     ///
136     /// let x = 3.5_f32;
137     /// let y = -3.5_f32;
138     /// let abs_difference_x = (x.fract() - 0.5).abs();
139     /// let abs_difference_y = (y.fract() - (-0.5)).abs();
140     ///
141     /// assert!(abs_difference_x <= f32::EPSILON);
142     /// assert!(abs_difference_y <= f32::EPSILON);
143     /// ```
144     #[stable(feature = "rust1", since = "1.0.0")]
145     #[inline]
146     pub fn fract(self) -> f32 { self - self.trunc() }
147
148     /// Computes the absolute value of `self`. Returns `NAN` if the
149     /// number is `NAN`.
150     ///
151     /// ```
152     /// use std::f32;
153     ///
154     /// let x = 3.5_f32;
155     /// let y = -3.5_f32;
156     ///
157     /// let abs_difference_x = (x.abs() - x).abs();
158     /// let abs_difference_y = (y.abs() - (-y)).abs();
159     ///
160     /// assert!(abs_difference_x <= f32::EPSILON);
161     /// assert!(abs_difference_y <= f32::EPSILON);
162     ///
163     /// assert!(f32::NAN.abs().is_nan());
164     /// ```
165     #[stable(feature = "rust1", since = "1.0.0")]
166     #[inline]
167     pub fn abs(self) -> f32 {
168         unsafe { intrinsics::fabsf32(self) }
169     }
170
171     /// Returns a number that represents the sign of `self`.
172     ///
173     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
174     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
175     /// - `NAN` if the number is `NAN`
176     ///
177     /// ```
178     /// use std::f32;
179     ///
180     /// let f = 3.5_f32;
181     ///
182     /// assert_eq!(f.signum(), 1.0);
183     /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
184     ///
185     /// assert!(f32::NAN.signum().is_nan());
186     /// ```
187     #[stable(feature = "rust1", since = "1.0.0")]
188     #[inline]
189     pub fn signum(self) -> f32 {
190         if self.is_nan() {
191             NAN
192         } else {
193             unsafe { intrinsics::copysignf32(1.0, self) }
194         }
195     }
196
197     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
198     /// error, yielding a more accurate result than an unfused multiply-add.
199     ///
200     /// Using `mul_add` can be more performant than an unfused multiply-add if
201     /// the target architecture has a dedicated `fma` CPU instruction.
202     ///
203     /// ```
204     /// use std::f32;
205     ///
206     /// let m = 10.0_f32;
207     /// let x = 4.0_f32;
208     /// let b = 60.0_f32;
209     ///
210     /// // 100.0
211     /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
212     ///
213     /// assert!(abs_difference <= f32::EPSILON);
214     /// ```
215     #[stable(feature = "rust1", since = "1.0.0")]
216     #[inline]
217     pub fn mul_add(self, a: f32, b: f32) -> f32 {
218         unsafe { intrinsics::fmaf32(self, a, b) }
219     }
220
221     /// Calculates Euclidean division, the matching method for `mod_euc`.
222     ///
223     /// This computes the integer `n` such that
224     /// `self = n * rhs + self.mod_euc(rhs)`.
225     /// In other words, the result is `self / rhs` rounded to the integer `n`
226     /// such that `self >= n * rhs`.
227     ///
228     /// ```
229     /// #![feature(euclidean_division)]
230     /// let a: f32 = 7.0;
231     /// let b = 4.0;
232     /// assert_eq!(a.div_euc(b), 1.0); // 7.0 > 4.0 * 1.0
233     /// assert_eq!((-a).div_euc(b), -2.0); // -7.0 >= 4.0 * -2.0
234     /// assert_eq!(a.div_euc(-b), -1.0); // 7.0 >= -4.0 * -1.0
235     /// assert_eq!((-a).div_euc(-b), 2.0); // -7.0 >= -4.0 * 2.0
236     /// ```
237     #[inline]
238     #[unstable(feature = "euclidean_division", issue = "49048")]
239     pub fn div_euc(self, rhs: f32) -> f32 {
240         let q = (self / rhs).trunc();
241         if self % rhs < 0.0 {
242             return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
243         }
244         q
245     }
246
247     /// Calculates the Euclidean modulo (self mod rhs), which is never negative.
248     ///
249     /// In particular, the result `n` satisfies `0 <= n < rhs.abs()`.
250     ///
251     /// ```
252     /// #![feature(euclidean_division)]
253     /// let a: f32 = 7.0;
254     /// let b = 4.0;
255     /// assert_eq!(a.mod_euc(b), 3.0);
256     /// assert_eq!((-a).mod_euc(b), 1.0);
257     /// assert_eq!(a.mod_euc(-b), 3.0);
258     /// assert_eq!((-a).mod_euc(-b), 1.0);
259     /// ```
260     #[inline]
261     #[unstable(feature = "euclidean_division", issue = "49048")]
262     pub fn mod_euc(self, rhs: f32) -> f32 {
263         let r = self % rhs;
264         if r < 0.0 {
265             r + rhs.abs()
266         } else {
267             r
268         }
269     }
270
271
272     /// Raises a number to an integer power.
273     ///
274     /// Using this function is generally faster than using `powf`
275     ///
276     /// ```
277     /// use std::f32;
278     ///
279     /// let x = 2.0_f32;
280     /// let abs_difference = (x.powi(2) - x*x).abs();
281     ///
282     /// assert!(abs_difference <= f32::EPSILON);
283     /// ```
284     #[stable(feature = "rust1", since = "1.0.0")]
285     #[inline]
286     pub fn powi(self, n: i32) -> f32 {
287         unsafe { intrinsics::powif32(self, n) }
288     }
289
290     /// Raises a number to a floating point power.
291     ///
292     /// ```
293     /// use std::f32;
294     ///
295     /// let x = 2.0_f32;
296     /// let abs_difference = (x.powf(2.0) - x*x).abs();
297     ///
298     /// assert!(abs_difference <= f32::EPSILON);
299     /// ```
300     #[stable(feature = "rust1", since = "1.0.0")]
301     #[inline]
302     pub fn powf(self, n: f32) -> f32 {
303         // see notes above in `floor`
304         #[cfg(target_env = "msvc")]
305         return (self as f64).powf(n as f64) as f32;
306         #[cfg(not(target_env = "msvc"))]
307         return unsafe { intrinsics::powf32(self, n) };
308     }
309
310     /// Takes the square root of a number.
311     ///
312     /// Returns NaN if `self` is a negative number.
313     ///
314     /// ```
315     /// use std::f32;
316     ///
317     /// let positive = 4.0_f32;
318     /// let negative = -4.0_f32;
319     ///
320     /// let abs_difference = (positive.sqrt() - 2.0).abs();
321     ///
322     /// assert!(abs_difference <= f32::EPSILON);
323     /// assert!(negative.sqrt().is_nan());
324     /// ```
325     #[stable(feature = "rust1", since = "1.0.0")]
326     #[inline]
327     pub fn sqrt(self) -> f32 {
328         if self < 0.0 {
329             NAN
330         } else {
331             unsafe { intrinsics::sqrtf32(self) }
332         }
333     }
334
335     /// Returns `e^(self)`, (the exponential function).
336     ///
337     /// ```
338     /// use std::f32;
339     ///
340     /// let one = 1.0f32;
341     /// // e^1
342     /// let e = one.exp();
343     ///
344     /// // ln(e) - 1 == 0
345     /// let abs_difference = (e.ln() - 1.0).abs();
346     ///
347     /// assert!(abs_difference <= f32::EPSILON);
348     /// ```
349     #[stable(feature = "rust1", since = "1.0.0")]
350     #[inline]
351     pub fn exp(self) -> f32 {
352         // see notes above in `floor`
353         #[cfg(target_env = "msvc")]
354         return (self as f64).exp() as f32;
355         #[cfg(not(target_env = "msvc"))]
356         return unsafe { intrinsics::expf32(self) };
357     }
358
359     /// Returns `2^(self)`.
360     ///
361     /// ```
362     /// use std::f32;
363     ///
364     /// let f = 2.0f32;
365     ///
366     /// // 2^2 - 4 == 0
367     /// let abs_difference = (f.exp2() - 4.0).abs();
368     ///
369     /// assert!(abs_difference <= f32::EPSILON);
370     /// ```
371     #[stable(feature = "rust1", since = "1.0.0")]
372     #[inline]
373     pub fn exp2(self) -> f32 {
374         unsafe { intrinsics::exp2f32(self) }
375     }
376
377     /// Returns the natural logarithm of the number.
378     ///
379     /// ```
380     /// use std::f32;
381     ///
382     /// let one = 1.0f32;
383     /// // e^1
384     /// let e = one.exp();
385     ///
386     /// // ln(e) - 1 == 0
387     /// let abs_difference = (e.ln() - 1.0).abs();
388     ///
389     /// assert!(abs_difference <= f32::EPSILON);
390     /// ```
391     #[stable(feature = "rust1", since = "1.0.0")]
392     #[inline]
393     pub fn ln(self) -> f32 {
394         // see notes above in `floor`
395         #[cfg(target_env = "msvc")]
396         return (self as f64).ln() as f32;
397         #[cfg(not(target_env = "msvc"))]
398         return unsafe { intrinsics::logf32(self) };
399     }
400
401     /// Returns the logarithm of the number with respect to an arbitrary base.
402     ///
403     /// The result may not be correctly rounded owing to implementation details;
404     /// `self.log2()` can produce more accurate results for base 2, and
405     /// `self.log10()` can produce more accurate results for base 10.
406     ///
407     /// ```
408     /// use std::f32;
409     ///
410     /// let five = 5.0f32;
411     ///
412     /// // log5(5) - 1 == 0
413     /// let abs_difference = (five.log(5.0) - 1.0).abs();
414     ///
415     /// assert!(abs_difference <= f32::EPSILON);
416     /// ```
417     #[stable(feature = "rust1", since = "1.0.0")]
418     #[inline]
419     pub fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
420
421     /// Returns the base 2 logarithm of the number.
422     ///
423     /// ```
424     /// use std::f32;
425     ///
426     /// let two = 2.0f32;
427     ///
428     /// // log2(2) - 1 == 0
429     /// let abs_difference = (two.log2() - 1.0).abs();
430     ///
431     /// assert!(abs_difference <= f32::EPSILON);
432     /// ```
433     #[stable(feature = "rust1", since = "1.0.0")]
434     #[inline]
435     pub fn log2(self) -> f32 {
436         #[cfg(target_os = "android")]
437         return ::sys::android::log2f32(self);
438         #[cfg(not(target_os = "android"))]
439         return unsafe { intrinsics::log2f32(self) };
440     }
441
442     /// Returns the base 10 logarithm of the number.
443     ///
444     /// ```
445     /// use std::f32;
446     ///
447     /// let ten = 10.0f32;
448     ///
449     /// // log10(10) - 1 == 0
450     /// let abs_difference = (ten.log10() - 1.0).abs();
451     ///
452     /// assert!(abs_difference <= f32::EPSILON);
453     /// ```
454     #[stable(feature = "rust1", since = "1.0.0")]
455     #[inline]
456     pub fn log10(self) -> f32 {
457         // see notes above in `floor`
458         #[cfg(target_env = "msvc")]
459         return (self as f64).log10() as f32;
460         #[cfg(not(target_env = "msvc"))]
461         return unsafe { intrinsics::log10f32(self) };
462     }
463
464     /// The positive difference of two numbers.
465     ///
466     /// * If `self <= other`: `0:0`
467     /// * Else: `self - other`
468     ///
469     /// ```
470     /// use std::f32;
471     ///
472     /// let x = 3.0f32;
473     /// let y = -3.0f32;
474     ///
475     /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
476     /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
477     ///
478     /// assert!(abs_difference_x <= f32::EPSILON);
479     /// assert!(abs_difference_y <= f32::EPSILON);
480     /// ```
481     #[stable(feature = "rust1", since = "1.0.0")]
482     #[inline]
483     #[rustc_deprecated(since = "1.10.0",
484                        reason = "you probably meant `(self - other).abs()`: \
485                                  this operation is `(self - other).max(0.0)` (also \
486                                  known as `fdimf` in C). If you truly need the positive \
487                                  difference, consider using that expression or the C function \
488                                  `fdimf`, depending on how you wish to handle NaN (please consider \
489                                  filing an issue describing your use-case too).")]
490     pub fn abs_sub(self, other: f32) -> f32 {
491         unsafe { cmath::fdimf(self, other) }
492     }
493
494     /// Takes the cubic root of a number.
495     ///
496     /// ```
497     /// use std::f32;
498     ///
499     /// let x = 8.0f32;
500     ///
501     /// // x^(1/3) - 2 == 0
502     /// let abs_difference = (x.cbrt() - 2.0).abs();
503     ///
504     /// assert!(abs_difference <= f32::EPSILON);
505     /// ```
506     #[stable(feature = "rust1", since = "1.0.0")]
507     #[inline]
508     pub fn cbrt(self) -> f32 {
509         unsafe { cmath::cbrtf(self) }
510     }
511
512     /// Calculates the length of the hypotenuse of a right-angle triangle given
513     /// legs of length `x` and `y`.
514     ///
515     /// ```
516     /// use std::f32;
517     ///
518     /// let x = 2.0f32;
519     /// let y = 3.0f32;
520     ///
521     /// // sqrt(x^2 + y^2)
522     /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
523     ///
524     /// assert!(abs_difference <= f32::EPSILON);
525     /// ```
526     #[stable(feature = "rust1", since = "1.0.0")]
527     #[inline]
528     pub fn hypot(self, other: f32) -> f32 {
529         unsafe { cmath::hypotf(self, other) }
530     }
531
532     /// Computes the sine of a number (in radians).
533     ///
534     /// ```
535     /// use std::f32;
536     ///
537     /// let x = f32::consts::PI/2.0;
538     ///
539     /// let abs_difference = (x.sin() - 1.0).abs();
540     ///
541     /// assert!(abs_difference <= f32::EPSILON);
542     /// ```
543     #[stable(feature = "rust1", since = "1.0.0")]
544     #[inline]
545     pub fn sin(self) -> f32 {
546         // see notes in `core::f32::Float::floor`
547         #[cfg(target_env = "msvc")]
548         return (self as f64).sin() as f32;
549         #[cfg(not(target_env = "msvc"))]
550         return unsafe { intrinsics::sinf32(self) };
551     }
552
553     /// Computes the cosine of a number (in radians).
554     ///
555     /// ```
556     /// use std::f32;
557     ///
558     /// let x = 2.0*f32::consts::PI;
559     ///
560     /// let abs_difference = (x.cos() - 1.0).abs();
561     ///
562     /// assert!(abs_difference <= f32::EPSILON);
563     /// ```
564     #[stable(feature = "rust1", since = "1.0.0")]
565     #[inline]
566     pub fn cos(self) -> f32 {
567         // see notes in `core::f32::Float::floor`
568         #[cfg(target_env = "msvc")]
569         return (self as f64).cos() as f32;
570         #[cfg(not(target_env = "msvc"))]
571         return unsafe { intrinsics::cosf32(self) };
572     }
573
574     /// Computes the tangent of a number (in radians).
575     ///
576     /// ```
577     /// use std::f32;
578     ///
579     /// let x = f32::consts::PI / 4.0;
580     /// let abs_difference = (x.tan() - 1.0).abs();
581     ///
582     /// assert!(abs_difference <= f32::EPSILON);
583     /// ```
584     #[stable(feature = "rust1", since = "1.0.0")]
585     #[inline]
586     pub fn tan(self) -> f32 {
587         unsafe { cmath::tanf(self) }
588     }
589
590     /// Computes the arcsine of a number. Return value is in radians in
591     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
592     /// [-1, 1].
593     ///
594     /// ```
595     /// use std::f32;
596     ///
597     /// let f = f32::consts::PI / 2.0;
598     ///
599     /// // asin(sin(pi/2))
600     /// let abs_difference = (f.sin().asin() - f32::consts::PI / 2.0).abs();
601     ///
602     /// assert!(abs_difference <= f32::EPSILON);
603     /// ```
604     #[stable(feature = "rust1", since = "1.0.0")]
605     #[inline]
606     pub fn asin(self) -> f32 {
607         unsafe { cmath::asinf(self) }
608     }
609
610     /// Computes the arccosine of a number. Return value is in radians in
611     /// the range [0, pi] or NaN if the number is outside the range
612     /// [-1, 1].
613     ///
614     /// ```
615     /// use std::f32;
616     ///
617     /// let f = f32::consts::PI / 4.0;
618     ///
619     /// // acos(cos(pi/4))
620     /// let abs_difference = (f.cos().acos() - f32::consts::PI / 4.0).abs();
621     ///
622     /// assert!(abs_difference <= f32::EPSILON);
623     /// ```
624     #[stable(feature = "rust1", since = "1.0.0")]
625     #[inline]
626     pub fn acos(self) -> f32 {
627         unsafe { cmath::acosf(self) }
628     }
629
630     /// Computes the arctangent of a number. Return value is in radians in the
631     /// range [-pi/2, pi/2];
632     ///
633     /// ```
634     /// use std::f32;
635     ///
636     /// let f = 1.0f32;
637     ///
638     /// // atan(tan(1))
639     /// let abs_difference = (f.tan().atan() - 1.0).abs();
640     ///
641     /// assert!(abs_difference <= f32::EPSILON);
642     /// ```
643     #[stable(feature = "rust1", since = "1.0.0")]
644     #[inline]
645     pub fn atan(self) -> f32 {
646         unsafe { cmath::atanf(self) }
647     }
648
649     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`) in radians.
650     ///
651     /// * `x = 0`, `y = 0`: `0`
652     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
653     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
654     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
655     ///
656     /// ```
657     /// use std::f32;
658     ///
659     /// let pi = f32::consts::PI;
660     /// // Positive angles measured counter-clockwise
661     /// // from positive x axis
662     /// // -pi/4 radians (45 deg clockwise)
663     /// let x1 = 3.0f32;
664     /// let y1 = -3.0f32;
665     ///
666     /// // 3pi/4 radians (135 deg counter-clockwise)
667     /// let x2 = -3.0f32;
668     /// let y2 = 3.0f32;
669     ///
670     /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
671     /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
672     ///
673     /// assert!(abs_difference_1 <= f32::EPSILON);
674     /// assert!(abs_difference_2 <= f32::EPSILON);
675     /// ```
676     #[stable(feature = "rust1", since = "1.0.0")]
677     #[inline]
678     pub fn atan2(self, other: f32) -> f32 {
679         unsafe { cmath::atan2f(self, other) }
680     }
681
682     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
683     /// `(sin(x), cos(x))`.
684     ///
685     /// ```
686     /// use std::f32;
687     ///
688     /// let x = f32::consts::PI/4.0;
689     /// let f = x.sin_cos();
690     ///
691     /// let abs_difference_0 = (f.0 - x.sin()).abs();
692     /// let abs_difference_1 = (f.1 - x.cos()).abs();
693     ///
694     /// assert!(abs_difference_0 <= f32::EPSILON);
695     /// assert!(abs_difference_1 <= f32::EPSILON);
696     /// ```
697     #[stable(feature = "rust1", since = "1.0.0")]
698     #[inline]
699     pub fn sin_cos(self) -> (f32, f32) {
700         (self.sin(), self.cos())
701     }
702
703     /// Returns `e^(self) - 1` in a way that is accurate even if the
704     /// number is close to zero.
705     ///
706     /// ```
707     /// use std::f32;
708     ///
709     /// let x = 6.0f32;
710     ///
711     /// // e^(ln(6)) - 1
712     /// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
713     ///
714     /// assert!(abs_difference <= f32::EPSILON);
715     /// ```
716     #[stable(feature = "rust1", since = "1.0.0")]
717     #[inline]
718     pub fn exp_m1(self) -> f32 {
719         unsafe { cmath::expm1f(self) }
720     }
721
722     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
723     /// the operations were performed separately.
724     ///
725     /// ```
726     /// use std::f32;
727     ///
728     /// let x = f32::consts::E - 1.0;
729     ///
730     /// // ln(1 + (e - 1)) == ln(e) == 1
731     /// let abs_difference = (x.ln_1p() - 1.0).abs();
732     ///
733     /// assert!(abs_difference <= f32::EPSILON);
734     /// ```
735     #[stable(feature = "rust1", since = "1.0.0")]
736     #[inline]
737     pub fn ln_1p(self) -> f32 {
738         unsafe { cmath::log1pf(self) }
739     }
740
741     /// Hyperbolic sine function.
742     ///
743     /// ```
744     /// use std::f32;
745     ///
746     /// let e = f32::consts::E;
747     /// let x = 1.0f32;
748     ///
749     /// let f = x.sinh();
750     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
751     /// let g = (e*e - 1.0)/(2.0*e);
752     /// let abs_difference = (f - g).abs();
753     ///
754     /// assert!(abs_difference <= f32::EPSILON);
755     /// ```
756     #[stable(feature = "rust1", since = "1.0.0")]
757     #[inline]
758     pub fn sinh(self) -> f32 {
759         unsafe { cmath::sinhf(self) }
760     }
761
762     /// Hyperbolic cosine function.
763     ///
764     /// ```
765     /// use std::f32;
766     ///
767     /// let e = f32::consts::E;
768     /// let x = 1.0f32;
769     /// let f = x.cosh();
770     /// // Solving cosh() at 1 gives this result
771     /// let g = (e*e + 1.0)/(2.0*e);
772     /// let abs_difference = (f - g).abs();
773     ///
774     /// // Same result
775     /// assert!(abs_difference <= f32::EPSILON);
776     /// ```
777     #[stable(feature = "rust1", since = "1.0.0")]
778     #[inline]
779     pub fn cosh(self) -> f32 {
780         unsafe { cmath::coshf(self) }
781     }
782
783     /// Hyperbolic tangent function.
784     ///
785     /// ```
786     /// use std::f32;
787     ///
788     /// let e = f32::consts::E;
789     /// let x = 1.0f32;
790     ///
791     /// let f = x.tanh();
792     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
793     /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
794     /// let abs_difference = (f - g).abs();
795     ///
796     /// assert!(abs_difference <= f32::EPSILON);
797     /// ```
798     #[stable(feature = "rust1", since = "1.0.0")]
799     #[inline]
800     pub fn tanh(self) -> f32 {
801         unsafe { cmath::tanhf(self) }
802     }
803
804     /// Inverse hyperbolic sine function.
805     ///
806     /// ```
807     /// use std::f32;
808     ///
809     /// let x = 1.0f32;
810     /// let f = x.sinh().asinh();
811     ///
812     /// let abs_difference = (f - x).abs();
813     ///
814     /// assert!(abs_difference <= f32::EPSILON);
815     /// ```
816     #[stable(feature = "rust1", since = "1.0.0")]
817     #[inline]
818     pub fn asinh(self) -> f32 {
819         if self == NEG_INFINITY {
820             NEG_INFINITY
821         } else {
822             (self + ((self * self) + 1.0).sqrt()).ln()
823         }
824     }
825
826     /// Inverse hyperbolic cosine function.
827     ///
828     /// ```
829     /// use std::f32;
830     ///
831     /// let x = 1.0f32;
832     /// let f = x.cosh().acosh();
833     ///
834     /// let abs_difference = (f - x).abs();
835     ///
836     /// assert!(abs_difference <= f32::EPSILON);
837     /// ```
838     #[stable(feature = "rust1", since = "1.0.0")]
839     #[inline]
840     pub fn acosh(self) -> f32 {
841         match self {
842             x if x < 1.0 => ::f32::NAN,
843             x => (x + ((x * x) - 1.0).sqrt()).ln(),
844         }
845     }
846
847     /// Inverse hyperbolic tangent function.
848     ///
849     /// ```
850     /// use std::f32;
851     ///
852     /// let e = f32::consts::E;
853     /// let f = e.tanh().atanh();
854     ///
855     /// let abs_difference = (f - e).abs();
856     ///
857     /// assert!(abs_difference <= 1e-5);
858     /// ```
859     #[stable(feature = "rust1", since = "1.0.0")]
860     #[inline]
861     pub fn atanh(self) -> f32 {
862         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
863     }
864 }
865
866 #[cfg(test)]
867 mod tests {
868     use f32;
869     use f32::*;
870     use num::*;
871     use num::FpCategory as Fp;
872
873     #[test]
874     fn test_num_f32() {
875         test_num(10f32, 2f32);
876     }
877
878     #[test]
879     fn test_min_nan() {
880         assert_eq!(NAN.min(2.0), 2.0);
881         assert_eq!(2.0f32.min(NAN), 2.0);
882     }
883
884     #[test]
885     fn test_max_nan() {
886         assert_eq!(NAN.max(2.0), 2.0);
887         assert_eq!(2.0f32.max(NAN), 2.0);
888     }
889
890     #[test]
891     fn test_nan() {
892         let nan: f32 = f32::NAN;
893         assert!(nan.is_nan());
894         assert!(!nan.is_infinite());
895         assert!(!nan.is_finite());
896         assert!(!nan.is_normal());
897         assert!(nan.is_sign_positive());
898         assert!(!nan.is_sign_negative());
899         assert_eq!(Fp::Nan, nan.classify());
900     }
901
902     #[test]
903     fn test_infinity() {
904         let inf: f32 = f32::INFINITY;
905         assert!(inf.is_infinite());
906         assert!(!inf.is_finite());
907         assert!(inf.is_sign_positive());
908         assert!(!inf.is_sign_negative());
909         assert!(!inf.is_nan());
910         assert!(!inf.is_normal());
911         assert_eq!(Fp::Infinite, inf.classify());
912     }
913
914     #[test]
915     fn test_neg_infinity() {
916         let neg_inf: f32 = f32::NEG_INFINITY;
917         assert!(neg_inf.is_infinite());
918         assert!(!neg_inf.is_finite());
919         assert!(!neg_inf.is_sign_positive());
920         assert!(neg_inf.is_sign_negative());
921         assert!(!neg_inf.is_nan());
922         assert!(!neg_inf.is_normal());
923         assert_eq!(Fp::Infinite, neg_inf.classify());
924     }
925
926     #[test]
927     fn test_zero() {
928         let zero: f32 = 0.0f32;
929         assert_eq!(0.0, zero);
930         assert!(!zero.is_infinite());
931         assert!(zero.is_finite());
932         assert!(zero.is_sign_positive());
933         assert!(!zero.is_sign_negative());
934         assert!(!zero.is_nan());
935         assert!(!zero.is_normal());
936         assert_eq!(Fp::Zero, zero.classify());
937     }
938
939     #[test]
940     fn test_neg_zero() {
941         let neg_zero: f32 = -0.0;
942         assert_eq!(0.0, neg_zero);
943         assert!(!neg_zero.is_infinite());
944         assert!(neg_zero.is_finite());
945         assert!(!neg_zero.is_sign_positive());
946         assert!(neg_zero.is_sign_negative());
947         assert!(!neg_zero.is_nan());
948         assert!(!neg_zero.is_normal());
949         assert_eq!(Fp::Zero, neg_zero.classify());
950     }
951
952     #[test]
953     fn test_one() {
954         let one: f32 = 1.0f32;
955         assert_eq!(1.0, one);
956         assert!(!one.is_infinite());
957         assert!(one.is_finite());
958         assert!(one.is_sign_positive());
959         assert!(!one.is_sign_negative());
960         assert!(!one.is_nan());
961         assert!(one.is_normal());
962         assert_eq!(Fp::Normal, one.classify());
963     }
964
965     #[test]
966     fn test_is_nan() {
967         let nan: f32 = f32::NAN;
968         let inf: f32 = f32::INFINITY;
969         let neg_inf: f32 = f32::NEG_INFINITY;
970         assert!(nan.is_nan());
971         assert!(!0.0f32.is_nan());
972         assert!(!5.3f32.is_nan());
973         assert!(!(-10.732f32).is_nan());
974         assert!(!inf.is_nan());
975         assert!(!neg_inf.is_nan());
976     }
977
978     #[test]
979     fn test_is_infinite() {
980         let nan: f32 = f32::NAN;
981         let inf: f32 = f32::INFINITY;
982         let neg_inf: f32 = f32::NEG_INFINITY;
983         assert!(!nan.is_infinite());
984         assert!(inf.is_infinite());
985         assert!(neg_inf.is_infinite());
986         assert!(!0.0f32.is_infinite());
987         assert!(!42.8f32.is_infinite());
988         assert!(!(-109.2f32).is_infinite());
989     }
990
991     #[test]
992     fn test_is_finite() {
993         let nan: f32 = f32::NAN;
994         let inf: f32 = f32::INFINITY;
995         let neg_inf: f32 = f32::NEG_INFINITY;
996         assert!(!nan.is_finite());
997         assert!(!inf.is_finite());
998         assert!(!neg_inf.is_finite());
999         assert!(0.0f32.is_finite());
1000         assert!(42.8f32.is_finite());
1001         assert!((-109.2f32).is_finite());
1002     }
1003
1004     #[test]
1005     fn test_is_normal() {
1006         let nan: f32 = f32::NAN;
1007         let inf: f32 = f32::INFINITY;
1008         let neg_inf: f32 = f32::NEG_INFINITY;
1009         let zero: f32 = 0.0f32;
1010         let neg_zero: f32 = -0.0;
1011         assert!(!nan.is_normal());
1012         assert!(!inf.is_normal());
1013         assert!(!neg_inf.is_normal());
1014         assert!(!zero.is_normal());
1015         assert!(!neg_zero.is_normal());
1016         assert!(1f32.is_normal());
1017         assert!(1e-37f32.is_normal());
1018         assert!(!1e-38f32.is_normal());
1019     }
1020
1021     #[test]
1022     fn test_classify() {
1023         let nan: f32 = f32::NAN;
1024         let inf: f32 = f32::INFINITY;
1025         let neg_inf: f32 = f32::NEG_INFINITY;
1026         let zero: f32 = 0.0f32;
1027         let neg_zero: f32 = -0.0;
1028         assert_eq!(nan.classify(), Fp::Nan);
1029         assert_eq!(inf.classify(), Fp::Infinite);
1030         assert_eq!(neg_inf.classify(), Fp::Infinite);
1031         assert_eq!(zero.classify(), Fp::Zero);
1032         assert_eq!(neg_zero.classify(), Fp::Zero);
1033         assert_eq!(1f32.classify(), Fp::Normal);
1034         assert_eq!(1e-37f32.classify(), Fp::Normal);
1035         assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1036     }
1037
1038     #[test]
1039     fn test_floor() {
1040         assert_approx_eq!(1.0f32.floor(), 1.0f32);
1041         assert_approx_eq!(1.3f32.floor(), 1.0f32);
1042         assert_approx_eq!(1.5f32.floor(), 1.0f32);
1043         assert_approx_eq!(1.7f32.floor(), 1.0f32);
1044         assert_approx_eq!(0.0f32.floor(), 0.0f32);
1045         assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1046         assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1047         assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1048         assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1049         assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1050     }
1051
1052     #[test]
1053     fn test_ceil() {
1054         assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1055         assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1056         assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1057         assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1058         assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1059         assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1060         assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1061         assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1062         assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1063         assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1064     }
1065
1066     #[test]
1067     fn test_round() {
1068         assert_approx_eq!(1.0f32.round(), 1.0f32);
1069         assert_approx_eq!(1.3f32.round(), 1.0f32);
1070         assert_approx_eq!(1.5f32.round(), 2.0f32);
1071         assert_approx_eq!(1.7f32.round(), 2.0f32);
1072         assert_approx_eq!(0.0f32.round(), 0.0f32);
1073         assert_approx_eq!((-0.0f32).round(), -0.0f32);
1074         assert_approx_eq!((-1.0f32).round(), -1.0f32);
1075         assert_approx_eq!((-1.3f32).round(), -1.0f32);
1076         assert_approx_eq!((-1.5f32).round(), -2.0f32);
1077         assert_approx_eq!((-1.7f32).round(), -2.0f32);
1078     }
1079
1080     #[test]
1081     fn test_trunc() {
1082         assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1083         assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1084         assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1085         assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1086         assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1087         assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1088         assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1089         assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1090         assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1091         assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1092     }
1093
1094     #[test]
1095     fn test_fract() {
1096         assert_approx_eq!(1.0f32.fract(), 0.0f32);
1097         assert_approx_eq!(1.3f32.fract(), 0.3f32);
1098         assert_approx_eq!(1.5f32.fract(), 0.5f32);
1099         assert_approx_eq!(1.7f32.fract(), 0.7f32);
1100         assert_approx_eq!(0.0f32.fract(), 0.0f32);
1101         assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1102         assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1103         assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1104         assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1105         assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1106     }
1107
1108     #[test]
1109     fn test_abs() {
1110         assert_eq!(INFINITY.abs(), INFINITY);
1111         assert_eq!(1f32.abs(), 1f32);
1112         assert_eq!(0f32.abs(), 0f32);
1113         assert_eq!((-0f32).abs(), 0f32);
1114         assert_eq!((-1f32).abs(), 1f32);
1115         assert_eq!(NEG_INFINITY.abs(), INFINITY);
1116         assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1117         assert!(NAN.abs().is_nan());
1118     }
1119
1120     #[test]
1121     fn test_signum() {
1122         assert_eq!(INFINITY.signum(), 1f32);
1123         assert_eq!(1f32.signum(), 1f32);
1124         assert_eq!(0f32.signum(), 1f32);
1125         assert_eq!((-0f32).signum(), -1f32);
1126         assert_eq!((-1f32).signum(), -1f32);
1127         assert_eq!(NEG_INFINITY.signum(), -1f32);
1128         assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1129         assert!(NAN.signum().is_nan());
1130     }
1131
1132     #[test]
1133     fn test_is_sign_positive() {
1134         assert!(INFINITY.is_sign_positive());
1135         assert!(1f32.is_sign_positive());
1136         assert!(0f32.is_sign_positive());
1137         assert!(!(-0f32).is_sign_positive());
1138         assert!(!(-1f32).is_sign_positive());
1139         assert!(!NEG_INFINITY.is_sign_positive());
1140         assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1141         assert!(NAN.is_sign_positive());
1142         assert!(!(-NAN).is_sign_positive());
1143     }
1144
1145     #[test]
1146     fn test_is_sign_negative() {
1147         assert!(!INFINITY.is_sign_negative());
1148         assert!(!1f32.is_sign_negative());
1149         assert!(!0f32.is_sign_negative());
1150         assert!((-0f32).is_sign_negative());
1151         assert!((-1f32).is_sign_negative());
1152         assert!(NEG_INFINITY.is_sign_negative());
1153         assert!((1f32/NEG_INFINITY).is_sign_negative());
1154         assert!(!NAN.is_sign_negative());
1155         assert!((-NAN).is_sign_negative());
1156     }
1157
1158     #[test]
1159     fn test_mul_add() {
1160         let nan: f32 = f32::NAN;
1161         let inf: f32 = f32::INFINITY;
1162         let neg_inf: f32 = f32::NEG_INFINITY;
1163         assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1164         assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1165         assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1166         assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1167         assert!(nan.mul_add(7.8, 9.0).is_nan());
1168         assert_eq!(inf.mul_add(7.8, 9.0), inf);
1169         assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1170         assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1171         assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1172     }
1173
1174     #[test]
1175     fn test_recip() {
1176         let nan: f32 = f32::NAN;
1177         let inf: f32 = f32::INFINITY;
1178         let neg_inf: f32 = f32::NEG_INFINITY;
1179         assert_eq!(1.0f32.recip(), 1.0);
1180         assert_eq!(2.0f32.recip(), 0.5);
1181         assert_eq!((-0.4f32).recip(), -2.5);
1182         assert_eq!(0.0f32.recip(), inf);
1183         assert!(nan.recip().is_nan());
1184         assert_eq!(inf.recip(), 0.0);
1185         assert_eq!(neg_inf.recip(), 0.0);
1186     }
1187
1188     #[test]
1189     fn test_powi() {
1190         let nan: f32 = f32::NAN;
1191         let inf: f32 = f32::INFINITY;
1192         let neg_inf: f32 = f32::NEG_INFINITY;
1193         assert_eq!(1.0f32.powi(1), 1.0);
1194         assert_approx_eq!((-3.1f32).powi(2), 9.61);
1195         assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1196         assert_eq!(8.3f32.powi(0), 1.0);
1197         assert!(nan.powi(2).is_nan());
1198         assert_eq!(inf.powi(3), inf);
1199         assert_eq!(neg_inf.powi(2), inf);
1200     }
1201
1202     #[test]
1203     fn test_powf() {
1204         let nan: f32 = f32::NAN;
1205         let inf: f32 = f32::INFINITY;
1206         let neg_inf: f32 = f32::NEG_INFINITY;
1207         assert_eq!(1.0f32.powf(1.0), 1.0);
1208         assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1209         assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1210         assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1211         assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1212         assert_eq!(8.3f32.powf(0.0), 1.0);
1213         assert!(nan.powf(2.0).is_nan());
1214         assert_eq!(inf.powf(2.0), inf);
1215         assert_eq!(neg_inf.powf(3.0), neg_inf);
1216     }
1217
1218     #[test]
1219     fn test_sqrt_domain() {
1220         assert!(NAN.sqrt().is_nan());
1221         assert!(NEG_INFINITY.sqrt().is_nan());
1222         assert!((-1.0f32).sqrt().is_nan());
1223         assert_eq!((-0.0f32).sqrt(), -0.0);
1224         assert_eq!(0.0f32.sqrt(), 0.0);
1225         assert_eq!(1.0f32.sqrt(), 1.0);
1226         assert_eq!(INFINITY.sqrt(), INFINITY);
1227     }
1228
1229     #[test]
1230     fn test_exp() {
1231         assert_eq!(1.0, 0.0f32.exp());
1232         assert_approx_eq!(2.718282, 1.0f32.exp());
1233         assert_approx_eq!(148.413162, 5.0f32.exp());
1234
1235         let inf: f32 = f32::INFINITY;
1236         let neg_inf: f32 = f32::NEG_INFINITY;
1237         let nan: f32 = f32::NAN;
1238         assert_eq!(inf, inf.exp());
1239         assert_eq!(0.0, neg_inf.exp());
1240         assert!(nan.exp().is_nan());
1241     }
1242
1243     #[test]
1244     fn test_exp2() {
1245         assert_eq!(32.0, 5.0f32.exp2());
1246         assert_eq!(1.0, 0.0f32.exp2());
1247
1248         let inf: f32 = f32::INFINITY;
1249         let neg_inf: f32 = f32::NEG_INFINITY;
1250         let nan: f32 = f32::NAN;
1251         assert_eq!(inf, inf.exp2());
1252         assert_eq!(0.0, neg_inf.exp2());
1253         assert!(nan.exp2().is_nan());
1254     }
1255
1256     #[test]
1257     fn test_ln() {
1258         let nan: f32 = f32::NAN;
1259         let inf: f32 = f32::INFINITY;
1260         let neg_inf: f32 = f32::NEG_INFINITY;
1261         assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1262         assert!(nan.ln().is_nan());
1263         assert_eq!(inf.ln(), inf);
1264         assert!(neg_inf.ln().is_nan());
1265         assert!((-2.3f32).ln().is_nan());
1266         assert_eq!((-0.0f32).ln(), neg_inf);
1267         assert_eq!(0.0f32.ln(), neg_inf);
1268         assert_approx_eq!(4.0f32.ln(), 1.386294);
1269     }
1270
1271     #[test]
1272     fn test_log() {
1273         let nan: f32 = f32::NAN;
1274         let inf: f32 = f32::INFINITY;
1275         let neg_inf: f32 = f32::NEG_INFINITY;
1276         assert_eq!(10.0f32.log(10.0), 1.0);
1277         assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1278         assert_eq!(1.0f32.exp().log(1.0f32.exp()), 1.0);
1279         assert!(1.0f32.log(1.0).is_nan());
1280         assert!(1.0f32.log(-13.9).is_nan());
1281         assert!(nan.log(2.3).is_nan());
1282         assert_eq!(inf.log(10.0), inf);
1283         assert!(neg_inf.log(8.8).is_nan());
1284         assert!((-2.3f32).log(0.1).is_nan());
1285         assert_eq!((-0.0f32).log(2.0), neg_inf);
1286         assert_eq!(0.0f32.log(7.0), neg_inf);
1287     }
1288
1289     #[test]
1290     fn test_log2() {
1291         let nan: f32 = f32::NAN;
1292         let inf: f32 = f32::INFINITY;
1293         let neg_inf: f32 = f32::NEG_INFINITY;
1294         assert_approx_eq!(10.0f32.log2(), 3.321928);
1295         assert_approx_eq!(2.3f32.log2(), 1.201634);
1296         assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1297         assert!(nan.log2().is_nan());
1298         assert_eq!(inf.log2(), inf);
1299         assert!(neg_inf.log2().is_nan());
1300         assert!((-2.3f32).log2().is_nan());
1301         assert_eq!((-0.0f32).log2(), neg_inf);
1302         assert_eq!(0.0f32.log2(), neg_inf);
1303     }
1304
1305     #[test]
1306     fn test_log10() {
1307         let nan: f32 = f32::NAN;
1308         let inf: f32 = f32::INFINITY;
1309         let neg_inf: f32 = f32::NEG_INFINITY;
1310         assert_eq!(10.0f32.log10(), 1.0);
1311         assert_approx_eq!(2.3f32.log10(), 0.361728);
1312         assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1313         assert_eq!(1.0f32.log10(), 0.0);
1314         assert!(nan.log10().is_nan());
1315         assert_eq!(inf.log10(), inf);
1316         assert!(neg_inf.log10().is_nan());
1317         assert!((-2.3f32).log10().is_nan());
1318         assert_eq!((-0.0f32).log10(), neg_inf);
1319         assert_eq!(0.0f32.log10(), neg_inf);
1320     }
1321
1322     #[test]
1323     fn test_to_degrees() {
1324         let pi: f32 = consts::PI;
1325         let nan: f32 = f32::NAN;
1326         let inf: f32 = f32::INFINITY;
1327         let neg_inf: f32 = f32::NEG_INFINITY;
1328         assert_eq!(0.0f32.to_degrees(), 0.0);
1329         assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1330         assert_eq!(pi.to_degrees(), 180.0);
1331         assert!(nan.to_degrees().is_nan());
1332         assert_eq!(inf.to_degrees(), inf);
1333         assert_eq!(neg_inf.to_degrees(), neg_inf);
1334         assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
1335     }
1336
1337     #[test]
1338     fn test_to_radians() {
1339         let pi: f32 = consts::PI;
1340         let nan: f32 = f32::NAN;
1341         let inf: f32 = f32::INFINITY;
1342         let neg_inf: f32 = f32::NEG_INFINITY;
1343         assert_eq!(0.0f32.to_radians(), 0.0);
1344         assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1345         assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1346         assert_eq!(180.0f32.to_radians(), pi);
1347         assert!(nan.to_radians().is_nan());
1348         assert_eq!(inf.to_radians(), inf);
1349         assert_eq!(neg_inf.to_radians(), neg_inf);
1350     }
1351
1352     #[test]
1353     fn test_asinh() {
1354         assert_eq!(0.0f32.asinh(), 0.0f32);
1355         assert_eq!((-0.0f32).asinh(), -0.0f32);
1356
1357         let inf: f32 = f32::INFINITY;
1358         let neg_inf: f32 = f32::NEG_INFINITY;
1359         let nan: f32 = f32::NAN;
1360         assert_eq!(inf.asinh(), inf);
1361         assert_eq!(neg_inf.asinh(), neg_inf);
1362         assert!(nan.asinh().is_nan());
1363         assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
1364         assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
1365     }
1366
1367     #[test]
1368     fn test_acosh() {
1369         assert_eq!(1.0f32.acosh(), 0.0f32);
1370         assert!(0.999f32.acosh().is_nan());
1371
1372         let inf: f32 = f32::INFINITY;
1373         let neg_inf: f32 = f32::NEG_INFINITY;
1374         let nan: f32 = f32::NAN;
1375         assert_eq!(inf.acosh(), inf);
1376         assert!(neg_inf.acosh().is_nan());
1377         assert!(nan.acosh().is_nan());
1378         assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
1379         assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
1380     }
1381
1382     #[test]
1383     fn test_atanh() {
1384         assert_eq!(0.0f32.atanh(), 0.0f32);
1385         assert_eq!((-0.0f32).atanh(), -0.0f32);
1386
1387         let inf32: f32 = f32::INFINITY;
1388         let neg_inf32: f32 = f32::NEG_INFINITY;
1389         assert_eq!(1.0f32.atanh(), inf32);
1390         assert_eq!((-1.0f32).atanh(), neg_inf32);
1391
1392         assert!(2f64.atanh().atanh().is_nan());
1393         assert!((-2f64).atanh().atanh().is_nan());
1394
1395         let inf64: f32 = f32::INFINITY;
1396         let neg_inf64: f32 = f32::NEG_INFINITY;
1397         let nan32: f32 = f32::NAN;
1398         assert!(inf64.atanh().is_nan());
1399         assert!(neg_inf64.atanh().is_nan());
1400         assert!(nan32.atanh().is_nan());
1401
1402         assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
1403         assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
1404     }
1405
1406     #[test]
1407     fn test_real_consts() {
1408         use super::consts;
1409
1410         let pi: f32 = consts::PI;
1411         let frac_pi_2: f32 = consts::FRAC_PI_2;
1412         let frac_pi_3: f32 = consts::FRAC_PI_3;
1413         let frac_pi_4: f32 = consts::FRAC_PI_4;
1414         let frac_pi_6: f32 = consts::FRAC_PI_6;
1415         let frac_pi_8: f32 = consts::FRAC_PI_8;
1416         let frac_1_pi: f32 = consts::FRAC_1_PI;
1417         let frac_2_pi: f32 = consts::FRAC_2_PI;
1418         let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRT_PI;
1419         let sqrt2: f32 = consts::SQRT_2;
1420         let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT_2;
1421         let e: f32 = consts::E;
1422         let log2_e: f32 = consts::LOG2_E;
1423         let log10_e: f32 = consts::LOG10_E;
1424         let ln_2: f32 = consts::LN_2;
1425         let ln_10: f32 = consts::LN_10;
1426
1427         assert_approx_eq!(frac_pi_2, pi / 2f32);
1428         assert_approx_eq!(frac_pi_3, pi / 3f32);
1429         assert_approx_eq!(frac_pi_4, pi / 4f32);
1430         assert_approx_eq!(frac_pi_6, pi / 6f32);
1431         assert_approx_eq!(frac_pi_8, pi / 8f32);
1432         assert_approx_eq!(frac_1_pi, 1f32 / pi);
1433         assert_approx_eq!(frac_2_pi, 2f32 / pi);
1434         assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
1435         assert_approx_eq!(sqrt2, 2f32.sqrt());
1436         assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
1437         assert_approx_eq!(log2_e, e.log2());
1438         assert_approx_eq!(log10_e, e.log10());
1439         assert_approx_eq!(ln_2, 2f32.ln());
1440         assert_approx_eq!(ln_10, 10f32.ln());
1441     }
1442
1443     #[test]
1444     fn test_float_bits_conv() {
1445         assert_eq!((1f32).to_bits(), 0x3f800000);
1446         assert_eq!((12.5f32).to_bits(), 0x41480000);
1447         assert_eq!((1337f32).to_bits(), 0x44a72000);
1448         assert_eq!((-14.25f32).to_bits(), 0xc1640000);
1449         assert_approx_eq!(f32::from_bits(0x3f800000), 1.0);
1450         assert_approx_eq!(f32::from_bits(0x41480000), 12.5);
1451         assert_approx_eq!(f32::from_bits(0x44a72000), 1337.0);
1452         assert_approx_eq!(f32::from_bits(0xc1640000), -14.25);
1453
1454         // Check that NaNs roundtrip their bits regardless of signalingness
1455         // 0xA is 0b1010; 0x5 is 0b0101 -- so these two together clobbers all the mantissa bits
1456         let masked_nan1 = f32::NAN.to_bits() ^ 0x002A_AAAA;
1457         let masked_nan2 = f32::NAN.to_bits() ^ 0x0055_5555;
1458         assert!(f32::from_bits(masked_nan1).is_nan());
1459         assert!(f32::from_bits(masked_nan2).is_nan());
1460
1461         assert_eq!(f32::from_bits(masked_nan1).to_bits(), masked_nan1);
1462         assert_eq!(f32::from_bits(masked_nan2).to_bits(), masked_nan2);
1463     }
1464 }