]> git.lizzy.rs Git - rust.git/blob - src/libstd/num/f32.rs
Auto merge of #23678 - richo:check-flightcheck, r=alexcrichton
[rust.git] / src / libstd / num / f32.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Operations and constants for 32-bits floats (`f32` type)
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14 #![allow(missing_docs)]
15 #![allow(unsigned_negation)]
16 #![doc(primitive = "f32")]
17
18 use prelude::v1::*;
19
20 use intrinsics;
21 use libc::c_int;
22 use num::{Float, FpCategory};
23 use num::strconv;
24 use num::strconv::ExponentFormat::{ExpNone, ExpDec};
25 use num::strconv::SignificantDigits::{DigAll, DigMax, DigExact};
26 use num::strconv::SignFormat::SignNeg;
27
28 use core::num;
29
30 pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON, MIN_VALUE};
31 pub use core::f32::{MIN_POS_VALUE, MAX_VALUE, MIN_EXP, MAX_EXP, MIN_10_EXP};
32 pub use core::f32::{MAX_10_EXP, NAN, INFINITY, NEG_INFINITY};
33 pub use core::f32::{MIN, MIN_POSITIVE, MAX};
34 pub use core::f32::consts;
35
36 #[allow(dead_code)]
37 mod cmath {
38     use libc::{c_float, c_int};
39
40     #[link_name = "m"]
41     extern {
42         pub fn acosf(n: c_float) -> c_float;
43         pub fn asinf(n: c_float) -> c_float;
44         pub fn atanf(n: c_float) -> c_float;
45         pub fn atan2f(a: c_float, b: c_float) -> c_float;
46         pub fn cbrtf(n: c_float) -> c_float;
47         pub fn coshf(n: c_float) -> c_float;
48         pub fn erff(n: c_float) -> c_float;
49         pub fn erfcf(n: c_float) -> c_float;
50         pub fn expm1f(n: c_float) -> c_float;
51         pub fn fdimf(a: c_float, b: c_float) -> c_float;
52         pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
53         pub fn fmaxf(a: c_float, b: c_float) -> c_float;
54         pub fn fminf(a: c_float, b: c_float) -> c_float;
55         pub fn fmodf(a: c_float, b: c_float) -> c_float;
56         pub fn nextafterf(x: c_float, y: c_float) -> c_float;
57         pub fn hypotf(x: c_float, y: c_float) -> c_float;
58         pub fn ldexpf(x: c_float, n: c_int) -> c_float;
59         pub fn logbf(n: c_float) -> c_float;
60         pub fn log1pf(n: c_float) -> c_float;
61         pub fn ilogbf(n: c_float) -> c_int;
62         pub fn modff(n: c_float, iptr: &mut c_float) -> c_float;
63         pub fn sinhf(n: c_float) -> c_float;
64         pub fn tanf(n: c_float) -> c_float;
65         pub fn tanhf(n: c_float) -> c_float;
66         pub fn tgammaf(n: c_float) -> c_float;
67
68         #[cfg(unix)]
69         pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
70
71         #[cfg(windows)]
72         #[link_name="__lgammaf_r"]
73         pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
74     }
75 }
76
77 #[stable(feature = "rust1", since = "1.0.0")]
78 #[allow(deprecated)]
79 impl Float for f32 {
80     #[inline]
81     fn nan() -> f32 { num::Float::nan() }
82     #[inline]
83     fn infinity() -> f32 { num::Float::infinity() }
84     #[inline]
85     fn neg_infinity() -> f32 { num::Float::neg_infinity() }
86     #[inline]
87     fn zero() -> f32 { num::Float::zero() }
88     #[inline]
89     fn neg_zero() -> f32 { num::Float::neg_zero() }
90     #[inline]
91     fn one() -> f32 { num::Float::one() }
92
93     #[allow(deprecated)]
94     #[inline]
95     fn mantissa_digits(unused_self: Option<f32>) -> usize {
96         num::Float::mantissa_digits(unused_self)
97     }
98     #[allow(deprecated)]
99     #[inline]
100     fn digits(unused_self: Option<f32>) -> usize { num::Float::digits(unused_self) }
101     #[allow(deprecated)]
102     #[inline]
103     fn epsilon() -> f32 { num::Float::epsilon() }
104     #[allow(deprecated)]
105     #[inline]
106     fn min_exp(unused_self: Option<f32>) -> isize { num::Float::min_exp(unused_self) }
107     #[allow(deprecated)]
108     #[inline]
109     fn max_exp(unused_self: Option<f32>) -> isize { num::Float::max_exp(unused_self) }
110     #[allow(deprecated)]
111     #[inline]
112     fn min_10_exp(unused_self: Option<f32>) -> isize { num::Float::min_10_exp(unused_self) }
113     #[allow(deprecated)]
114     #[inline]
115     fn max_10_exp(unused_self: Option<f32>) -> isize { num::Float::max_10_exp(unused_self) }
116     #[allow(deprecated)]
117     #[inline]
118     fn min_value() -> f32 { num::Float::min_value() }
119     #[allow(deprecated)]
120     #[inline]
121     fn min_pos_value(unused_self: Option<f32>) -> f32 { num::Float::min_pos_value(unused_self) }
122     #[allow(deprecated)]
123     #[inline]
124     fn max_value() -> f32 { num::Float::max_value() }
125
126     #[inline]
127     fn is_nan(self) -> bool { num::Float::is_nan(self) }
128     #[inline]
129     fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
130     #[inline]
131     fn is_finite(self) -> bool { num::Float::is_finite(self) }
132     #[inline]
133     fn is_normal(self) -> bool { num::Float::is_normal(self) }
134     #[inline]
135     fn classify(self) -> FpCategory { num::Float::classify(self) }
136
137     #[inline]
138     fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
139
140     #[inline]
141     fn floor(self) -> f32 { num::Float::floor(self) }
142     #[inline]
143     fn ceil(self) -> f32 { num::Float::ceil(self) }
144     #[inline]
145     fn round(self) -> f32 { num::Float::round(self) }
146     #[inline]
147     fn trunc(self) -> f32 { num::Float::trunc(self) }
148     #[inline]
149     fn fract(self) -> f32 { num::Float::fract(self) }
150
151     #[inline]
152     fn abs(self) -> f32 { num::Float::abs(self) }
153     #[inline]
154     fn signum(self) -> f32 { num::Float::signum(self) }
155     #[inline]
156     fn is_positive(self) -> bool { num::Float::is_positive(self) }
157     #[inline]
158     fn is_negative(self) -> bool { num::Float::is_negative(self) }
159
160     #[inline]
161     fn mul_add(self, a: f32, b: f32) -> f32 { num::Float::mul_add(self, a, b) }
162     #[inline]
163     fn recip(self) -> f32 { num::Float::recip(self) }
164
165     #[inline]
166     fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
167     #[inline]
168     fn powf(self, n: f32) -> f32 { num::Float::powf(self, n) }
169
170     #[inline]
171     fn sqrt(self) -> f32 { num::Float::sqrt(self) }
172     #[inline]
173     fn rsqrt(self) -> f32 { num::Float::rsqrt(self) }
174
175     #[inline]
176     fn exp(self) -> f32 { num::Float::exp(self) }
177     #[inline]
178     fn exp2(self) -> f32 { num::Float::exp2(self) }
179     #[inline]
180     fn ln(self) -> f32 { num::Float::ln(self) }
181     #[inline]
182     fn log(self, base: f32) -> f32 { num::Float::log(self, base) }
183     #[inline]
184     fn log2(self) -> f32 { num::Float::log2(self) }
185     #[inline]
186     fn log10(self) -> f32 { num::Float::log10(self) }
187     #[inline]
188     fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
189     #[inline]
190     fn to_radians(self) -> f32 { num::Float::to_radians(self) }
191
192     /// Constructs a floating point number by multiplying `x` by 2 raised to the
193     /// power of `exp`
194     #[inline]
195     fn ldexp(self, exp: isize) -> f32 {
196         unsafe { cmath::ldexpf(self, exp as c_int) }
197     }
198
199     /// Breaks the number into a normalized fraction and a base-2 exponent,
200     /// satisfying:
201     ///
202     /// - `self = x * pow(2, exp)`
203     /// - `0.5 <= abs(x) < 1.0`
204     #[inline]
205     fn frexp(self) -> (f32, isize) {
206         unsafe {
207             let mut exp = 0;
208             let x = cmath::frexpf(self, &mut exp);
209             (x, exp as isize)
210         }
211     }
212
213     /// Returns the next representable floating-point value in the direction of
214     /// `other`.
215     #[inline]
216     fn next_after(self, other: f32) -> f32 {
217         unsafe { cmath::nextafterf(self, other) }
218     }
219
220     #[inline]
221     fn max(self, other: f32) -> f32 {
222         unsafe { cmath::fmaxf(self, other) }
223     }
224
225     #[inline]
226     fn min(self, other: f32) -> f32 {
227         unsafe { cmath::fminf(self, other) }
228     }
229
230     #[inline]
231     fn abs_sub(self, other: f32) -> f32 {
232         unsafe { cmath::fdimf(self, other) }
233     }
234
235     #[inline]
236     fn cbrt(self) -> f32 {
237         unsafe { cmath::cbrtf(self) }
238     }
239
240     #[inline]
241     fn hypot(self, other: f32) -> f32 {
242         unsafe { cmath::hypotf(self, other) }
243     }
244
245     #[inline]
246     fn sin(self) -> f32 {
247         unsafe { intrinsics::sinf32(self) }
248     }
249
250     #[inline]
251     fn cos(self) -> f32 {
252         unsafe { intrinsics::cosf32(self) }
253     }
254
255     #[inline]
256     fn tan(self) -> f32 {
257         unsafe { cmath::tanf(self) }
258     }
259
260     #[inline]
261     fn asin(self) -> f32 {
262         unsafe { cmath::asinf(self) }
263     }
264
265     #[inline]
266     fn acos(self) -> f32 {
267         unsafe { cmath::acosf(self) }
268     }
269
270     #[inline]
271     fn atan(self) -> f32 {
272         unsafe { cmath::atanf(self) }
273     }
274
275     #[inline]
276     fn atan2(self, other: f32) -> f32 {
277         unsafe { cmath::atan2f(self, other) }
278     }
279
280     /// Simultaneously computes the sine and cosine of the number
281     #[inline]
282     fn sin_cos(self) -> (f32, f32) {
283         (self.sin(), self.cos())
284     }
285
286     /// Returns the exponential of the number, minus `1`, in a way that is
287     /// accurate even if the number is close to zero
288     #[inline]
289     fn exp_m1(self) -> f32 {
290         unsafe { cmath::expm1f(self) }
291     }
292
293     /// Returns the natural logarithm of the number plus `1` (`ln(1+n)`) more
294     /// accurately than if the operations were performed separately
295     #[inline]
296     fn ln_1p(self) -> f32 {
297         unsafe { cmath::log1pf(self) }
298     }
299
300     #[inline]
301     fn sinh(self) -> f32 {
302         unsafe { cmath::sinhf(self) }
303     }
304
305     #[inline]
306     fn cosh(self) -> f32 {
307         unsafe { cmath::coshf(self) }
308     }
309
310     #[inline]
311     fn tanh(self) -> f32 {
312         unsafe { cmath::tanhf(self) }
313     }
314
315     /// Inverse hyperbolic sine
316     ///
317     /// # Returns
318     ///
319     /// - on success, the inverse hyperbolic sine of `self` will be returned
320     /// - `self` if `self` is `0.0`, `-0.0`, `INFINITY`, or `NEG_INFINITY`
321     /// - `NAN` if `self` is `NAN`
322     #[inline]
323     fn asinh(self) -> f32 {
324         match self {
325             NEG_INFINITY => NEG_INFINITY,
326             x => (x + ((x * x) + 1.0).sqrt()).ln(),
327         }
328     }
329
330     /// Inverse hyperbolic cosine
331     ///
332     /// # Returns
333     ///
334     /// - on success, the inverse hyperbolic cosine of `self` will be returned
335     /// - `INFINITY` if `self` is `INFINITY`
336     /// - `NAN` if `self` is `NAN` or `self < 1.0` (including `NEG_INFINITY`)
337     #[inline]
338     fn acosh(self) -> f32 {
339         match self {
340             x if x < 1.0 => Float::nan(),
341             x => (x + ((x * x) - 1.0).sqrt()).ln(),
342         }
343     }
344
345     /// Inverse hyperbolic tangent
346     ///
347     /// # Returns
348     ///
349     /// - on success, the inverse hyperbolic tangent of `self` will be returned
350     /// - `self` if `self` is `0.0` or `-0.0`
351     /// - `INFINITY` if `self` is `1.0`
352     /// - `NEG_INFINITY` if `self` is `-1.0`
353     /// - `NAN` if the `self` is `NAN` or outside the domain of `-1.0 <= self <= 1.0`
354     ///   (including `INFINITY` and `NEG_INFINITY`)
355     #[inline]
356     fn atanh(self) -> f32 {
357         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
358     }
359 }
360
361 #[cfg(not(test))]
362 #[lang = "f32"]
363 #[stable(feature = "rust1", since = "1.0.0")]
364 impl f32 {
365     /// Returns `true` if this value is `NaN` and false otherwise.
366     ///
367     /// ```
368     /// use std::f32;
369     ///
370     /// let nan = f32::NAN;
371     /// let f = 7.0_f32;
372     ///
373     /// assert!(nan.is_nan());
374     /// assert!(!f.is_nan());
375     /// ```
376     #[stable(feature = "rust1", since = "1.0.0")]
377     #[inline]
378     pub fn is_nan(self) -> bool { num::Float::is_nan(self) }
379
380     /// Returns `true` if this value is positive infinity or negative infinity and
381     /// false otherwise.
382     ///
383     /// ```
384     /// use std::f32;
385     ///
386     /// let f = 7.0f32;
387     /// let inf = f32::INFINITY;
388     /// let neg_inf = f32::NEG_INFINITY;
389     /// let nan = f32::NAN;
390     ///
391     /// assert!(!f.is_infinite());
392     /// assert!(!nan.is_infinite());
393     ///
394     /// assert!(inf.is_infinite());
395     /// assert!(neg_inf.is_infinite());
396     /// ```
397     #[stable(feature = "rust1", since = "1.0.0")]
398     #[inline]
399     pub fn is_infinite(self) -> bool { num::Float::is_infinite(self) }
400
401     /// Returns `true` if this number is neither infinite nor `NaN`.
402     ///
403     /// ```
404     /// use std::f32;
405     ///
406     /// let f = 7.0f32;
407     /// let inf = f32::INFINITY;
408     /// let neg_inf = f32::NEG_INFINITY;
409     /// let nan = f32::NAN;
410     ///
411     /// assert!(f.is_finite());
412     ///
413     /// assert!(!nan.is_finite());
414     /// assert!(!inf.is_finite());
415     /// assert!(!neg_inf.is_finite());
416     /// ```
417     #[stable(feature = "rust1", since = "1.0.0")]
418     #[inline]
419     pub fn is_finite(self) -> bool { num::Float::is_finite(self) }
420
421     /// Returns `true` if the number is neither zero, infinite,
422     /// [subnormal][subnormal], or `NaN`.
423     ///
424     /// ```
425     /// # #![feature(std_misc)]
426     /// use std::f32;
427     ///
428     /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32
429     /// let max = f32::MAX;
430     /// let lower_than_min = 1.0e-40_f32;
431     /// let zero = 0.0_f32;
432     ///
433     /// assert!(min.is_normal());
434     /// assert!(max.is_normal());
435     ///
436     /// assert!(!zero.is_normal());
437     /// assert!(!f32::NAN.is_normal());
438     /// assert!(!f32::INFINITY.is_normal());
439     /// // Values between `0` and `min` are Subnormal.
440     /// assert!(!lower_than_min.is_normal());
441     /// ```
442     /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number
443     #[stable(feature = "rust1", since = "1.0.0")]
444     #[inline]
445     pub fn is_normal(self) -> bool { num::Float::is_normal(self) }
446
447     /// Returns the floating point category of the number. If only one property
448     /// is going to be tested, it is generally faster to use the specific
449     /// predicate instead.
450     ///
451     /// ```
452     /// use std::num::FpCategory;
453     /// use std::f32;
454     ///
455     /// let num = 12.4_f32;
456     /// let inf = f32::INFINITY;
457     ///
458     /// assert_eq!(num.classify(), FpCategory::Normal);
459     /// assert_eq!(inf.classify(), FpCategory::Infinite);
460     /// ```
461     #[stable(feature = "rust1", since = "1.0.0")]
462     #[inline]
463     pub fn classify(self) -> FpCategory { num::Float::classify(self) }
464
465     /// Returns the mantissa, base 2 exponent, and sign as integers, respectively.
466     /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`.
467     /// The floating point encoding is documented in the [Reference][floating-point].
468     ///
469     /// ```
470     /// # #![feature(std_misc)]
471     /// use std::f32;
472     ///
473     /// let num = 2.0f32;
474     ///
475     /// // (8388608, -22, 1)
476     /// let (mantissa, exponent, sign) = num.integer_decode();
477     /// let sign_f = sign as f32;
478     /// let mantissa_f = mantissa as f32;
479     /// let exponent_f = num.powf(exponent as f32);
480     ///
481     /// // 1 * 8388608 * 2^(-22) == 2
482     /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs();
483     ///
484     /// assert!(abs_difference <= f32::EPSILON);
485     /// ```
486     /// [floating-point]: ../../../../../reference.html#machine-types
487     #[unstable(feature = "std_misc", reason = "signature is undecided")]
488     #[inline]
489     pub fn integer_decode(self) -> (u64, i16, i8) { num::Float::integer_decode(self) }
490
491     /// Returns the largest integer less than or equal to a number.
492     ///
493     /// ```
494     /// let f = 3.99_f32;
495     /// let g = 3.0_f32;
496     ///
497     /// assert_eq!(f.floor(), 3.0);
498     /// assert_eq!(g.floor(), 3.0);
499     /// ```
500     #[stable(feature = "rust1", since = "1.0.0")]
501     #[inline]
502     pub fn floor(self) -> f32 { num::Float::floor(self) }
503
504     /// Returns the smallest integer greater than or equal to a number.
505     ///
506     /// ```
507     /// let f = 3.01_f32;
508     /// let g = 4.0_f32;
509     ///
510     /// assert_eq!(f.ceil(), 4.0);
511     /// assert_eq!(g.ceil(), 4.0);
512     /// ```
513     #[stable(feature = "rust1", since = "1.0.0")]
514     #[inline]
515     pub fn ceil(self) -> f32 { num::Float::ceil(self) }
516
517     /// Returns the nearest integer to a number. Round half-way cases away from
518     /// `0.0`.
519     ///
520     /// ```
521     /// let f = 3.3_f32;
522     /// let g = -3.3_f32;
523     ///
524     /// assert_eq!(f.round(), 3.0);
525     /// assert_eq!(g.round(), -3.0);
526     /// ```
527     #[stable(feature = "rust1", since = "1.0.0")]
528     #[inline]
529     pub fn round(self) -> f32 { num::Float::round(self) }
530
531     /// Return the integer part of a number.
532     ///
533     /// ```
534     /// let f = 3.3_f32;
535     /// let g = -3.7_f32;
536     ///
537     /// assert_eq!(f.trunc(), 3.0);
538     /// assert_eq!(g.trunc(), -3.0);
539     /// ```
540     #[stable(feature = "rust1", since = "1.0.0")]
541     #[inline]
542     pub fn trunc(self) -> f32 { num::Float::trunc(self) }
543
544     /// Returns the fractional part of a number.
545     ///
546     /// ```
547     /// use std::f32;
548     ///
549     /// let x = 3.5_f32;
550     /// let y = -3.5_f32;
551     /// let abs_difference_x = (x.fract() - 0.5).abs();
552     /// let abs_difference_y = (y.fract() - (-0.5)).abs();
553     ///
554     /// assert!(abs_difference_x <= f32::EPSILON);
555     /// assert!(abs_difference_y <= f32::EPSILON);
556     /// ```
557     #[stable(feature = "rust1", since = "1.0.0")]
558     #[inline]
559     pub fn fract(self) -> f32 { num::Float::fract(self) }
560
561     /// Computes the absolute value of `self`. Returns `NAN` if the
562     /// number is `NAN`.
563     ///
564     /// ```
565     /// use std::f32;
566     ///
567     /// let x = 3.5_f32;
568     /// let y = -3.5_f32;
569     ///
570     /// let abs_difference_x = (x.abs() - x).abs();
571     /// let abs_difference_y = (y.abs() - (-y)).abs();
572     ///
573     /// assert!(abs_difference_x <= f32::EPSILON);
574     /// assert!(abs_difference_y <= f32::EPSILON);
575     ///
576     /// assert!(f32::NAN.abs().is_nan());
577     /// ```
578     #[stable(feature = "rust1", since = "1.0.0")]
579     #[inline]
580     pub fn abs(self) -> f32 { num::Float::abs(self) }
581
582     /// Returns a number that represents the sign of `self`.
583     ///
584     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
585     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
586     /// - `NAN` if the number is `NAN`
587     ///
588     /// ```
589     /// use std::f32;
590     ///
591     /// let f = 3.5_f32;
592     ///
593     /// assert_eq!(f.signum(), 1.0);
594     /// assert_eq!(f32::NEG_INFINITY.signum(), -1.0);
595     ///
596     /// assert!(f32::NAN.signum().is_nan());
597     /// ```
598     #[stable(feature = "rust1", since = "1.0.0")]
599     #[inline]
600     pub fn signum(self) -> f32 { num::Float::signum(self) }
601
602     /// Returns `true` if `self`'s sign bit is positive, including
603     /// `+0.0` and `INFINITY`.
604     ///
605     /// ```
606     /// use std::f32;
607     ///
608     /// let nan = f32::NAN;
609     /// let f = 7.0_f32;
610     /// let g = -7.0_f32;
611     ///
612     /// assert!(f.is_sign_positive());
613     /// assert!(!g.is_sign_positive());
614     /// // Requires both tests to determine if is `NaN`
615     /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
616     /// ```
617     #[stable(feature = "rust1", since = "1.0.0")]
618     #[inline]
619     pub fn is_sign_positive(self) -> bool { num::Float::is_positive(self) }
620
621     #[stable(feature = "rust1", since = "1.0.0")]
622     #[deprecated(since = "1.0.0", reason = "renamed to is_sign_positive")]
623     #[inline]
624     pub fn is_positive(self) -> bool { num::Float::is_positive(self) }
625
626     /// Returns `true` if `self`'s sign is negative, including `-0.0`
627     /// and `NEG_INFINITY`.
628     ///
629     /// ```
630     /// use std::f32;
631     ///
632     /// let nan = f32::NAN;
633     /// let f = 7.0f32;
634     /// let g = -7.0f32;
635     ///
636     /// assert!(!f.is_sign_negative());
637     /// assert!(g.is_sign_negative());
638     /// // Requires both tests to determine if is `NaN`.
639     /// assert!(!nan.is_sign_positive() && !nan.is_sign_negative());
640     /// ```
641     #[stable(feature = "rust1", since = "1.0.0")]
642     #[inline]
643     pub fn is_sign_negative(self) -> bool { num::Float::is_negative(self) }
644
645     #[stable(feature = "rust1", since = "1.0.0")]
646     #[deprecated(since = "1.0.0", reason = "renamed to is_sign_negative")]
647     #[inline]
648     pub fn is_negative(self) -> bool { num::Float::is_negative(self) }
649
650     /// Fused multiply-add. Computes `(self * a) + b` with only one rounding
651     /// error. This produces a more accurate result with better performance than
652     /// a separate multiplication operation followed by an add.
653     ///
654     /// ```
655     /// use std::f32;
656     ///
657     /// let m = 10.0_f32;
658     /// let x = 4.0_f32;
659     /// let b = 60.0_f32;
660     ///
661     /// // 100.0
662     /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs();
663     ///
664     /// assert!(abs_difference <= f32::EPSILON);
665     /// ```
666     #[stable(feature = "rust1", since = "1.0.0")]
667     #[inline]
668     pub fn mul_add(self, a: f32, b: f32) -> f32 { num::Float::mul_add(self, a, b) }
669
670     /// Take the reciprocal (inverse) of a number, `1/x`.
671     ///
672     /// ```
673     /// use std::f32;
674     ///
675     /// let x = 2.0_f32;
676     /// let abs_difference = (x.recip() - (1.0/x)).abs();
677     ///
678     /// assert!(abs_difference <= f32::EPSILON);
679     /// ```
680     #[stable(feature = "rust1", since = "1.0.0")]
681     #[inline]
682     pub fn recip(self) -> f32 { num::Float::recip(self) }
683
684     /// Raise a number to an integer power.
685     ///
686     /// Using this function is generally faster than using `powf`
687     ///
688     /// ```
689     /// use std::f32;
690     ///
691     /// let x = 2.0_f32;
692     /// let abs_difference = (x.powi(2) - x*x).abs();
693     ///
694     /// assert!(abs_difference <= f32::EPSILON);
695     /// ```
696     #[stable(feature = "rust1", since = "1.0.0")]
697     #[inline]
698     pub fn powi(self, n: i32) -> f32 { num::Float::powi(self, n) }
699
700     /// Raise a number to a floating point power.
701     ///
702     /// ```
703     /// use std::f32;
704     ///
705     /// let x = 2.0_f32;
706     /// let abs_difference = (x.powf(2.0) - x*x).abs();
707     ///
708     /// assert!(abs_difference <= f32::EPSILON);
709     /// ```
710     #[stable(feature = "rust1", since = "1.0.0")]
711     #[inline]
712     pub fn powf(self, n: f32) -> f32 { num::Float::powf(self, n) }
713
714     /// Take the square root of a number.
715     ///
716     /// Returns NaN if `self` is a negative number.
717     ///
718     /// ```
719     /// use std::f32;
720     ///
721     /// let positive = 4.0_f32;
722     /// let negative = -4.0_f32;
723     ///
724     /// let abs_difference = (positive.sqrt() - 2.0).abs();
725     ///
726     /// assert!(abs_difference <= f32::EPSILON);
727     /// assert!(negative.sqrt().is_nan());
728     /// ```
729     #[stable(feature = "rust1", since = "1.0.0")]
730     #[inline]
731     pub fn sqrt(self) -> f32 { num::Float::sqrt(self) }
732
733     /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
734     ///
735     /// ```
736     /// # #![feature(std_misc)]
737     /// use std::f32;
738     ///
739     /// let f = 4.0f32;
740     ///
741     /// let abs_difference = (f.rsqrt() - 0.5).abs();
742     ///
743     /// assert!(abs_difference <= f32::EPSILON);
744     /// ```
745     #[unstable(feature = "std_misc",
746                reason = "unsure about its place in the world")]
747     #[deprecated(since = "1.0.0", reason = "use self.sqrt().recip() instead")]
748     #[inline]
749     pub fn rsqrt(self) -> f32 { num::Float::rsqrt(self) }
750
751     /// Returns `e^(self)`, (the exponential function).
752     ///
753     /// ```
754     /// use std::f32;
755     ///
756     /// let one = 1.0f32;
757     /// // e^1
758     /// let e = one.exp();
759     ///
760     /// // ln(e) - 1 == 0
761     /// let abs_difference = (e.ln() - 1.0).abs();
762     ///
763     /// assert!(abs_difference <= f32::EPSILON);
764     /// ```
765     #[stable(feature = "rust1", since = "1.0.0")]
766     #[inline]
767     pub fn exp(self) -> f32 { num::Float::exp(self) }
768
769     /// Returns `2^(self)`.
770     ///
771     /// ```
772     /// use std::f32;
773     ///
774     /// let f = 2.0f32;
775     ///
776     /// // 2^2 - 4 == 0
777     /// let abs_difference = (f.exp2() - 4.0).abs();
778     ///
779     /// assert!(abs_difference <= f32::EPSILON);
780     /// ```
781     #[stable(feature = "rust1", since = "1.0.0")]
782     #[inline]
783     pub fn exp2(self) -> f32 { num::Float::exp2(self) }
784
785     /// Returns the natural logarithm of the number.
786     ///
787     /// ```
788     /// use std::f32;
789     ///
790     /// let one = 1.0f32;
791     /// // e^1
792     /// let e = one.exp();
793     ///
794     /// // ln(e) - 1 == 0
795     /// let abs_difference = (e.ln() - 1.0).abs();
796     ///
797     /// assert!(abs_difference <= f32::EPSILON);
798     /// ```
799     #[stable(feature = "rust1", since = "1.0.0")]
800     #[inline]
801     pub fn ln(self) -> f32 { num::Float::ln(self) }
802
803     /// Returns the logarithm of the number with respect to an arbitrary base.
804     ///
805     /// ```
806     /// use std::f32;
807     ///
808     /// let ten = 10.0f32;
809     /// let two = 2.0f32;
810     ///
811     /// // log10(10) - 1 == 0
812     /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
813     ///
814     /// // log2(2) - 1 == 0
815     /// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
816     ///
817     /// assert!(abs_difference_10 <= f32::EPSILON);
818     /// assert!(abs_difference_2 <= f32::EPSILON);
819     /// ```
820     #[stable(feature = "rust1", since = "1.0.0")]
821     #[inline]
822     pub fn log(self, base: f32) -> f32 { num::Float::log(self, base) }
823
824     /// Returns the base 2 logarithm of the number.
825     ///
826     /// ```
827     /// use std::f32;
828     ///
829     /// let two = 2.0f32;
830     ///
831     /// // log2(2) - 1 == 0
832     /// let abs_difference = (two.log2() - 1.0).abs();
833     ///
834     /// assert!(abs_difference <= f32::EPSILON);
835     /// ```
836     #[stable(feature = "rust1", since = "1.0.0")]
837     #[inline]
838     pub fn log2(self) -> f32 { num::Float::log2(self) }
839
840     /// Returns the base 10 logarithm of the number.
841     ///
842     /// ```
843     /// use std::f32;
844     ///
845     /// let ten = 10.0f32;
846     ///
847     /// // log10(10) - 1 == 0
848     /// let abs_difference = (ten.log10() - 1.0).abs();
849     ///
850     /// assert!(abs_difference <= f32::EPSILON);
851     /// ```
852     #[stable(feature = "rust1", since = "1.0.0")]
853     #[inline]
854     pub fn log10(self) -> f32 { num::Float::log10(self) }
855
856     /// Convert radians to degrees.
857     ///
858     /// ```
859     /// # #![feature(std_misc, core)]
860     /// use std::f32::{self, consts};
861     ///
862     /// let angle = consts::PI;
863     ///
864     /// let abs_difference = (angle.to_degrees() - 180.0).abs();
865     ///
866     /// assert!(abs_difference <= f32::EPSILON);
867     /// ```
868     #[unstable(feature = "std_misc", reason = "desirability is unclear")]
869     #[inline]
870     pub fn to_degrees(self) -> f32 { num::Float::to_degrees(self) }
871
872     /// Convert degrees to radians.
873     ///
874     /// ```
875     /// # #![feature(std_misc)]
876     /// use std::f32::{self, consts};
877     ///
878     /// let angle = 180.0f32;
879     ///
880     /// let abs_difference = (angle.to_radians() - consts::PI).abs();
881     ///
882     /// assert!(abs_difference <= f32::EPSILON);
883     /// ```
884     #[unstable(feature = "std_misc", reason = "desirability is unclear")]
885     #[inline]
886     pub fn to_radians(self) -> f32 { num::Float::to_radians(self) }
887
888     /// Constructs a floating point number of `x*2^exp`.
889     ///
890     /// ```
891     /// # #![feature(std_misc)]
892     /// use std::f32;
893     /// // 3*2^2 - 12 == 0
894     /// let abs_difference = (f32::ldexp(3.0, 2) - 12.0).abs();
895     ///
896     /// assert!(abs_difference <= f32::EPSILON);
897     /// ```
898     #[unstable(feature = "std_misc",
899                reason = "pending integer conventions")]
900     #[inline]
901     pub fn ldexp(x: f32, exp: isize) -> f32 {
902         unsafe { cmath::ldexpf(x, exp as c_int) }
903     }
904
905     /// Breaks the number into a normalized fraction and a base-2 exponent,
906     /// satisfying:
907     ///
908     ///  * `self = x * 2^exp`
909     ///  * `0.5 <= abs(x) < 1.0`
910     ///
911     /// ```
912     /// # #![feature(std_misc)]
913     /// use std::f32;
914     ///
915     /// let x = 4.0f32;
916     ///
917     /// // (1/2)*2^3 -> 1 * 8/2 -> 4.0
918     /// let f = x.frexp();
919     /// let abs_difference_0 = (f.0 - 0.5).abs();
920     /// let abs_difference_1 = (f.1 as f32 - 3.0).abs();
921     ///
922     /// assert!(abs_difference_0 <= f32::EPSILON);
923     /// assert!(abs_difference_1 <= f32::EPSILON);
924     /// ```
925     #[unstable(feature = "std_misc",
926                reason = "pending integer conventions")]
927     #[inline]
928     pub fn frexp(self) -> (f32, isize) {
929         unsafe {
930             let mut exp = 0;
931             let x = cmath::frexpf(self, &mut exp);
932             (x, exp as isize)
933         }
934     }
935
936     /// Returns the next representable floating-point value in the direction of
937     /// `other`.
938     ///
939     /// ```
940     /// # #![feature(std_misc)]
941     /// use std::f32;
942     ///
943     /// let x = 1.0f32;
944     ///
945     /// let abs_diff = (x.next_after(2.0) - 1.00000011920928955078125_f32).abs();
946     ///
947     /// assert!(abs_diff <= f32::EPSILON);
948     /// ```
949     #[unstable(feature = "std_misc",
950                reason = "unsure about its place in the world")]
951     #[inline]
952     pub fn next_after(self, other: f32) -> f32 {
953         unsafe { cmath::nextafterf(self, other) }
954     }
955
956     /// Returns the maximum of the two numbers.
957     ///
958     /// ```
959     /// let x = 1.0f32;
960     /// let y = 2.0f32;
961     ///
962     /// assert_eq!(x.max(y), y);
963     /// ```
964     #[stable(feature = "rust1", since = "1.0.0")]
965     #[inline]
966     pub fn max(self, other: f32) -> f32 {
967         unsafe { cmath::fmaxf(self, other) }
968     }
969
970     /// Returns the minimum of the two numbers.
971     ///
972     /// ```
973     /// let x = 1.0f32;
974     /// let y = 2.0f32;
975     ///
976     /// assert_eq!(x.min(y), x);
977     /// ```
978     #[stable(feature = "rust1", since = "1.0.0")]
979     #[inline]
980     pub fn min(self, other: f32) -> f32 {
981         unsafe { cmath::fminf(self, other) }
982     }
983
984     /// The positive difference of two numbers.
985     ///
986     /// * If `self <= other`: `0:0`
987     /// * Else: `self - other`
988     ///
989     /// ```
990     /// # #![feature(std_misc)]
991     /// use std::f32;
992     ///
993     /// let x = 3.0f32;
994     /// let y = -3.0f32;
995     ///
996     /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs();
997     /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs();
998     ///
999     /// assert!(abs_difference_x <= f32::EPSILON);
1000     /// assert!(abs_difference_y <= f32::EPSILON);
1001     /// ```
1002     #[stable(feature = "rust1", since = "1.0.0")]
1003     #[inline]
1004     pub fn abs_sub(self, other: f32) -> f32 {
1005         unsafe { cmath::fdimf(self, other) }
1006     }
1007
1008     /// Take the cubic root of a number.
1009     ///
1010     /// ```
1011     /// # #![feature(std_misc)]
1012     /// use std::f32;
1013     ///
1014     /// let x = 8.0f32;
1015     ///
1016     /// // x^(1/3) - 2 == 0
1017     /// let abs_difference = (x.cbrt() - 2.0).abs();
1018     ///
1019     /// assert!(abs_difference <= f32::EPSILON);
1020     /// ```
1021     #[stable(feature = "rust1", since = "1.0.0")]
1022     #[inline]
1023     pub fn cbrt(self) -> f32 {
1024         unsafe { cmath::cbrtf(self) }
1025     }
1026
1027     /// Calculate the length of the hypotenuse of a right-angle triangle given
1028     /// legs of length `x` and `y`.
1029     ///
1030     /// ```
1031     /// use std::f32;
1032     ///
1033     /// let x = 2.0f32;
1034     /// let y = 3.0f32;
1035     ///
1036     /// // sqrt(x^2 + y^2)
1037     /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs();
1038     ///
1039     /// assert!(abs_difference <= f32::EPSILON);
1040     /// ```
1041     #[stable(feature = "rust1", since = "1.0.0")]
1042     #[inline]
1043     pub fn hypot(self, other: f32) -> f32 {
1044         unsafe { cmath::hypotf(self, other) }
1045     }
1046
1047     /// Computes the sine of a number (in radians).
1048     ///
1049     /// ```
1050     /// use std::f32;
1051     ///
1052     /// let x = f32::consts::PI/2.0;
1053     ///
1054     /// let abs_difference = (x.sin() - 1.0).abs();
1055     ///
1056     /// assert!(abs_difference <= f32::EPSILON);
1057     /// ```
1058     #[stable(feature = "rust1", since = "1.0.0")]
1059     #[inline]
1060     pub fn sin(self) -> f32 {
1061         unsafe { intrinsics::sinf32(self) }
1062     }
1063
1064     /// Computes the cosine of a number (in radians).
1065     ///
1066     /// ```
1067     /// use std::f32;
1068     ///
1069     /// let x = 2.0*f32::consts::PI;
1070     ///
1071     /// let abs_difference = (x.cos() - 1.0).abs();
1072     ///
1073     /// assert!(abs_difference <= f32::EPSILON);
1074     /// ```
1075     #[stable(feature = "rust1", since = "1.0.0")]
1076     #[inline]
1077     pub fn cos(self) -> f32 {
1078         unsafe { intrinsics::cosf32(self) }
1079     }
1080
1081     /// Computes the tangent of a number (in radians).
1082     ///
1083     /// ```
1084     /// use std::f64;
1085     ///
1086     /// let x = f64::consts::PI/4.0;
1087     /// let abs_difference = (x.tan() - 1.0).abs();
1088     ///
1089     /// assert!(abs_difference < 1e-10);
1090     /// ```
1091     #[stable(feature = "rust1", since = "1.0.0")]
1092     #[inline]
1093     pub fn tan(self) -> f32 {
1094         unsafe { cmath::tanf(self) }
1095     }
1096
1097     /// Computes the arcsine of a number. Return value is in radians in
1098     /// the range [-pi/2, pi/2] or NaN if the number is outside the range
1099     /// [-1, 1].
1100     ///
1101     /// ```
1102     /// use std::f32;
1103     ///
1104     /// let f = f32::consts::PI / 2.0;
1105     ///
1106     /// // asin(sin(pi/2))
1107     /// let abs_difference = f.sin().asin().abs_sub(f32::consts::PI / 2.0);
1108     ///
1109     /// assert!(abs_difference <= f32::EPSILON);
1110     /// ```
1111     #[stable(feature = "rust1", since = "1.0.0")]
1112     #[inline]
1113     pub fn asin(self) -> f32 {
1114         unsafe { cmath::asinf(self) }
1115     }
1116
1117     /// Computes the arccosine of a number. Return value is in radians in
1118     /// the range [0, pi] or NaN if the number is outside the range
1119     /// [-1, 1].
1120     ///
1121     /// ```
1122     /// use std::f32;
1123     ///
1124     /// let f = f32::consts::PI / 4.0;
1125     ///
1126     /// // acos(cos(pi/4))
1127     /// let abs_difference = f.cos().acos().abs_sub(f32::consts::PI / 4.0);
1128     ///
1129     /// assert!(abs_difference <= f32::EPSILON);
1130     /// ```
1131     #[stable(feature = "rust1", since = "1.0.0")]
1132     #[inline]
1133     pub fn acos(self) -> f32 {
1134         unsafe { cmath::acosf(self) }
1135     }
1136
1137     /// Computes the arctangent of a number. Return value is in radians in the
1138     /// range [-pi/2, pi/2];
1139     ///
1140     /// ```
1141     /// use std::f32;
1142     ///
1143     /// let f = 1.0f32;
1144     ///
1145     /// // atan(tan(1))
1146     /// let abs_difference = f.tan().atan().abs_sub(1.0);
1147     ///
1148     /// assert!(abs_difference <= f32::EPSILON);
1149     /// ```
1150     #[stable(feature = "rust1", since = "1.0.0")]
1151     #[inline]
1152     pub fn atan(self) -> f32 {
1153         unsafe { cmath::atanf(self) }
1154     }
1155
1156     /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`).
1157     ///
1158     /// * `x = 0`, `y = 0`: `0`
1159     /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]`
1160     /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]`
1161     /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)`
1162     ///
1163     /// ```
1164     /// use std::f32;
1165     ///
1166     /// let pi = f32::consts::PI;
1167     /// // All angles from horizontal right (+x)
1168     /// // 45 deg counter-clockwise
1169     /// let x1 = 3.0f32;
1170     /// let y1 = -3.0f32;
1171     ///
1172     /// // 135 deg clockwise
1173     /// let x2 = -3.0f32;
1174     /// let y2 = 3.0f32;
1175     ///
1176     /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs();
1177     /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs();
1178     ///
1179     /// assert!(abs_difference_1 <= f32::EPSILON);
1180     /// assert!(abs_difference_2 <= f32::EPSILON);
1181     /// ```
1182     #[stable(feature = "rust1", since = "1.0.0")]
1183     #[inline]
1184     pub fn atan2(self, other: f32) -> f32 {
1185         unsafe { cmath::atan2f(self, other) }
1186     }
1187
1188     /// Simultaneously computes the sine and cosine of the number, `x`. Returns
1189     /// `(sin(x), cos(x))`.
1190     ///
1191     /// ```
1192     /// use std::f32;
1193     ///
1194     /// let x = f32::consts::PI/4.0;
1195     /// let f = x.sin_cos();
1196     ///
1197     /// let abs_difference_0 = (f.0 - x.sin()).abs();
1198     /// let abs_difference_1 = (f.1 - x.cos()).abs();
1199     ///
1200     /// assert!(abs_difference_0 <= f32::EPSILON);
1201     /// assert!(abs_difference_0 <= f32::EPSILON);
1202     /// ```
1203     #[stable(feature = "rust1", since = "1.0.0")]
1204     #[inline]
1205     pub fn sin_cos(self) -> (f32, f32) {
1206         (self.sin(), self.cos())
1207     }
1208
1209     /// Returns `e^(self) - 1` in a way that is accurate even if the
1210     /// number is close to zero.
1211     ///
1212     /// ```
1213     /// use std::f64;
1214     ///
1215     /// let x = 7.0f64;
1216     ///
1217     /// // e^(ln(7)) - 1
1218     /// let abs_difference = x.ln().exp_m1().abs_sub(6.0);
1219     ///
1220     /// assert!(abs_difference < 1e-10);
1221     /// ```
1222     #[stable(feature = "rust1", since = "1.0.0")]
1223     #[inline]
1224     pub fn exp_m1(self) -> f32 {
1225         unsafe { cmath::expm1f(self) }
1226     }
1227
1228     /// Returns `ln(1+n)` (natural logarithm) more accurately than if
1229     /// the operations were performed separately.
1230     ///
1231     /// ```
1232     /// use std::f32;
1233     ///
1234     /// let x = f32::consts::E - 1.0;
1235     ///
1236     /// // ln(1 + (e - 1)) == ln(e) == 1
1237     /// let abs_difference = (x.ln_1p() - 1.0).abs();
1238     ///
1239     /// assert!(abs_difference <= f32::EPSILON);
1240     /// ```
1241     #[stable(feature = "rust1", since = "1.0.0")]
1242     #[inline]
1243     pub fn ln_1p(self) -> f32 {
1244         unsafe { cmath::log1pf(self) }
1245     }
1246
1247     /// Hyperbolic sine function.
1248     ///
1249     /// ```
1250     /// use std::f32;
1251     ///
1252     /// let e = f32::consts::E;
1253     /// let x = 1.0f32;
1254     ///
1255     /// let f = x.sinh();
1256     /// // Solving sinh() at 1 gives `(e^2-1)/(2e)`
1257     /// let g = (e*e - 1.0)/(2.0*e);
1258     /// let abs_difference = (f - g).abs();
1259     ///
1260     /// assert!(abs_difference <= f32::EPSILON);
1261     /// ```
1262     #[stable(feature = "rust1", since = "1.0.0")]
1263     #[inline]
1264     pub fn sinh(self) -> f32 {
1265         unsafe { cmath::sinhf(self) }
1266     }
1267
1268     /// Hyperbolic cosine function.
1269     ///
1270     /// ```
1271     /// use std::f32;
1272     ///
1273     /// let e = f32::consts::E;
1274     /// let x = 1.0f32;
1275     /// let f = x.cosh();
1276     /// // Solving cosh() at 1 gives this result
1277     /// let g = (e*e + 1.0)/(2.0*e);
1278     /// let abs_difference = f.abs_sub(g);
1279     ///
1280     /// // Same result
1281     /// assert!(abs_difference <= f32::EPSILON);
1282     /// ```
1283     #[stable(feature = "rust1", since = "1.0.0")]
1284     #[inline]
1285     pub fn cosh(self) -> f32 {
1286         unsafe { cmath::coshf(self) }
1287     }
1288
1289     /// Hyperbolic tangent function.
1290     ///
1291     /// ```
1292     /// use std::f32;
1293     ///
1294     /// let e = f32::consts::E;
1295     /// let x = 1.0f32;
1296     ///
1297     /// let f = x.tanh();
1298     /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))`
1299     /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2));
1300     /// let abs_difference = (f - g).abs();
1301     ///
1302     /// assert!(abs_difference <= f32::EPSILON);
1303     /// ```
1304     #[stable(feature = "rust1", since = "1.0.0")]
1305     #[inline]
1306     pub fn tanh(self) -> f32 {
1307         unsafe { cmath::tanhf(self) }
1308     }
1309
1310     /// Inverse hyperbolic sine function.
1311     ///
1312     /// ```
1313     /// use std::f32;
1314     ///
1315     /// let x = 1.0f32;
1316     /// let f = x.sinh().asinh();
1317     ///
1318     /// let abs_difference = (f - x).abs();
1319     ///
1320     /// assert!(abs_difference <= f32::EPSILON);
1321     /// ```
1322     #[stable(feature = "rust1", since = "1.0.0")]
1323     #[inline]
1324     pub fn asinh(self) -> f32 {
1325         match self {
1326             NEG_INFINITY => NEG_INFINITY,
1327             x => (x + ((x * x) + 1.0).sqrt()).ln(),
1328         }
1329     }
1330
1331     /// Inverse hyperbolic cosine function.
1332     ///
1333     /// ```
1334     /// use std::f32;
1335     ///
1336     /// let x = 1.0f32;
1337     /// let f = x.cosh().acosh();
1338     ///
1339     /// let abs_difference = (f - x).abs();
1340     ///
1341     /// assert!(abs_difference <= f32::EPSILON);
1342     /// ```
1343     #[stable(feature = "rust1", since = "1.0.0")]
1344     #[inline]
1345     pub fn acosh(self) -> f32 {
1346         match self {
1347             x if x < 1.0 => Float::nan(),
1348             x => (x + ((x * x) - 1.0).sqrt()).ln(),
1349         }
1350     }
1351
1352     /// Inverse hyperbolic tangent function.
1353     ///
1354     /// ```
1355     /// use std::f32;
1356     ///
1357     /// let e = f32::consts::E;
1358     /// let f = e.tanh().atanh();
1359     ///
1360     /// let abs_difference = f.abs_sub(e);
1361     ///
1362     /// assert!(abs_difference <= f32::EPSILON);
1363     /// ```
1364     #[stable(feature = "rust1", since = "1.0.0")]
1365     #[inline]
1366     pub fn atanh(self) -> f32 {
1367         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
1368     }
1369 }
1370
1371 //
1372 // Section: String Conversions
1373 //
1374
1375 /// Converts a float to a string
1376 ///
1377 /// # Arguments
1378 ///
1379 /// * num - The float value
1380 #[inline]
1381 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1382 #[deprecated(since = "1.0.0", reason = "use the ToString trait instead")]
1383 pub fn to_string(num: f32) -> String {
1384     let (r, _) = strconv::float_to_str_common(
1385         num, 10, true, SignNeg, DigAll, ExpNone, false);
1386     r
1387 }
1388
1389 /// Converts a float to a string in hexadecimal format
1390 ///
1391 /// # Arguments
1392 ///
1393 /// * num - The float value
1394 #[inline]
1395 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1396 #[deprecated(since = "1.0.0", reason = "use format! instead")]
1397 pub fn to_str_hex(num: f32) -> String {
1398     let (r, _) = strconv::float_to_str_common(
1399         num, 16, true, SignNeg, DigAll, ExpNone, false);
1400     r
1401 }
1402
1403 /// Converts a float to a string in a given radix, and a flag indicating
1404 /// whether it's a special value
1405 ///
1406 /// # Arguments
1407 ///
1408 /// * num - The float value
1409 /// * radix - The base to use
1410 #[inline]
1411 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1412 #[deprecated(since = "1.0.0", reason = "use format! instead")]
1413 pub fn to_str_radix_special(num: f32, rdx: u32) -> (String, bool) {
1414     strconv::float_to_str_common(num, rdx, true, SignNeg, DigAll, ExpNone, false)
1415 }
1416
1417 /// Converts a float to a string with exactly the number of
1418 /// provided significant digits
1419 ///
1420 /// # Arguments
1421 ///
1422 /// * num - The float value
1423 /// * digits - The number of significant digits
1424 #[inline]
1425 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1426 pub fn to_str_exact(num: f32, dig: usize) -> String {
1427     let (r, _) = strconv::float_to_str_common(
1428         num, 10, true, SignNeg, DigExact(dig), ExpNone, false);
1429     r
1430 }
1431
1432 /// Converts a float to a string with a maximum number of
1433 /// significant digits
1434 ///
1435 /// # Arguments
1436 ///
1437 /// * num - The float value
1438 /// * digits - The number of significant digits
1439 #[inline]
1440 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1441 pub fn to_str_digits(num: f32, dig: usize) -> String {
1442     let (r, _) = strconv::float_to_str_common(
1443         num, 10, true, SignNeg, DigMax(dig), ExpNone, false);
1444     r
1445 }
1446
1447 /// Converts a float to a string using the exponential notation with exactly the number of
1448 /// provided digits after the decimal point in the significand
1449 ///
1450 /// # Arguments
1451 ///
1452 /// * num - The float value
1453 /// * digits - The number of digits after the decimal point
1454 /// * upper - Use `E` instead of `e` for the exponent sign
1455 #[inline]
1456 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1457 pub fn to_str_exp_exact(num: f32, dig: usize, upper: bool) -> String {
1458     let (r, _) = strconv::float_to_str_common(
1459         num, 10, true, SignNeg, DigExact(dig), ExpDec, upper);
1460     r
1461 }
1462
1463 /// Converts a float to a string using the exponential notation with the maximum number of
1464 /// digits after the decimal point in the significand
1465 ///
1466 /// # Arguments
1467 ///
1468 /// * num - The float value
1469 /// * digits - The number of digits after the decimal point
1470 /// * upper - Use `E` instead of `e` for the exponent sign
1471 #[inline]
1472 #[unstable(feature = "std_misc", reason = "may be removed or relocated")]
1473 pub fn to_str_exp_digits(num: f32, dig: usize, upper: bool) -> String {
1474     let (r, _) = strconv::float_to_str_common(
1475         num, 10, true, SignNeg, DigMax(dig), ExpDec, upper);
1476     r
1477 }
1478
1479 #[cfg(test)]
1480 mod tests {
1481     use f32::*;
1482     use num::*;
1483     use num::FpCategory as Fp;
1484
1485     #[test]
1486     fn test_num_f32() {
1487         test_num(10f32, 2f32);
1488     }
1489
1490     #[test]
1491     fn test_min_nan() {
1492         assert_eq!(NAN.min(2.0), 2.0);
1493         assert_eq!(2.0f32.min(NAN), 2.0);
1494     }
1495
1496     #[test]
1497     fn test_max_nan() {
1498         assert_eq!(NAN.max(2.0), 2.0);
1499         assert_eq!(2.0f32.max(NAN), 2.0);
1500     }
1501
1502     #[test]
1503     fn test_nan() {
1504         let nan: f32 = Float::nan();
1505         assert!(nan.is_nan());
1506         assert!(!nan.is_infinite());
1507         assert!(!nan.is_finite());
1508         assert!(!nan.is_normal());
1509         assert!(!nan.is_sign_positive());
1510         assert!(!nan.is_sign_negative());
1511         assert_eq!(Fp::Nan, nan.classify());
1512     }
1513
1514     #[test]
1515     fn test_infinity() {
1516         let inf: f32 = Float::infinity();
1517         assert!(inf.is_infinite());
1518         assert!(!inf.is_finite());
1519         assert!(inf.is_sign_positive());
1520         assert!(!inf.is_sign_negative());
1521         assert!(!inf.is_nan());
1522         assert!(!inf.is_normal());
1523         assert_eq!(Fp::Infinite, inf.classify());
1524     }
1525
1526     #[test]
1527     fn test_neg_infinity() {
1528         let neg_inf: f32 = Float::neg_infinity();
1529         assert!(neg_inf.is_infinite());
1530         assert!(!neg_inf.is_finite());
1531         assert!(!neg_inf.is_sign_positive());
1532         assert!(neg_inf.is_sign_negative());
1533         assert!(!neg_inf.is_nan());
1534         assert!(!neg_inf.is_normal());
1535         assert_eq!(Fp::Infinite, neg_inf.classify());
1536     }
1537
1538     #[test]
1539     fn test_zero() {
1540         let zero: f32 = Float::zero();
1541         assert_eq!(0.0, zero);
1542         assert!(!zero.is_infinite());
1543         assert!(zero.is_finite());
1544         assert!(zero.is_sign_positive());
1545         assert!(!zero.is_sign_negative());
1546         assert!(!zero.is_nan());
1547         assert!(!zero.is_normal());
1548         assert_eq!(Fp::Zero, zero.classify());
1549     }
1550
1551     #[test]
1552     fn test_neg_zero() {
1553         let neg_zero: f32 = Float::neg_zero();
1554         assert_eq!(0.0, neg_zero);
1555         assert!(!neg_zero.is_infinite());
1556         assert!(neg_zero.is_finite());
1557         assert!(!neg_zero.is_sign_positive());
1558         assert!(neg_zero.is_sign_negative());
1559         assert!(!neg_zero.is_nan());
1560         assert!(!neg_zero.is_normal());
1561         assert_eq!(Fp::Zero, neg_zero.classify());
1562     }
1563
1564     #[test]
1565     fn test_one() {
1566         let one: f32 = Float::one();
1567         assert_eq!(1.0, one);
1568         assert!(!one.is_infinite());
1569         assert!(one.is_finite());
1570         assert!(one.is_sign_positive());
1571         assert!(!one.is_sign_negative());
1572         assert!(!one.is_nan());
1573         assert!(one.is_normal());
1574         assert_eq!(Fp::Normal, one.classify());
1575     }
1576
1577     #[test]
1578     fn test_is_nan() {
1579         let nan: f32 = Float::nan();
1580         let inf: f32 = Float::infinity();
1581         let neg_inf: f32 = Float::neg_infinity();
1582         assert!(nan.is_nan());
1583         assert!(!0.0f32.is_nan());
1584         assert!(!5.3f32.is_nan());
1585         assert!(!(-10.732f32).is_nan());
1586         assert!(!inf.is_nan());
1587         assert!(!neg_inf.is_nan());
1588     }
1589
1590     #[test]
1591     fn test_is_infinite() {
1592         let nan: f32 = Float::nan();
1593         let inf: f32 = Float::infinity();
1594         let neg_inf: f32 = Float::neg_infinity();
1595         assert!(!nan.is_infinite());
1596         assert!(inf.is_infinite());
1597         assert!(neg_inf.is_infinite());
1598         assert!(!0.0f32.is_infinite());
1599         assert!(!42.8f32.is_infinite());
1600         assert!(!(-109.2f32).is_infinite());
1601     }
1602
1603     #[test]
1604     fn test_is_finite() {
1605         let nan: f32 = Float::nan();
1606         let inf: f32 = Float::infinity();
1607         let neg_inf: f32 = Float::neg_infinity();
1608         assert!(!nan.is_finite());
1609         assert!(!inf.is_finite());
1610         assert!(!neg_inf.is_finite());
1611         assert!(0.0f32.is_finite());
1612         assert!(42.8f32.is_finite());
1613         assert!((-109.2f32).is_finite());
1614     }
1615
1616     #[test]
1617     fn test_is_normal() {
1618         let nan: f32 = Float::nan();
1619         let inf: f32 = Float::infinity();
1620         let neg_inf: f32 = Float::neg_infinity();
1621         let zero: f32 = Float::zero();
1622         let neg_zero: f32 = Float::neg_zero();
1623         assert!(!nan.is_normal());
1624         assert!(!inf.is_normal());
1625         assert!(!neg_inf.is_normal());
1626         assert!(!zero.is_normal());
1627         assert!(!neg_zero.is_normal());
1628         assert!(1f32.is_normal());
1629         assert!(1e-37f32.is_normal());
1630         assert!(!1e-38f32.is_normal());
1631     }
1632
1633     #[test]
1634     fn test_classify() {
1635         let nan: f32 = Float::nan();
1636         let inf: f32 = Float::infinity();
1637         let neg_inf: f32 = Float::neg_infinity();
1638         let zero: f32 = Float::zero();
1639         let neg_zero: f32 = Float::neg_zero();
1640         assert_eq!(nan.classify(), Fp::Nan);
1641         assert_eq!(inf.classify(), Fp::Infinite);
1642         assert_eq!(neg_inf.classify(), Fp::Infinite);
1643         assert_eq!(zero.classify(), Fp::Zero);
1644         assert_eq!(neg_zero.classify(), Fp::Zero);
1645         assert_eq!(1f32.classify(), Fp::Normal);
1646         assert_eq!(1e-37f32.classify(), Fp::Normal);
1647         assert_eq!(1e-38f32.classify(), Fp::Subnormal);
1648     }
1649
1650     #[test]
1651     fn test_integer_decode() {
1652         assert_eq!(3.14159265359f32.integer_decode(), (13176795, -22, 1));
1653         assert_eq!((-8573.5918555f32).integer_decode(), (8779358, -10, -1));
1654         assert_eq!(2f32.powf(100.0).integer_decode(), (8388608, 77, 1));
1655         assert_eq!(0f32.integer_decode(), (0, -150, 1));
1656         assert_eq!((-0f32).integer_decode(), (0, -150, -1));
1657         assert_eq!(INFINITY.integer_decode(), (8388608, 105, 1));
1658         assert_eq!(NEG_INFINITY.integer_decode(), (8388608, 105, -1));
1659         assert_eq!(NAN.integer_decode(), (12582912, 105, 1));
1660     }
1661
1662     #[test]
1663     fn test_floor() {
1664         assert_approx_eq!(1.0f32.floor(), 1.0f32);
1665         assert_approx_eq!(1.3f32.floor(), 1.0f32);
1666         assert_approx_eq!(1.5f32.floor(), 1.0f32);
1667         assert_approx_eq!(1.7f32.floor(), 1.0f32);
1668         assert_approx_eq!(0.0f32.floor(), 0.0f32);
1669         assert_approx_eq!((-0.0f32).floor(), -0.0f32);
1670         assert_approx_eq!((-1.0f32).floor(), -1.0f32);
1671         assert_approx_eq!((-1.3f32).floor(), -2.0f32);
1672         assert_approx_eq!((-1.5f32).floor(), -2.0f32);
1673         assert_approx_eq!((-1.7f32).floor(), -2.0f32);
1674     }
1675
1676     #[test]
1677     fn test_ceil() {
1678         assert_approx_eq!(1.0f32.ceil(), 1.0f32);
1679         assert_approx_eq!(1.3f32.ceil(), 2.0f32);
1680         assert_approx_eq!(1.5f32.ceil(), 2.0f32);
1681         assert_approx_eq!(1.7f32.ceil(), 2.0f32);
1682         assert_approx_eq!(0.0f32.ceil(), 0.0f32);
1683         assert_approx_eq!((-0.0f32).ceil(), -0.0f32);
1684         assert_approx_eq!((-1.0f32).ceil(), -1.0f32);
1685         assert_approx_eq!((-1.3f32).ceil(), -1.0f32);
1686         assert_approx_eq!((-1.5f32).ceil(), -1.0f32);
1687         assert_approx_eq!((-1.7f32).ceil(), -1.0f32);
1688     }
1689
1690     #[test]
1691     fn test_round() {
1692         assert_approx_eq!(1.0f32.round(), 1.0f32);
1693         assert_approx_eq!(1.3f32.round(), 1.0f32);
1694         assert_approx_eq!(1.5f32.round(), 2.0f32);
1695         assert_approx_eq!(1.7f32.round(), 2.0f32);
1696         assert_approx_eq!(0.0f32.round(), 0.0f32);
1697         assert_approx_eq!((-0.0f32).round(), -0.0f32);
1698         assert_approx_eq!((-1.0f32).round(), -1.0f32);
1699         assert_approx_eq!((-1.3f32).round(), -1.0f32);
1700         assert_approx_eq!((-1.5f32).round(), -2.0f32);
1701         assert_approx_eq!((-1.7f32).round(), -2.0f32);
1702     }
1703
1704     #[test]
1705     fn test_trunc() {
1706         assert_approx_eq!(1.0f32.trunc(), 1.0f32);
1707         assert_approx_eq!(1.3f32.trunc(), 1.0f32);
1708         assert_approx_eq!(1.5f32.trunc(), 1.0f32);
1709         assert_approx_eq!(1.7f32.trunc(), 1.0f32);
1710         assert_approx_eq!(0.0f32.trunc(), 0.0f32);
1711         assert_approx_eq!((-0.0f32).trunc(), -0.0f32);
1712         assert_approx_eq!((-1.0f32).trunc(), -1.0f32);
1713         assert_approx_eq!((-1.3f32).trunc(), -1.0f32);
1714         assert_approx_eq!((-1.5f32).trunc(), -1.0f32);
1715         assert_approx_eq!((-1.7f32).trunc(), -1.0f32);
1716     }
1717
1718     #[test]
1719     fn test_fract() {
1720         assert_approx_eq!(1.0f32.fract(), 0.0f32);
1721         assert_approx_eq!(1.3f32.fract(), 0.3f32);
1722         assert_approx_eq!(1.5f32.fract(), 0.5f32);
1723         assert_approx_eq!(1.7f32.fract(), 0.7f32);
1724         assert_approx_eq!(0.0f32.fract(), 0.0f32);
1725         assert_approx_eq!((-0.0f32).fract(), -0.0f32);
1726         assert_approx_eq!((-1.0f32).fract(), -0.0f32);
1727         assert_approx_eq!((-1.3f32).fract(), -0.3f32);
1728         assert_approx_eq!((-1.5f32).fract(), -0.5f32);
1729         assert_approx_eq!((-1.7f32).fract(), -0.7f32);
1730     }
1731
1732     #[test]
1733     fn test_abs() {
1734         assert_eq!(INFINITY.abs(), INFINITY);
1735         assert_eq!(1f32.abs(), 1f32);
1736         assert_eq!(0f32.abs(), 0f32);
1737         assert_eq!((-0f32).abs(), 0f32);
1738         assert_eq!((-1f32).abs(), 1f32);
1739         assert_eq!(NEG_INFINITY.abs(), INFINITY);
1740         assert_eq!((1f32/NEG_INFINITY).abs(), 0f32);
1741         assert!(NAN.abs().is_nan());
1742     }
1743
1744     #[test]
1745     fn test_signum() {
1746         assert_eq!(INFINITY.signum(), 1f32);
1747         assert_eq!(1f32.signum(), 1f32);
1748         assert_eq!(0f32.signum(), 1f32);
1749         assert_eq!((-0f32).signum(), -1f32);
1750         assert_eq!((-1f32).signum(), -1f32);
1751         assert_eq!(NEG_INFINITY.signum(), -1f32);
1752         assert_eq!((1f32/NEG_INFINITY).signum(), -1f32);
1753         assert!(NAN.signum().is_nan());
1754     }
1755
1756     #[test]
1757     fn test_is_sign_positive() {
1758         assert!(INFINITY.is_sign_positive());
1759         assert!(1f32.is_sign_positive());
1760         assert!(0f32.is_sign_positive());
1761         assert!(!(-0f32).is_sign_positive());
1762         assert!(!(-1f32).is_sign_positive());
1763         assert!(!NEG_INFINITY.is_sign_positive());
1764         assert!(!(1f32/NEG_INFINITY).is_sign_positive());
1765         assert!(!NAN.is_sign_positive());
1766     }
1767
1768     #[test]
1769     fn test_is_sign_negative() {
1770         assert!(!INFINITY.is_sign_negative());
1771         assert!(!1f32.is_sign_negative());
1772         assert!(!0f32.is_sign_negative());
1773         assert!((-0f32).is_sign_negative());
1774         assert!((-1f32).is_sign_negative());
1775         assert!(NEG_INFINITY.is_sign_negative());
1776         assert!((1f32/NEG_INFINITY).is_sign_negative());
1777         assert!(!NAN.is_sign_negative());
1778     }
1779
1780     #[test]
1781     fn test_mul_add() {
1782         let nan: f32 = Float::nan();
1783         let inf: f32 = Float::infinity();
1784         let neg_inf: f32 = Float::neg_infinity();
1785         assert_approx_eq!(12.3f32.mul_add(4.5, 6.7), 62.05);
1786         assert_approx_eq!((-12.3f32).mul_add(-4.5, -6.7), 48.65);
1787         assert_approx_eq!(0.0f32.mul_add(8.9, 1.2), 1.2);
1788         assert_approx_eq!(3.4f32.mul_add(-0.0, 5.6), 5.6);
1789         assert!(nan.mul_add(7.8, 9.0).is_nan());
1790         assert_eq!(inf.mul_add(7.8, 9.0), inf);
1791         assert_eq!(neg_inf.mul_add(7.8, 9.0), neg_inf);
1792         assert_eq!(8.9f32.mul_add(inf, 3.2), inf);
1793         assert_eq!((-3.2f32).mul_add(2.4, neg_inf), neg_inf);
1794     }
1795
1796     #[test]
1797     fn test_recip() {
1798         let nan: f32 = Float::nan();
1799         let inf: f32 = Float::infinity();
1800         let neg_inf: f32 = Float::neg_infinity();
1801         assert_eq!(1.0f32.recip(), 1.0);
1802         assert_eq!(2.0f32.recip(), 0.5);
1803         assert_eq!((-0.4f32).recip(), -2.5);
1804         assert_eq!(0.0f32.recip(), inf);
1805         assert!(nan.recip().is_nan());
1806         assert_eq!(inf.recip(), 0.0);
1807         assert_eq!(neg_inf.recip(), 0.0);
1808     }
1809
1810     #[test]
1811     fn test_powi() {
1812         let nan: f32 = Float::nan();
1813         let inf: f32 = Float::infinity();
1814         let neg_inf: f32 = Float::neg_infinity();
1815         assert_eq!(1.0f32.powi(1), 1.0);
1816         assert_approx_eq!((-3.1f32).powi(2), 9.61);
1817         assert_approx_eq!(5.9f32.powi(-2), 0.028727);
1818         assert_eq!(8.3f32.powi(0), 1.0);
1819         assert!(nan.powi(2).is_nan());
1820         assert_eq!(inf.powi(3), inf);
1821         assert_eq!(neg_inf.powi(2), inf);
1822     }
1823
1824     #[test]
1825     fn test_powf() {
1826         let nan: f32 = Float::nan();
1827         let inf: f32 = Float::infinity();
1828         let neg_inf: f32 = Float::neg_infinity();
1829         assert_eq!(1.0f32.powf(1.0), 1.0);
1830         assert_approx_eq!(3.4f32.powf(4.5), 246.408218);
1831         assert_approx_eq!(2.7f32.powf(-3.2), 0.041652);
1832         assert_approx_eq!((-3.1f32).powf(2.0), 9.61);
1833         assert_approx_eq!(5.9f32.powf(-2.0), 0.028727);
1834         assert_eq!(8.3f32.powf(0.0), 1.0);
1835         assert!(nan.powf(2.0).is_nan());
1836         assert_eq!(inf.powf(2.0), inf);
1837         assert_eq!(neg_inf.powf(3.0), neg_inf);
1838     }
1839
1840     #[test]
1841     fn test_sqrt_domain() {
1842         assert!(NAN.sqrt().is_nan());
1843         assert!(NEG_INFINITY.sqrt().is_nan());
1844         assert!((-1.0f32).sqrt().is_nan());
1845         assert_eq!((-0.0f32).sqrt(), -0.0);
1846         assert_eq!(0.0f32.sqrt(), 0.0);
1847         assert_eq!(1.0f32.sqrt(), 1.0);
1848         assert_eq!(INFINITY.sqrt(), INFINITY);
1849     }
1850
1851     #[test]
1852     fn test_rsqrt() {
1853         let nan: f32 = Float::nan();
1854         let inf: f32 = Float::infinity();
1855         let neg_inf: f32 = Float::neg_infinity();
1856         assert!(nan.rsqrt().is_nan());
1857         assert_eq!(inf.rsqrt(), 0.0);
1858         assert!(neg_inf.rsqrt().is_nan());
1859         assert!((-1.0f32).rsqrt().is_nan());
1860         assert_eq!((-0.0f32).rsqrt(), neg_inf);
1861         assert_eq!(0.0f32.rsqrt(), inf);
1862         assert_eq!(1.0f32.rsqrt(), 1.0);
1863         assert_eq!(4.0f32.rsqrt(), 0.5);
1864     }
1865
1866     #[test]
1867     fn test_exp() {
1868         assert_eq!(1.0, 0.0f32.exp());
1869         assert_approx_eq!(2.718282, 1.0f32.exp());
1870         assert_approx_eq!(148.413162, 5.0f32.exp());
1871
1872         let inf: f32 = Float::infinity();
1873         let neg_inf: f32 = Float::neg_infinity();
1874         let nan: f32 = Float::nan();
1875         assert_eq!(inf, inf.exp());
1876         assert_eq!(0.0, neg_inf.exp());
1877         assert!(nan.exp().is_nan());
1878     }
1879
1880     #[test]
1881     fn test_exp2() {
1882         assert_eq!(32.0, 5.0f32.exp2());
1883         assert_eq!(1.0, 0.0f32.exp2());
1884
1885         let inf: f32 = Float::infinity();
1886         let neg_inf: f32 = Float::neg_infinity();
1887         let nan: f32 = Float::nan();
1888         assert_eq!(inf, inf.exp2());
1889         assert_eq!(0.0, neg_inf.exp2());
1890         assert!(nan.exp2().is_nan());
1891     }
1892
1893     #[test]
1894     fn test_ln() {
1895         let nan: f32 = Float::nan();
1896         let inf: f32 = Float::infinity();
1897         let neg_inf: f32 = Float::neg_infinity();
1898         assert_approx_eq!(1.0f32.exp().ln(), 1.0);
1899         assert!(nan.ln().is_nan());
1900         assert_eq!(inf.ln(), inf);
1901         assert!(neg_inf.ln().is_nan());
1902         assert!((-2.3f32).ln().is_nan());
1903         assert_eq!((-0.0f32).ln(), neg_inf);
1904         assert_eq!(0.0f32.ln(), neg_inf);
1905         assert_approx_eq!(4.0f32.ln(), 1.386294);
1906     }
1907
1908     #[test]
1909     fn test_log() {
1910         let nan: f32 = Float::nan();
1911         let inf: f32 = Float::infinity();
1912         let neg_inf: f32 = Float::neg_infinity();
1913         assert_eq!(10.0f32.log(10.0), 1.0);
1914         assert_approx_eq!(2.3f32.log(3.5), 0.664858);
1915         assert_eq!(1.0f32.exp().log(1.0.exp()), 1.0);
1916         assert!(1.0f32.log(1.0).is_nan());
1917         assert!(1.0f32.log(-13.9).is_nan());
1918         assert!(nan.log(2.3).is_nan());
1919         assert_eq!(inf.log(10.0), inf);
1920         assert!(neg_inf.log(8.8).is_nan());
1921         assert!((-2.3f32).log(0.1).is_nan());
1922         assert_eq!((-0.0f32).log(2.0), neg_inf);
1923         assert_eq!(0.0f32.log(7.0), neg_inf);
1924     }
1925
1926     #[test]
1927     fn test_log2() {
1928         let nan: f32 = Float::nan();
1929         let inf: f32 = Float::infinity();
1930         let neg_inf: f32 = Float::neg_infinity();
1931         assert_approx_eq!(10.0f32.log2(), 3.321928);
1932         assert_approx_eq!(2.3f32.log2(), 1.201634);
1933         assert_approx_eq!(1.0f32.exp().log2(), 1.442695);
1934         assert!(nan.log2().is_nan());
1935         assert_eq!(inf.log2(), inf);
1936         assert!(neg_inf.log2().is_nan());
1937         assert!((-2.3f32).log2().is_nan());
1938         assert_eq!((-0.0f32).log2(), neg_inf);
1939         assert_eq!(0.0f32.log2(), neg_inf);
1940     }
1941
1942     #[test]
1943     fn test_log10() {
1944         let nan: f32 = Float::nan();
1945         let inf: f32 = Float::infinity();
1946         let neg_inf: f32 = Float::neg_infinity();
1947         assert_eq!(10.0f32.log10(), 1.0);
1948         assert_approx_eq!(2.3f32.log10(), 0.361728);
1949         assert_approx_eq!(1.0f32.exp().log10(), 0.434294);
1950         assert_eq!(1.0f32.log10(), 0.0);
1951         assert!(nan.log10().is_nan());
1952         assert_eq!(inf.log10(), inf);
1953         assert!(neg_inf.log10().is_nan());
1954         assert!((-2.3f32).log10().is_nan());
1955         assert_eq!((-0.0f32).log10(), neg_inf);
1956         assert_eq!(0.0f32.log10(), neg_inf);
1957     }
1958
1959     #[test]
1960     fn test_to_degrees() {
1961         let pi: f32 = consts::PI;
1962         let nan: f32 = Float::nan();
1963         let inf: f32 = Float::infinity();
1964         let neg_inf: f32 = Float::neg_infinity();
1965         assert_eq!(0.0f32.to_degrees(), 0.0);
1966         assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
1967         assert_eq!(pi.to_degrees(), 180.0);
1968         assert!(nan.to_degrees().is_nan());
1969         assert_eq!(inf.to_degrees(), inf);
1970         assert_eq!(neg_inf.to_degrees(), neg_inf);
1971     }
1972
1973     #[test]
1974     fn test_to_radians() {
1975         let pi: f32 = consts::PI;
1976         let nan: f32 = Float::nan();
1977         let inf: f32 = Float::infinity();
1978         let neg_inf: f32 = Float::neg_infinity();
1979         assert_eq!(0.0f32.to_radians(), 0.0);
1980         assert_approx_eq!(154.6f32.to_radians(), 2.698279);
1981         assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
1982         assert_eq!(180.0f32.to_radians(), pi);
1983         assert!(nan.to_radians().is_nan());
1984         assert_eq!(inf.to_radians(), inf);
1985         assert_eq!(neg_inf.to_radians(), neg_inf);
1986     }
1987
1988     #[test]
1989     fn test_ldexp() {
1990         // We have to use from_str until base-2 exponents
1991         // are supported in floating-point literals
1992         let f1: f32 = FromStrRadix::from_str_radix("1p-123", 16).unwrap();
1993         let f2: f32 = FromStrRadix::from_str_radix("1p-111", 16).unwrap();
1994         let f3: f32 = FromStrRadix::from_str_radix("1.Cp-12", 16).unwrap();
1995         assert_eq!(1f32.ldexp(-123), f1);
1996         assert_eq!(1f32.ldexp(-111), f2);
1997         assert_eq!(Float::ldexp(1.75f32, -12), f3);
1998
1999         assert_eq!(Float::ldexp(0f32, -123), 0f32);
2000         assert_eq!(Float::ldexp(-0f32, -123), -0f32);
2001
2002         let inf: f32 = Float::infinity();
2003         let neg_inf: f32 = Float::neg_infinity();
2004         let nan: f32 = Float::nan();
2005         assert_eq!(Float::ldexp(inf, -123), inf);
2006         assert_eq!(Float::ldexp(neg_inf, -123), neg_inf);
2007         assert!(Float::ldexp(nan, -123).is_nan());
2008     }
2009
2010     #[test]
2011     fn test_frexp() {
2012         // We have to use from_str until base-2 exponents
2013         // are supported in floating-point literals
2014         let f1: f32 = FromStrRadix::from_str_radix("1p-123", 16).unwrap();
2015         let f2: f32 = FromStrRadix::from_str_radix("1p-111", 16).unwrap();
2016         let f3: f32 = FromStrRadix::from_str_radix("1.Cp-123", 16).unwrap();
2017         let (x1, exp1) = f1.frexp();
2018         let (x2, exp2) = f2.frexp();
2019         let (x3, exp3) = f3.frexp();
2020         assert_eq!((x1, exp1), (0.5f32, -122));
2021         assert_eq!((x2, exp2), (0.5f32, -110));
2022         assert_eq!((x3, exp3), (0.875f32, -122));
2023         assert_eq!(Float::ldexp(x1, exp1), f1);
2024         assert_eq!(Float::ldexp(x2, exp2), f2);
2025         assert_eq!(Float::ldexp(x3, exp3), f3);
2026
2027         assert_eq!(0f32.frexp(), (0f32, 0));
2028         assert_eq!((-0f32).frexp(), (-0f32, 0));
2029     }
2030
2031     #[test] #[cfg_attr(windows, ignore)] // FIXME #8755
2032     fn test_frexp_nowin() {
2033         let inf: f32 = Float::infinity();
2034         let neg_inf: f32 = Float::neg_infinity();
2035         let nan: f32 = Float::nan();
2036         assert_eq!(match inf.frexp() { (x, _) => x }, inf);
2037         assert_eq!(match neg_inf.frexp() { (x, _) => x }, neg_inf);
2038         assert!(match nan.frexp() { (x, _) => x.is_nan() })
2039     }
2040
2041     #[test]
2042     fn test_abs_sub() {
2043         assert_eq!((-1f32).abs_sub(1f32), 0f32);
2044         assert_eq!(1f32.abs_sub(1f32), 0f32);
2045         assert_eq!(1f32.abs_sub(0f32), 1f32);
2046         assert_eq!(1f32.abs_sub(-1f32), 2f32);
2047         assert_eq!(NEG_INFINITY.abs_sub(0f32), 0f32);
2048         assert_eq!(INFINITY.abs_sub(1f32), INFINITY);
2049         assert_eq!(0f32.abs_sub(NEG_INFINITY), INFINITY);
2050         assert_eq!(0f32.abs_sub(INFINITY), 0f32);
2051     }
2052
2053     #[test]
2054     fn test_abs_sub_nowin() {
2055         assert!(NAN.abs_sub(-1f32).is_nan());
2056         assert!(1f32.abs_sub(NAN).is_nan());
2057     }
2058
2059     #[test]
2060     fn test_asinh() {
2061         assert_eq!(0.0f32.asinh(), 0.0f32);
2062         assert_eq!((-0.0f32).asinh(), -0.0f32);
2063
2064         let inf: f32 = Float::infinity();
2065         let neg_inf: f32 = Float::neg_infinity();
2066         let nan: f32 = Float::nan();
2067         assert_eq!(inf.asinh(), inf);
2068         assert_eq!(neg_inf.asinh(), neg_inf);
2069         assert!(nan.asinh().is_nan());
2070         assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
2071         assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
2072     }
2073
2074     #[test]
2075     fn test_acosh() {
2076         assert_eq!(1.0f32.acosh(), 0.0f32);
2077         assert!(0.999f32.acosh().is_nan());
2078
2079         let inf: f32 = Float::infinity();
2080         let neg_inf: f32 = Float::neg_infinity();
2081         let nan: f32 = Float::nan();
2082         assert_eq!(inf.acosh(), inf);
2083         assert!(neg_inf.acosh().is_nan());
2084         assert!(nan.acosh().is_nan());
2085         assert_approx_eq!(2.0f32.acosh(), 1.31695789692481670862504634730796844f32);
2086         assert_approx_eq!(3.0f32.acosh(), 1.76274717403908605046521864995958461f32);
2087     }
2088
2089     #[test]
2090     fn test_atanh() {
2091         assert_eq!(0.0f32.atanh(), 0.0f32);
2092         assert_eq!((-0.0f32).atanh(), -0.0f32);
2093
2094         let inf32: f32 = Float::infinity();
2095         let neg_inf32: f32 = Float::neg_infinity();
2096         assert_eq!(1.0f32.atanh(), inf32);
2097         assert_eq!((-1.0f32).atanh(), neg_inf32);
2098
2099         assert!(2f64.atanh().atanh().is_nan());
2100         assert!((-2f64).atanh().atanh().is_nan());
2101
2102         let inf64: f32 = Float::infinity();
2103         let neg_inf64: f32 = Float::neg_infinity();
2104         let nan32: f32 = Float::nan();
2105         assert!(inf64.atanh().is_nan());
2106         assert!(neg_inf64.atanh().is_nan());
2107         assert!(nan32.atanh().is_nan());
2108
2109         assert_approx_eq!(0.5f32.atanh(), 0.54930614433405484569762261846126285f32);
2110         assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
2111     }
2112
2113     #[test]
2114     fn test_real_consts() {
2115         use super::consts;
2116
2117         let pi: f32 = consts::PI;
2118         let two_pi: f32 = consts::PI_2;
2119         let frac_pi_2: f32 = consts::FRAC_PI_2;
2120         let frac_pi_3: f32 = consts::FRAC_PI_3;
2121         let frac_pi_4: f32 = consts::FRAC_PI_4;
2122         let frac_pi_6: f32 = consts::FRAC_PI_6;
2123         let frac_pi_8: f32 = consts::FRAC_PI_8;
2124         let frac_1_pi: f32 = consts::FRAC_1_PI;
2125         let frac_2_pi: f32 = consts::FRAC_2_PI;
2126         let frac_2_sqrtpi: f32 = consts::FRAC_2_SQRTPI;
2127         let sqrt2: f32 = consts::SQRT2;
2128         let frac_1_sqrt2: f32 = consts::FRAC_1_SQRT2;
2129         let e: f32 = consts::E;
2130         let log2_e: f32 = consts::LOG2_E;
2131         let log10_e: f32 = consts::LOG10_E;
2132         let ln_2: f32 = consts::LN_2;
2133         let ln_10: f32 = consts::LN_10;
2134
2135         assert_approx_eq!(two_pi, 2f32 * pi);
2136         assert_approx_eq!(frac_pi_2, pi / 2f32);
2137         assert_approx_eq!(frac_pi_3, pi / 3f32);
2138         assert_approx_eq!(frac_pi_4, pi / 4f32);
2139         assert_approx_eq!(frac_pi_6, pi / 6f32);
2140         assert_approx_eq!(frac_pi_8, pi / 8f32);
2141         assert_approx_eq!(frac_1_pi, 1f32 / pi);
2142         assert_approx_eq!(frac_2_pi, 2f32 / pi);
2143         assert_approx_eq!(frac_2_sqrtpi, 2f32 / pi.sqrt());
2144         assert_approx_eq!(sqrt2, 2f32.sqrt());
2145         assert_approx_eq!(frac_1_sqrt2, 1f32 / 2f32.sqrt());
2146         assert_approx_eq!(log2_e, e.log2());
2147         assert_approx_eq!(log10_e, e.log10());
2148         assert_approx_eq!(ln_2, 2f32.ln());
2149         assert_approx_eq!(ln_10, 10f32.ln());
2150     }
2151 }