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