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