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