1 | use core::mem; |
2 | use core::num::FpCategory; |
3 | use core::ops::{Add, Div, Neg}; |
4 | |
5 | use core::f32; |
6 | use core::f64; |
7 | |
8 | use {Num, NumCast, ToPrimitive}; |
9 | |
10 | #[cfg (all(not(feature = "std" ), feature = "libm" ))] |
11 | use libm; |
12 | |
13 | /// Generic trait for floating point numbers that works with `no_std`. |
14 | /// |
15 | /// This trait implements a subset of the `Float` trait. |
16 | pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy { |
17 | /// Returns positive infinity. |
18 | /// |
19 | /// # Examples |
20 | /// |
21 | /// ``` |
22 | /// use num_traits::float::FloatCore; |
23 | /// use std::{f32, f64}; |
24 | /// |
25 | /// fn check<T: FloatCore>(x: T) { |
26 | /// assert!(T::infinity() == x); |
27 | /// } |
28 | /// |
29 | /// check(f32::INFINITY); |
30 | /// check(f64::INFINITY); |
31 | /// ``` |
32 | fn infinity() -> Self; |
33 | |
34 | /// Returns negative infinity. |
35 | /// |
36 | /// # Examples |
37 | /// |
38 | /// ``` |
39 | /// use num_traits::float::FloatCore; |
40 | /// use std::{f32, f64}; |
41 | /// |
42 | /// fn check<T: FloatCore>(x: T) { |
43 | /// assert!(T::neg_infinity() == x); |
44 | /// } |
45 | /// |
46 | /// check(f32::NEG_INFINITY); |
47 | /// check(f64::NEG_INFINITY); |
48 | /// ``` |
49 | fn neg_infinity() -> Self; |
50 | |
51 | /// Returns NaN. |
52 | /// |
53 | /// # Examples |
54 | /// |
55 | /// ``` |
56 | /// use num_traits::float::FloatCore; |
57 | /// |
58 | /// fn check<T: FloatCore>() { |
59 | /// let n = T::nan(); |
60 | /// assert!(n != n); |
61 | /// } |
62 | /// |
63 | /// check::<f32>(); |
64 | /// check::<f64>(); |
65 | /// ``` |
66 | fn nan() -> Self; |
67 | |
68 | /// Returns `-0.0`. |
69 | /// |
70 | /// # Examples |
71 | /// |
72 | /// ``` |
73 | /// use num_traits::float::FloatCore; |
74 | /// use std::{f32, f64}; |
75 | /// |
76 | /// fn check<T: FloatCore>(n: T) { |
77 | /// let z = T::neg_zero(); |
78 | /// assert!(z.is_zero()); |
79 | /// assert!(T::one() / z == n); |
80 | /// } |
81 | /// |
82 | /// check(f32::NEG_INFINITY); |
83 | /// check(f64::NEG_INFINITY); |
84 | /// ``` |
85 | fn neg_zero() -> Self; |
86 | |
87 | /// Returns the smallest finite value that this type can represent. |
88 | /// |
89 | /// # Examples |
90 | /// |
91 | /// ``` |
92 | /// use num_traits::float::FloatCore; |
93 | /// use std::{f32, f64}; |
94 | /// |
95 | /// fn check<T: FloatCore>(x: T) { |
96 | /// assert!(T::min_value() == x); |
97 | /// } |
98 | /// |
99 | /// check(f32::MIN); |
100 | /// check(f64::MIN); |
101 | /// ``` |
102 | fn min_value() -> Self; |
103 | |
104 | /// Returns the smallest positive, normalized value that this type can represent. |
105 | /// |
106 | /// # Examples |
107 | /// |
108 | /// ``` |
109 | /// use num_traits::float::FloatCore; |
110 | /// use std::{f32, f64}; |
111 | /// |
112 | /// fn check<T: FloatCore>(x: T) { |
113 | /// assert!(T::min_positive_value() == x); |
114 | /// } |
115 | /// |
116 | /// check(f32::MIN_POSITIVE); |
117 | /// check(f64::MIN_POSITIVE); |
118 | /// ``` |
119 | fn min_positive_value() -> Self; |
120 | |
121 | /// Returns epsilon, a small positive value. |
122 | /// |
123 | /// # Examples |
124 | /// |
125 | /// ``` |
126 | /// use num_traits::float::FloatCore; |
127 | /// use std::{f32, f64}; |
128 | /// |
129 | /// fn check<T: FloatCore>(x: T) { |
130 | /// assert!(T::epsilon() == x); |
131 | /// } |
132 | /// |
133 | /// check(f32::EPSILON); |
134 | /// check(f64::EPSILON); |
135 | /// ``` |
136 | fn epsilon() -> Self; |
137 | |
138 | /// Returns the largest finite value that this type can represent. |
139 | /// |
140 | /// # Examples |
141 | /// |
142 | /// ``` |
143 | /// use num_traits::float::FloatCore; |
144 | /// use std::{f32, f64}; |
145 | /// |
146 | /// fn check<T: FloatCore>(x: T) { |
147 | /// assert!(T::max_value() == x); |
148 | /// } |
149 | /// |
150 | /// check(f32::MAX); |
151 | /// check(f64::MAX); |
152 | /// ``` |
153 | fn max_value() -> Self; |
154 | |
155 | /// Returns `true` if the number is NaN. |
156 | /// |
157 | /// # Examples |
158 | /// |
159 | /// ``` |
160 | /// use num_traits::float::FloatCore; |
161 | /// use std::{f32, f64}; |
162 | /// |
163 | /// fn check<T: FloatCore>(x: T, p: bool) { |
164 | /// assert!(x.is_nan() == p); |
165 | /// } |
166 | /// |
167 | /// check(f32::NAN, true); |
168 | /// check(f32::INFINITY, false); |
169 | /// check(f64::NAN, true); |
170 | /// check(0.0f64, false); |
171 | /// ``` |
172 | #[inline ] |
173 | fn is_nan(self) -> bool { |
174 | self != self |
175 | } |
176 | |
177 | /// Returns `true` if the number is infinite. |
178 | /// |
179 | /// # Examples |
180 | /// |
181 | /// ``` |
182 | /// use num_traits::float::FloatCore; |
183 | /// use std::{f32, f64}; |
184 | /// |
185 | /// fn check<T: FloatCore>(x: T, p: bool) { |
186 | /// assert!(x.is_infinite() == p); |
187 | /// } |
188 | /// |
189 | /// check(f32::INFINITY, true); |
190 | /// check(f32::NEG_INFINITY, true); |
191 | /// check(f32::NAN, false); |
192 | /// check(f64::INFINITY, true); |
193 | /// check(f64::NEG_INFINITY, true); |
194 | /// check(0.0f64, false); |
195 | /// ``` |
196 | #[inline ] |
197 | fn is_infinite(self) -> bool { |
198 | self == Self::infinity() || self == Self::neg_infinity() |
199 | } |
200 | |
201 | /// Returns `true` if the number is neither infinite or NaN. |
202 | /// |
203 | /// # Examples |
204 | /// |
205 | /// ``` |
206 | /// use num_traits::float::FloatCore; |
207 | /// use std::{f32, f64}; |
208 | /// |
209 | /// fn check<T: FloatCore>(x: T, p: bool) { |
210 | /// assert!(x.is_finite() == p); |
211 | /// } |
212 | /// |
213 | /// check(f32::INFINITY, false); |
214 | /// check(f32::MAX, true); |
215 | /// check(f64::NEG_INFINITY, false); |
216 | /// check(f64::MIN_POSITIVE, true); |
217 | /// check(f64::NAN, false); |
218 | /// ``` |
219 | #[inline ] |
220 | fn is_finite(self) -> bool { |
221 | !(self.is_nan() || self.is_infinite()) |
222 | } |
223 | |
224 | /// Returns `true` if the number is neither zero, infinite, subnormal or NaN. |
225 | /// |
226 | /// # Examples |
227 | /// |
228 | /// ``` |
229 | /// use num_traits::float::FloatCore; |
230 | /// use std::{f32, f64}; |
231 | /// |
232 | /// fn check<T: FloatCore>(x: T, p: bool) { |
233 | /// assert!(x.is_normal() == p); |
234 | /// } |
235 | /// |
236 | /// check(f32::INFINITY, false); |
237 | /// check(f32::MAX, true); |
238 | /// check(f64::NEG_INFINITY, false); |
239 | /// check(f64::MIN_POSITIVE, true); |
240 | /// check(0.0f64, false); |
241 | /// ``` |
242 | #[inline ] |
243 | fn is_normal(self) -> bool { |
244 | self.classify() == FpCategory::Normal |
245 | } |
246 | |
247 | /// Returns the floating point category of the number. If only one property |
248 | /// is going to be tested, it is generally faster to use the specific |
249 | /// predicate instead. |
250 | /// |
251 | /// # Examples |
252 | /// |
253 | /// ``` |
254 | /// use num_traits::float::FloatCore; |
255 | /// use std::{f32, f64}; |
256 | /// use std::num::FpCategory; |
257 | /// |
258 | /// fn check<T: FloatCore>(x: T, c: FpCategory) { |
259 | /// assert!(x.classify() == c); |
260 | /// } |
261 | /// |
262 | /// check(f32::INFINITY, FpCategory::Infinite); |
263 | /// check(f32::MAX, FpCategory::Normal); |
264 | /// check(f64::NAN, FpCategory::Nan); |
265 | /// check(f64::MIN_POSITIVE, FpCategory::Normal); |
266 | /// check(f64::MIN_POSITIVE / 2.0, FpCategory::Subnormal); |
267 | /// check(0.0f64, FpCategory::Zero); |
268 | /// ``` |
269 | fn classify(self) -> FpCategory; |
270 | |
271 | /// Returns the largest integer less than or equal to a number. |
272 | /// |
273 | /// # Examples |
274 | /// |
275 | /// ``` |
276 | /// use num_traits::float::FloatCore; |
277 | /// use std::{f32, f64}; |
278 | /// |
279 | /// fn check<T: FloatCore>(x: T, y: T) { |
280 | /// assert!(x.floor() == y); |
281 | /// } |
282 | /// |
283 | /// check(f32::INFINITY, f32::INFINITY); |
284 | /// check(0.9f32, 0.0); |
285 | /// check(1.0f32, 1.0); |
286 | /// check(1.1f32, 1.0); |
287 | /// check(-0.0f64, 0.0); |
288 | /// check(-0.9f64, -1.0); |
289 | /// check(-1.0f64, -1.0); |
290 | /// check(-1.1f64, -2.0); |
291 | /// check(f64::MIN, f64::MIN); |
292 | /// ``` |
293 | #[inline ] |
294 | fn floor(self) -> Self { |
295 | let f = self.fract(); |
296 | if f.is_nan() || f.is_zero() { |
297 | self |
298 | } else if self < Self::zero() { |
299 | self - f - Self::one() |
300 | } else { |
301 | self - f |
302 | } |
303 | } |
304 | |
305 | /// Returns the smallest integer greater than or equal to a number. |
306 | /// |
307 | /// # Examples |
308 | /// |
309 | /// ``` |
310 | /// use num_traits::float::FloatCore; |
311 | /// use std::{f32, f64}; |
312 | /// |
313 | /// fn check<T: FloatCore>(x: T, y: T) { |
314 | /// assert!(x.ceil() == y); |
315 | /// } |
316 | /// |
317 | /// check(f32::INFINITY, f32::INFINITY); |
318 | /// check(0.9f32, 1.0); |
319 | /// check(1.0f32, 1.0); |
320 | /// check(1.1f32, 2.0); |
321 | /// check(-0.0f64, 0.0); |
322 | /// check(-0.9f64, -0.0); |
323 | /// check(-1.0f64, -1.0); |
324 | /// check(-1.1f64, -1.0); |
325 | /// check(f64::MIN, f64::MIN); |
326 | /// ``` |
327 | #[inline ] |
328 | fn ceil(self) -> Self { |
329 | let f = self.fract(); |
330 | if f.is_nan() || f.is_zero() { |
331 | self |
332 | } else if self > Self::zero() { |
333 | self - f + Self::one() |
334 | } else { |
335 | self - f |
336 | } |
337 | } |
338 | |
339 | /// Returns the nearest integer to a number. Round half-way cases away from `0.0`. |
340 | /// |
341 | /// # Examples |
342 | /// |
343 | /// ``` |
344 | /// use num_traits::float::FloatCore; |
345 | /// use std::{f32, f64}; |
346 | /// |
347 | /// fn check<T: FloatCore>(x: T, y: T) { |
348 | /// assert!(x.round() == y); |
349 | /// } |
350 | /// |
351 | /// check(f32::INFINITY, f32::INFINITY); |
352 | /// check(0.4f32, 0.0); |
353 | /// check(0.5f32, 1.0); |
354 | /// check(0.6f32, 1.0); |
355 | /// check(-0.4f64, 0.0); |
356 | /// check(-0.5f64, -1.0); |
357 | /// check(-0.6f64, -1.0); |
358 | /// check(f64::MIN, f64::MIN); |
359 | /// ``` |
360 | #[inline ] |
361 | fn round(self) -> Self { |
362 | let one = Self::one(); |
363 | let h = Self::from(0.5).expect("Unable to cast from 0.5" ); |
364 | let f = self.fract(); |
365 | if f.is_nan() || f.is_zero() { |
366 | self |
367 | } else if self > Self::zero() { |
368 | if f < h { |
369 | self - f |
370 | } else { |
371 | self - f + one |
372 | } |
373 | } else { |
374 | if -f < h { |
375 | self - f |
376 | } else { |
377 | self - f - one |
378 | } |
379 | } |
380 | } |
381 | |
382 | /// Return the integer part of a number. |
383 | /// |
384 | /// # Examples |
385 | /// |
386 | /// ``` |
387 | /// use num_traits::float::FloatCore; |
388 | /// use std::{f32, f64}; |
389 | /// |
390 | /// fn check<T: FloatCore>(x: T, y: T) { |
391 | /// assert!(x.trunc() == y); |
392 | /// } |
393 | /// |
394 | /// check(f32::INFINITY, f32::INFINITY); |
395 | /// check(0.9f32, 0.0); |
396 | /// check(1.0f32, 1.0); |
397 | /// check(1.1f32, 1.0); |
398 | /// check(-0.0f64, 0.0); |
399 | /// check(-0.9f64, -0.0); |
400 | /// check(-1.0f64, -1.0); |
401 | /// check(-1.1f64, -1.0); |
402 | /// check(f64::MIN, f64::MIN); |
403 | /// ``` |
404 | #[inline ] |
405 | fn trunc(self) -> Self { |
406 | let f = self.fract(); |
407 | if f.is_nan() { |
408 | self |
409 | } else { |
410 | self - f |
411 | } |
412 | } |
413 | |
414 | /// Returns the fractional part of a number. |
415 | /// |
416 | /// # Examples |
417 | /// |
418 | /// ``` |
419 | /// use num_traits::float::FloatCore; |
420 | /// use std::{f32, f64}; |
421 | /// |
422 | /// fn check<T: FloatCore>(x: T, y: T) { |
423 | /// assert!(x.fract() == y); |
424 | /// } |
425 | /// |
426 | /// check(f32::MAX, 0.0); |
427 | /// check(0.75f32, 0.75); |
428 | /// check(1.0f32, 0.0); |
429 | /// check(1.25f32, 0.25); |
430 | /// check(-0.0f64, 0.0); |
431 | /// check(-0.75f64, -0.75); |
432 | /// check(-1.0f64, 0.0); |
433 | /// check(-1.25f64, -0.25); |
434 | /// check(f64::MIN, 0.0); |
435 | /// ``` |
436 | #[inline ] |
437 | fn fract(self) -> Self { |
438 | if self.is_zero() { |
439 | Self::zero() |
440 | } else { |
441 | self % Self::one() |
442 | } |
443 | } |
444 | |
445 | /// Computes the absolute value of `self`. Returns `FloatCore::nan()` if the |
446 | /// number is `FloatCore::nan()`. |
447 | /// |
448 | /// # Examples |
449 | /// |
450 | /// ``` |
451 | /// use num_traits::float::FloatCore; |
452 | /// use std::{f32, f64}; |
453 | /// |
454 | /// fn check<T: FloatCore>(x: T, y: T) { |
455 | /// assert!(x.abs() == y); |
456 | /// } |
457 | /// |
458 | /// check(f32::INFINITY, f32::INFINITY); |
459 | /// check(1.0f32, 1.0); |
460 | /// check(0.0f64, 0.0); |
461 | /// check(-0.0f64, 0.0); |
462 | /// check(-1.0f64, 1.0); |
463 | /// check(f64::MIN, f64::MAX); |
464 | /// ``` |
465 | #[inline ] |
466 | fn abs(self) -> Self { |
467 | if self.is_sign_positive() { |
468 | return self; |
469 | } |
470 | if self.is_sign_negative() { |
471 | return -self; |
472 | } |
473 | Self::nan() |
474 | } |
475 | |
476 | /// Returns a number that represents the sign of `self`. |
477 | /// |
478 | /// - `1.0` if the number is positive, `+0.0` or `FloatCore::infinity()` |
479 | /// - `-1.0` if the number is negative, `-0.0` or `FloatCore::neg_infinity()` |
480 | /// - `FloatCore::nan()` if the number is `FloatCore::nan()` |
481 | /// |
482 | /// # Examples |
483 | /// |
484 | /// ``` |
485 | /// use num_traits::float::FloatCore; |
486 | /// use std::{f32, f64}; |
487 | /// |
488 | /// fn check<T: FloatCore>(x: T, y: T) { |
489 | /// assert!(x.signum() == y); |
490 | /// } |
491 | /// |
492 | /// check(f32::INFINITY, 1.0); |
493 | /// check(3.0f32, 1.0); |
494 | /// check(0.0f32, 1.0); |
495 | /// check(-0.0f64, -1.0); |
496 | /// check(-3.0f64, -1.0); |
497 | /// check(f64::MIN, -1.0); |
498 | /// ``` |
499 | #[inline ] |
500 | fn signum(self) -> Self { |
501 | if self.is_nan() { |
502 | Self::nan() |
503 | } else if self.is_sign_negative() { |
504 | -Self::one() |
505 | } else { |
506 | Self::one() |
507 | } |
508 | } |
509 | |
510 | /// Returns `true` if `self` is positive, including `+0.0` and |
511 | /// `FloatCore::infinity()`, and since Rust 1.20 also |
512 | /// `FloatCore::nan()`. |
513 | /// |
514 | /// # Examples |
515 | /// |
516 | /// ``` |
517 | /// use num_traits::float::FloatCore; |
518 | /// use std::{f32, f64}; |
519 | /// |
520 | /// fn check<T: FloatCore>(x: T, p: bool) { |
521 | /// assert!(x.is_sign_positive() == p); |
522 | /// } |
523 | /// |
524 | /// check(f32::INFINITY, true); |
525 | /// check(f32::MAX, true); |
526 | /// check(0.0f32, true); |
527 | /// check(-0.0f64, false); |
528 | /// check(f64::NEG_INFINITY, false); |
529 | /// check(f64::MIN_POSITIVE, true); |
530 | /// check(-f64::NAN, false); |
531 | /// ``` |
532 | #[inline ] |
533 | fn is_sign_positive(self) -> bool { |
534 | !self.is_sign_negative() |
535 | } |
536 | |
537 | /// Returns `true` if `self` is negative, including `-0.0` and |
538 | /// `FloatCore::neg_infinity()`, and since Rust 1.20 also |
539 | /// `-FloatCore::nan()`. |
540 | /// |
541 | /// # Examples |
542 | /// |
543 | /// ``` |
544 | /// use num_traits::float::FloatCore; |
545 | /// use std::{f32, f64}; |
546 | /// |
547 | /// fn check<T: FloatCore>(x: T, p: bool) { |
548 | /// assert!(x.is_sign_negative() == p); |
549 | /// } |
550 | /// |
551 | /// check(f32::INFINITY, false); |
552 | /// check(f32::MAX, false); |
553 | /// check(0.0f32, false); |
554 | /// check(-0.0f64, true); |
555 | /// check(f64::NEG_INFINITY, true); |
556 | /// check(f64::MIN_POSITIVE, false); |
557 | /// check(f64::NAN, false); |
558 | /// ``` |
559 | #[inline ] |
560 | fn is_sign_negative(self) -> bool { |
561 | let (_, _, sign) = self.integer_decode(); |
562 | sign < 0 |
563 | } |
564 | |
565 | /// Returns the minimum of the two numbers. |
566 | /// |
567 | /// If one of the arguments is NaN, then the other argument is returned. |
568 | /// |
569 | /// # Examples |
570 | /// |
571 | /// ``` |
572 | /// use num_traits::float::FloatCore; |
573 | /// use std::{f32, f64}; |
574 | /// |
575 | /// fn check<T: FloatCore>(x: T, y: T, min: T) { |
576 | /// assert!(x.min(y) == min); |
577 | /// } |
578 | /// |
579 | /// check(1.0f32, 2.0, 1.0); |
580 | /// check(f32::NAN, 2.0, 2.0); |
581 | /// check(1.0f64, -2.0, -2.0); |
582 | /// check(1.0f64, f64::NAN, 1.0); |
583 | /// ``` |
584 | #[inline ] |
585 | fn min(self, other: Self) -> Self { |
586 | if self.is_nan() { |
587 | return other; |
588 | } |
589 | if other.is_nan() { |
590 | return self; |
591 | } |
592 | if self < other { |
593 | self |
594 | } else { |
595 | other |
596 | } |
597 | } |
598 | |
599 | /// Returns the maximum of the two numbers. |
600 | /// |
601 | /// If one of the arguments is NaN, then the other argument is returned. |
602 | /// |
603 | /// # Examples |
604 | /// |
605 | /// ``` |
606 | /// use num_traits::float::FloatCore; |
607 | /// use std::{f32, f64}; |
608 | /// |
609 | /// fn check<T: FloatCore>(x: T, y: T, max: T) { |
610 | /// assert!(x.max(y) == max); |
611 | /// } |
612 | /// |
613 | /// check(1.0f32, 2.0, 2.0); |
614 | /// check(1.0f32, f32::NAN, 1.0); |
615 | /// check(-1.0f64, 2.0, 2.0); |
616 | /// check(-1.0f64, f64::NAN, -1.0); |
617 | /// ``` |
618 | #[inline ] |
619 | fn max(self, other: Self) -> Self { |
620 | if self.is_nan() { |
621 | return other; |
622 | } |
623 | if other.is_nan() { |
624 | return self; |
625 | } |
626 | if self > other { |
627 | self |
628 | } else { |
629 | other |
630 | } |
631 | } |
632 | |
633 | /// Returns the reciprocal (multiplicative inverse) of the number. |
634 | /// |
635 | /// # Examples |
636 | /// |
637 | /// ``` |
638 | /// use num_traits::float::FloatCore; |
639 | /// use std::{f32, f64}; |
640 | /// |
641 | /// fn check<T: FloatCore>(x: T, y: T) { |
642 | /// assert!(x.recip() == y); |
643 | /// assert!(y.recip() == x); |
644 | /// } |
645 | /// |
646 | /// check(f32::INFINITY, 0.0); |
647 | /// check(2.0f32, 0.5); |
648 | /// check(-0.25f64, -4.0); |
649 | /// check(-0.0f64, f64::NEG_INFINITY); |
650 | /// ``` |
651 | #[inline ] |
652 | fn recip(self) -> Self { |
653 | Self::one() / self |
654 | } |
655 | |
656 | /// Raise a number to an integer power. |
657 | /// |
658 | /// Using this function is generally faster than using `powf` |
659 | /// |
660 | /// # Examples |
661 | /// |
662 | /// ``` |
663 | /// use num_traits::float::FloatCore; |
664 | /// |
665 | /// fn check<T: FloatCore>(x: T, exp: i32, powi: T) { |
666 | /// assert!(x.powi(exp) == powi); |
667 | /// } |
668 | /// |
669 | /// check(9.0f32, 2, 81.0); |
670 | /// check(1.0f32, -2, 1.0); |
671 | /// check(10.0f64, 20, 1e20); |
672 | /// check(4.0f64, -2, 0.0625); |
673 | /// check(-1.0f64, std::i32::MIN, 1.0); |
674 | /// ``` |
675 | #[inline ] |
676 | fn powi(mut self, mut exp: i32) -> Self { |
677 | if exp < 0 { |
678 | exp = exp.wrapping_neg(); |
679 | self = self.recip(); |
680 | } |
681 | // It should always be possible to convert a positive `i32` to a `usize`. |
682 | // Note, `i32::MIN` will wrap and still be negative, so we need to convert |
683 | // to `u32` without sign-extension before growing to `usize`. |
684 | super::pow(self, (exp as u32).to_usize().unwrap()) |
685 | } |
686 | |
687 | /// Converts to degrees, assuming the number is in radians. |
688 | /// |
689 | /// # Examples |
690 | /// |
691 | /// ``` |
692 | /// use num_traits::float::FloatCore; |
693 | /// use std::{f32, f64}; |
694 | /// |
695 | /// fn check<T: FloatCore>(rad: T, deg: T) { |
696 | /// assert!(rad.to_degrees() == deg); |
697 | /// } |
698 | /// |
699 | /// check(0.0f32, 0.0); |
700 | /// check(f32::consts::PI, 180.0); |
701 | /// check(f64::consts::FRAC_PI_4, 45.0); |
702 | /// check(f64::INFINITY, f64::INFINITY); |
703 | /// ``` |
704 | fn to_degrees(self) -> Self; |
705 | |
706 | /// Converts to radians, assuming the number is in degrees. |
707 | /// |
708 | /// # Examples |
709 | /// |
710 | /// ``` |
711 | /// use num_traits::float::FloatCore; |
712 | /// use std::{f32, f64}; |
713 | /// |
714 | /// fn check<T: FloatCore>(deg: T, rad: T) { |
715 | /// assert!(deg.to_radians() == rad); |
716 | /// } |
717 | /// |
718 | /// check(0.0f32, 0.0); |
719 | /// check(180.0, f32::consts::PI); |
720 | /// check(45.0, f64::consts::FRAC_PI_4); |
721 | /// check(f64::INFINITY, f64::INFINITY); |
722 | /// ``` |
723 | fn to_radians(self) -> Self; |
724 | |
725 | /// Returns the mantissa, base 2 exponent, and sign as integers, respectively. |
726 | /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. |
727 | /// |
728 | /// # Examples |
729 | /// |
730 | /// ``` |
731 | /// use num_traits::float::FloatCore; |
732 | /// use std::{f32, f64}; |
733 | /// |
734 | /// fn check<T: FloatCore>(x: T, m: u64, e: i16, s:i8) { |
735 | /// let (mantissa, exponent, sign) = x.integer_decode(); |
736 | /// assert_eq!(mantissa, m); |
737 | /// assert_eq!(exponent, e); |
738 | /// assert_eq!(sign, s); |
739 | /// } |
740 | /// |
741 | /// check(2.0f32, 1 << 23, -22, 1); |
742 | /// check(-2.0f32, 1 << 23, -22, -1); |
743 | /// check(f32::INFINITY, 1 << 23, 105, 1); |
744 | /// check(f64::NEG_INFINITY, 1 << 52, 972, -1); |
745 | /// ``` |
746 | fn integer_decode(self) -> (u64, i16, i8); |
747 | } |
748 | |
749 | impl FloatCore for f32 { |
750 | constant! { |
751 | infinity() -> f32::INFINITY; |
752 | neg_infinity() -> f32::NEG_INFINITY; |
753 | nan() -> f32::NAN; |
754 | neg_zero() -> -0.0; |
755 | min_value() -> f32::MIN; |
756 | min_positive_value() -> f32::MIN_POSITIVE; |
757 | epsilon() -> f32::EPSILON; |
758 | max_value() -> f32::MAX; |
759 | } |
760 | |
761 | #[inline ] |
762 | fn integer_decode(self) -> (u64, i16, i8) { |
763 | integer_decode_f32(self) |
764 | } |
765 | |
766 | #[inline ] |
767 | #[cfg (not(feature = "std" ))] |
768 | fn classify(self) -> FpCategory { |
769 | const EXP_MASK: u32 = 0x7f800000; |
770 | const MAN_MASK: u32 = 0x007fffff; |
771 | |
772 | // Safety: this identical to the implementation of f32::to_bits(), |
773 | // which is only available starting at Rust 1.20 |
774 | let bits: u32 = unsafe { mem::transmute(self) }; |
775 | match (bits & MAN_MASK, bits & EXP_MASK) { |
776 | (0, 0) => FpCategory::Zero, |
777 | (_, 0) => FpCategory::Subnormal, |
778 | (0, EXP_MASK) => FpCategory::Infinite, |
779 | (_, EXP_MASK) => FpCategory::Nan, |
780 | _ => FpCategory::Normal, |
781 | } |
782 | } |
783 | |
784 | #[inline ] |
785 | #[cfg (not(feature = "std" ))] |
786 | fn is_sign_negative(self) -> bool { |
787 | const SIGN_MASK: u32 = 0x80000000; |
788 | |
789 | // Safety: this identical to the implementation of f32::to_bits(), |
790 | // which is only available starting at Rust 1.20 |
791 | let bits: u32 = unsafe { mem::transmute(self) }; |
792 | bits & SIGN_MASK != 0 |
793 | } |
794 | |
795 | #[inline ] |
796 | #[cfg (not(feature = "std" ))] |
797 | fn to_degrees(self) -> Self { |
798 | // Use a constant for better precision. |
799 | const PIS_IN_180: f32 = 57.2957795130823208767981548141051703_f32; |
800 | self * PIS_IN_180 |
801 | } |
802 | |
803 | #[inline ] |
804 | #[cfg (not(feature = "std" ))] |
805 | fn to_radians(self) -> Self { |
806 | self * (f32::consts::PI / 180.0) |
807 | } |
808 | |
809 | #[cfg (feature = "std" )] |
810 | forward! { |
811 | Self::is_nan(self) -> bool; |
812 | Self::is_infinite(self) -> bool; |
813 | Self::is_finite(self) -> bool; |
814 | Self::is_normal(self) -> bool; |
815 | Self::classify(self) -> FpCategory; |
816 | Self::floor(self) -> Self; |
817 | Self::ceil(self) -> Self; |
818 | Self::round(self) -> Self; |
819 | Self::trunc(self) -> Self; |
820 | Self::fract(self) -> Self; |
821 | Self::abs(self) -> Self; |
822 | Self::signum(self) -> Self; |
823 | Self::is_sign_positive(self) -> bool; |
824 | Self::is_sign_negative(self) -> bool; |
825 | Self::min(self, other: Self) -> Self; |
826 | Self::max(self, other: Self) -> Self; |
827 | Self::recip(self) -> Self; |
828 | Self::powi(self, n: i32) -> Self; |
829 | Self::to_degrees(self) -> Self; |
830 | Self::to_radians(self) -> Self; |
831 | } |
832 | |
833 | #[cfg (all(not(feature = "std" ), feature = "libm" ))] |
834 | forward! { |
835 | libm::floorf as floor(self) -> Self; |
836 | libm::ceilf as ceil(self) -> Self; |
837 | libm::roundf as round(self) -> Self; |
838 | libm::truncf as trunc(self) -> Self; |
839 | libm::fabsf as abs(self) -> Self; |
840 | libm::fminf as min(self, other: Self) -> Self; |
841 | libm::fmaxf as max(self, other: Self) -> Self; |
842 | } |
843 | |
844 | #[cfg (all(not(feature = "std" ), feature = "libm" ))] |
845 | #[inline ] |
846 | fn fract(self) -> Self { |
847 | self - libm::truncf(self) |
848 | } |
849 | } |
850 | |
851 | impl FloatCore for f64 { |
852 | constant! { |
853 | infinity() -> f64::INFINITY; |
854 | neg_infinity() -> f64::NEG_INFINITY; |
855 | nan() -> f64::NAN; |
856 | neg_zero() -> -0.0; |
857 | min_value() -> f64::MIN; |
858 | min_positive_value() -> f64::MIN_POSITIVE; |
859 | epsilon() -> f64::EPSILON; |
860 | max_value() -> f64::MAX; |
861 | } |
862 | |
863 | #[inline ] |
864 | fn integer_decode(self) -> (u64, i16, i8) { |
865 | integer_decode_f64(self) |
866 | } |
867 | |
868 | #[inline ] |
869 | #[cfg (not(feature = "std" ))] |
870 | fn classify(self) -> FpCategory { |
871 | const EXP_MASK: u64 = 0x7ff0000000000000; |
872 | const MAN_MASK: u64 = 0x000fffffffffffff; |
873 | |
874 | // Safety: this identical to the implementation of f64::to_bits(), |
875 | // which is only available starting at Rust 1.20 |
876 | let bits: u64 = unsafe { mem::transmute(self) }; |
877 | match (bits & MAN_MASK, bits & EXP_MASK) { |
878 | (0, 0) => FpCategory::Zero, |
879 | (_, 0) => FpCategory::Subnormal, |
880 | (0, EXP_MASK) => FpCategory::Infinite, |
881 | (_, EXP_MASK) => FpCategory::Nan, |
882 | _ => FpCategory::Normal, |
883 | } |
884 | } |
885 | |
886 | #[inline ] |
887 | #[cfg (not(feature = "std" ))] |
888 | fn is_sign_negative(self) -> bool { |
889 | const SIGN_MASK: u64 = 0x8000000000000000; |
890 | |
891 | // Safety: this identical to the implementation of f64::to_bits(), |
892 | // which is only available starting at Rust 1.20 |
893 | let bits: u64 = unsafe { mem::transmute(self) }; |
894 | bits & SIGN_MASK != 0 |
895 | } |
896 | |
897 | #[inline ] |
898 | #[cfg (not(feature = "std" ))] |
899 | fn to_degrees(self) -> Self { |
900 | // The division here is correctly rounded with respect to the true |
901 | // value of 180/π. (This differs from f32, where a constant must be |
902 | // used to ensure a correctly rounded result.) |
903 | self * (180.0 / f64::consts::PI) |
904 | } |
905 | |
906 | #[inline ] |
907 | #[cfg (not(feature = "std" ))] |
908 | fn to_radians(self) -> Self { |
909 | self * (f64::consts::PI / 180.0) |
910 | } |
911 | |
912 | #[cfg (feature = "std" )] |
913 | forward! { |
914 | Self::is_nan(self) -> bool; |
915 | Self::is_infinite(self) -> bool; |
916 | Self::is_finite(self) -> bool; |
917 | Self::is_normal(self) -> bool; |
918 | Self::classify(self) -> FpCategory; |
919 | Self::floor(self) -> Self; |
920 | Self::ceil(self) -> Self; |
921 | Self::round(self) -> Self; |
922 | Self::trunc(self) -> Self; |
923 | Self::fract(self) -> Self; |
924 | Self::abs(self) -> Self; |
925 | Self::signum(self) -> Self; |
926 | Self::is_sign_positive(self) -> bool; |
927 | Self::is_sign_negative(self) -> bool; |
928 | Self::min(self, other: Self) -> Self; |
929 | Self::max(self, other: Self) -> Self; |
930 | Self::recip(self) -> Self; |
931 | Self::powi(self, n: i32) -> Self; |
932 | Self::to_degrees(self) -> Self; |
933 | Self::to_radians(self) -> Self; |
934 | } |
935 | |
936 | #[cfg (all(not(feature = "std" ), feature = "libm" ))] |
937 | forward! { |
938 | libm::floor as floor(self) -> Self; |
939 | libm::ceil as ceil(self) -> Self; |
940 | libm::round as round(self) -> Self; |
941 | libm::trunc as trunc(self) -> Self; |
942 | libm::fabs as abs(self) -> Self; |
943 | libm::fmin as min(self, other: Self) -> Self; |
944 | libm::fmax as max(self, other: Self) -> Self; |
945 | } |
946 | |
947 | #[cfg (all(not(feature = "std" ), feature = "libm" ))] |
948 | #[inline ] |
949 | fn fract(self) -> Self { |
950 | self - libm::trunc(self) |
951 | } |
952 | } |
953 | |
954 | // FIXME: these doctests aren't actually helpful, because they're using and |
955 | // testing the inherent methods directly, not going through `Float`. |
956 | |
957 | /// Generic trait for floating point numbers |
958 | /// |
959 | /// This trait is only available with the `std` feature, or with the `libm` feature otherwise. |
960 | #[cfg (any(feature = "std" , feature = "libm" ))] |
961 | pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> { |
962 | /// Returns the `NaN` value. |
963 | /// |
964 | /// ``` |
965 | /// use num_traits::Float; |
966 | /// |
967 | /// let nan: f32 = Float::nan(); |
968 | /// |
969 | /// assert!(nan.is_nan()); |
970 | /// ``` |
971 | fn nan() -> Self; |
972 | /// Returns the infinite value. |
973 | /// |
974 | /// ``` |
975 | /// use num_traits::Float; |
976 | /// use std::f32; |
977 | /// |
978 | /// let infinity: f32 = Float::infinity(); |
979 | /// |
980 | /// assert!(infinity.is_infinite()); |
981 | /// assert!(!infinity.is_finite()); |
982 | /// assert!(infinity > f32::MAX); |
983 | /// ``` |
984 | fn infinity() -> Self; |
985 | /// Returns the negative infinite value. |
986 | /// |
987 | /// ``` |
988 | /// use num_traits::Float; |
989 | /// use std::f32; |
990 | /// |
991 | /// let neg_infinity: f32 = Float::neg_infinity(); |
992 | /// |
993 | /// assert!(neg_infinity.is_infinite()); |
994 | /// assert!(!neg_infinity.is_finite()); |
995 | /// assert!(neg_infinity < f32::MIN); |
996 | /// ``` |
997 | fn neg_infinity() -> Self; |
998 | /// Returns `-0.0`. |
999 | /// |
1000 | /// ``` |
1001 | /// use num_traits::{Zero, Float}; |
1002 | /// |
1003 | /// let inf: f32 = Float::infinity(); |
1004 | /// let zero: f32 = Zero::zero(); |
1005 | /// let neg_zero: f32 = Float::neg_zero(); |
1006 | /// |
1007 | /// assert_eq!(zero, neg_zero); |
1008 | /// assert_eq!(7.0f32/inf, zero); |
1009 | /// assert_eq!(zero * 10.0, zero); |
1010 | /// ``` |
1011 | fn neg_zero() -> Self; |
1012 | |
1013 | /// Returns the smallest finite value that this type can represent. |
1014 | /// |
1015 | /// ``` |
1016 | /// use num_traits::Float; |
1017 | /// use std::f64; |
1018 | /// |
1019 | /// let x: f64 = Float::min_value(); |
1020 | /// |
1021 | /// assert_eq!(x, f64::MIN); |
1022 | /// ``` |
1023 | fn min_value() -> Self; |
1024 | |
1025 | /// Returns the smallest positive, normalized value that this type can represent. |
1026 | /// |
1027 | /// ``` |
1028 | /// use num_traits::Float; |
1029 | /// use std::f64; |
1030 | /// |
1031 | /// let x: f64 = Float::min_positive_value(); |
1032 | /// |
1033 | /// assert_eq!(x, f64::MIN_POSITIVE); |
1034 | /// ``` |
1035 | fn min_positive_value() -> Self; |
1036 | |
1037 | /// Returns epsilon, a small positive value. |
1038 | /// |
1039 | /// ``` |
1040 | /// use num_traits::Float; |
1041 | /// use std::f64; |
1042 | /// |
1043 | /// let x: f64 = Float::epsilon(); |
1044 | /// |
1045 | /// assert_eq!(x, f64::EPSILON); |
1046 | /// ``` |
1047 | /// |
1048 | /// # Panics |
1049 | /// |
1050 | /// The default implementation will panic if `f32::EPSILON` cannot |
1051 | /// be cast to `Self`. |
1052 | fn epsilon() -> Self { |
1053 | Self::from(f32::EPSILON).expect("Unable to cast from f32::EPSILON" ) |
1054 | } |
1055 | |
1056 | /// Returns the largest finite value that this type can represent. |
1057 | /// |
1058 | /// ``` |
1059 | /// use num_traits::Float; |
1060 | /// use std::f64; |
1061 | /// |
1062 | /// let x: f64 = Float::max_value(); |
1063 | /// assert_eq!(x, f64::MAX); |
1064 | /// ``` |
1065 | fn max_value() -> Self; |
1066 | |
1067 | /// Returns `true` if this value is `NaN` and false otherwise. |
1068 | /// |
1069 | /// ``` |
1070 | /// use num_traits::Float; |
1071 | /// use std::f64; |
1072 | /// |
1073 | /// let nan = f64::NAN; |
1074 | /// let f = 7.0; |
1075 | /// |
1076 | /// assert!(nan.is_nan()); |
1077 | /// assert!(!f.is_nan()); |
1078 | /// ``` |
1079 | fn is_nan(self) -> bool; |
1080 | |
1081 | /// Returns `true` if this value is positive infinity or negative infinity and |
1082 | /// false otherwise. |
1083 | /// |
1084 | /// ``` |
1085 | /// use num_traits::Float; |
1086 | /// use std::f32; |
1087 | /// |
1088 | /// let f = 7.0f32; |
1089 | /// let inf: f32 = Float::infinity(); |
1090 | /// let neg_inf: f32 = Float::neg_infinity(); |
1091 | /// let nan: f32 = f32::NAN; |
1092 | /// |
1093 | /// assert!(!f.is_infinite()); |
1094 | /// assert!(!nan.is_infinite()); |
1095 | /// |
1096 | /// assert!(inf.is_infinite()); |
1097 | /// assert!(neg_inf.is_infinite()); |
1098 | /// ``` |
1099 | fn is_infinite(self) -> bool; |
1100 | |
1101 | /// Returns `true` if this number is neither infinite nor `NaN`. |
1102 | /// |
1103 | /// ``` |
1104 | /// use num_traits::Float; |
1105 | /// use std::f32; |
1106 | /// |
1107 | /// let f = 7.0f32; |
1108 | /// let inf: f32 = Float::infinity(); |
1109 | /// let neg_inf: f32 = Float::neg_infinity(); |
1110 | /// let nan: f32 = f32::NAN; |
1111 | /// |
1112 | /// assert!(f.is_finite()); |
1113 | /// |
1114 | /// assert!(!nan.is_finite()); |
1115 | /// assert!(!inf.is_finite()); |
1116 | /// assert!(!neg_inf.is_finite()); |
1117 | /// ``` |
1118 | fn is_finite(self) -> bool; |
1119 | |
1120 | /// Returns `true` if the number is neither zero, infinite, |
1121 | /// [subnormal][subnormal], or `NaN`. |
1122 | /// |
1123 | /// ``` |
1124 | /// use num_traits::Float; |
1125 | /// use std::f32; |
1126 | /// |
1127 | /// let min = f32::MIN_POSITIVE; // 1.17549435e-38f32 |
1128 | /// let max = f32::MAX; |
1129 | /// let lower_than_min = 1.0e-40_f32; |
1130 | /// let zero = 0.0f32; |
1131 | /// |
1132 | /// assert!(min.is_normal()); |
1133 | /// assert!(max.is_normal()); |
1134 | /// |
1135 | /// assert!(!zero.is_normal()); |
1136 | /// assert!(!f32::NAN.is_normal()); |
1137 | /// assert!(!f32::INFINITY.is_normal()); |
1138 | /// // Values between `0` and `min` are Subnormal. |
1139 | /// assert!(!lower_than_min.is_normal()); |
1140 | /// ``` |
1141 | /// [subnormal]: http://en.wikipedia.org/wiki/Denormal_number |
1142 | fn is_normal(self) -> bool; |
1143 | |
1144 | /// Returns the floating point category of the number. If only one property |
1145 | /// is going to be tested, it is generally faster to use the specific |
1146 | /// predicate instead. |
1147 | /// |
1148 | /// ``` |
1149 | /// use num_traits::Float; |
1150 | /// use std::num::FpCategory; |
1151 | /// use std::f32; |
1152 | /// |
1153 | /// let num = 12.4f32; |
1154 | /// let inf = f32::INFINITY; |
1155 | /// |
1156 | /// assert_eq!(num.classify(), FpCategory::Normal); |
1157 | /// assert_eq!(inf.classify(), FpCategory::Infinite); |
1158 | /// ``` |
1159 | fn classify(self) -> FpCategory; |
1160 | |
1161 | /// Returns the largest integer less than or equal to a number. |
1162 | /// |
1163 | /// ``` |
1164 | /// use num_traits::Float; |
1165 | /// |
1166 | /// let f = 3.99; |
1167 | /// let g = 3.0; |
1168 | /// |
1169 | /// assert_eq!(f.floor(), 3.0); |
1170 | /// assert_eq!(g.floor(), 3.0); |
1171 | /// ``` |
1172 | fn floor(self) -> Self; |
1173 | |
1174 | /// Returns the smallest integer greater than or equal to a number. |
1175 | /// |
1176 | /// ``` |
1177 | /// use num_traits::Float; |
1178 | /// |
1179 | /// let f = 3.01; |
1180 | /// let g = 4.0; |
1181 | /// |
1182 | /// assert_eq!(f.ceil(), 4.0); |
1183 | /// assert_eq!(g.ceil(), 4.0); |
1184 | /// ``` |
1185 | fn ceil(self) -> Self; |
1186 | |
1187 | /// Returns the nearest integer to a number. Round half-way cases away from |
1188 | /// `0.0`. |
1189 | /// |
1190 | /// ``` |
1191 | /// use num_traits::Float; |
1192 | /// |
1193 | /// let f = 3.3; |
1194 | /// let g = -3.3; |
1195 | /// |
1196 | /// assert_eq!(f.round(), 3.0); |
1197 | /// assert_eq!(g.round(), -3.0); |
1198 | /// ``` |
1199 | fn round(self) -> Self; |
1200 | |
1201 | /// Return the integer part of a number. |
1202 | /// |
1203 | /// ``` |
1204 | /// use num_traits::Float; |
1205 | /// |
1206 | /// let f = 3.3; |
1207 | /// let g = -3.7; |
1208 | /// |
1209 | /// assert_eq!(f.trunc(), 3.0); |
1210 | /// assert_eq!(g.trunc(), -3.0); |
1211 | /// ``` |
1212 | fn trunc(self) -> Self; |
1213 | |
1214 | /// Returns the fractional part of a number. |
1215 | /// |
1216 | /// ``` |
1217 | /// use num_traits::Float; |
1218 | /// |
1219 | /// let x = 3.5; |
1220 | /// let y = -3.5; |
1221 | /// let abs_difference_x = (x.fract() - 0.5).abs(); |
1222 | /// let abs_difference_y = (y.fract() - (-0.5)).abs(); |
1223 | /// |
1224 | /// assert!(abs_difference_x < 1e-10); |
1225 | /// assert!(abs_difference_y < 1e-10); |
1226 | /// ``` |
1227 | fn fract(self) -> Self; |
1228 | |
1229 | /// Computes the absolute value of `self`. Returns `Float::nan()` if the |
1230 | /// number is `Float::nan()`. |
1231 | /// |
1232 | /// ``` |
1233 | /// use num_traits::Float; |
1234 | /// use std::f64; |
1235 | /// |
1236 | /// let x = 3.5; |
1237 | /// let y = -3.5; |
1238 | /// |
1239 | /// let abs_difference_x = (x.abs() - x).abs(); |
1240 | /// let abs_difference_y = (y.abs() - (-y)).abs(); |
1241 | /// |
1242 | /// assert!(abs_difference_x < 1e-10); |
1243 | /// assert!(abs_difference_y < 1e-10); |
1244 | /// |
1245 | /// assert!(f64::NAN.abs().is_nan()); |
1246 | /// ``` |
1247 | fn abs(self) -> Self; |
1248 | |
1249 | /// Returns a number that represents the sign of `self`. |
1250 | /// |
1251 | /// - `1.0` if the number is positive, `+0.0` or `Float::infinity()` |
1252 | /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity()` |
1253 | /// - `Float::nan()` if the number is `Float::nan()` |
1254 | /// |
1255 | /// ``` |
1256 | /// use num_traits::Float; |
1257 | /// use std::f64; |
1258 | /// |
1259 | /// let f = 3.5; |
1260 | /// |
1261 | /// assert_eq!(f.signum(), 1.0); |
1262 | /// assert_eq!(f64::NEG_INFINITY.signum(), -1.0); |
1263 | /// |
1264 | /// assert!(f64::NAN.signum().is_nan()); |
1265 | /// ``` |
1266 | fn signum(self) -> Self; |
1267 | |
1268 | /// Returns `true` if `self` is positive, including `+0.0`, |
1269 | /// `Float::infinity()`, and since Rust 1.20 also `Float::nan()`. |
1270 | /// |
1271 | /// ``` |
1272 | /// use num_traits::Float; |
1273 | /// use std::f64; |
1274 | /// |
1275 | /// let neg_nan: f64 = -f64::NAN; |
1276 | /// |
1277 | /// let f = 7.0; |
1278 | /// let g = -7.0; |
1279 | /// |
1280 | /// assert!(f.is_sign_positive()); |
1281 | /// assert!(!g.is_sign_positive()); |
1282 | /// assert!(!neg_nan.is_sign_positive()); |
1283 | /// ``` |
1284 | fn is_sign_positive(self) -> bool; |
1285 | |
1286 | /// Returns `true` if `self` is negative, including `-0.0`, |
1287 | /// `Float::neg_infinity()`, and since Rust 1.20 also `-Float::nan()`. |
1288 | /// |
1289 | /// ``` |
1290 | /// use num_traits::Float; |
1291 | /// use std::f64; |
1292 | /// |
1293 | /// let nan: f64 = f64::NAN; |
1294 | /// |
1295 | /// let f = 7.0; |
1296 | /// let g = -7.0; |
1297 | /// |
1298 | /// assert!(!f.is_sign_negative()); |
1299 | /// assert!(g.is_sign_negative()); |
1300 | /// assert!(!nan.is_sign_negative()); |
1301 | /// ``` |
1302 | fn is_sign_negative(self) -> bool; |
1303 | |
1304 | /// Fused multiply-add. Computes `(self * a) + b` with only one rounding |
1305 | /// error, yielding a more accurate result than an unfused multiply-add. |
1306 | /// |
1307 | /// Using `mul_add` can be more performant than an unfused multiply-add if |
1308 | /// the target architecture has a dedicated `fma` CPU instruction. |
1309 | /// |
1310 | /// ``` |
1311 | /// use num_traits::Float; |
1312 | /// |
1313 | /// let m = 10.0; |
1314 | /// let x = 4.0; |
1315 | /// let b = 60.0; |
1316 | /// |
1317 | /// // 100.0 |
1318 | /// let abs_difference = (m.mul_add(x, b) - (m*x + b)).abs(); |
1319 | /// |
1320 | /// assert!(abs_difference < 1e-10); |
1321 | /// ``` |
1322 | fn mul_add(self, a: Self, b: Self) -> Self; |
1323 | /// Take the reciprocal (inverse) of a number, `1/x`. |
1324 | /// |
1325 | /// ``` |
1326 | /// use num_traits::Float; |
1327 | /// |
1328 | /// let x = 2.0; |
1329 | /// let abs_difference = (x.recip() - (1.0/x)).abs(); |
1330 | /// |
1331 | /// assert!(abs_difference < 1e-10); |
1332 | /// ``` |
1333 | fn recip(self) -> Self; |
1334 | |
1335 | /// Raise a number to an integer power. |
1336 | /// |
1337 | /// Using this function is generally faster than using `powf` |
1338 | /// |
1339 | /// ``` |
1340 | /// use num_traits::Float; |
1341 | /// |
1342 | /// let x = 2.0; |
1343 | /// let abs_difference = (x.powi(2) - x*x).abs(); |
1344 | /// |
1345 | /// assert!(abs_difference < 1e-10); |
1346 | /// ``` |
1347 | fn powi(self, n: i32) -> Self; |
1348 | |
1349 | /// Raise a number to a floating point power. |
1350 | /// |
1351 | /// ``` |
1352 | /// use num_traits::Float; |
1353 | /// |
1354 | /// let x = 2.0; |
1355 | /// let abs_difference = (x.powf(2.0) - x*x).abs(); |
1356 | /// |
1357 | /// assert!(abs_difference < 1e-10); |
1358 | /// ``` |
1359 | fn powf(self, n: Self) -> Self; |
1360 | |
1361 | /// Take the square root of a number. |
1362 | /// |
1363 | /// Returns NaN if `self` is a negative number. |
1364 | /// |
1365 | /// ``` |
1366 | /// use num_traits::Float; |
1367 | /// |
1368 | /// let positive = 4.0; |
1369 | /// let negative = -4.0; |
1370 | /// |
1371 | /// let abs_difference = (positive.sqrt() - 2.0).abs(); |
1372 | /// |
1373 | /// assert!(abs_difference < 1e-10); |
1374 | /// assert!(negative.sqrt().is_nan()); |
1375 | /// ``` |
1376 | fn sqrt(self) -> Self; |
1377 | |
1378 | /// Returns `e^(self)`, (the exponential function). |
1379 | /// |
1380 | /// ``` |
1381 | /// use num_traits::Float; |
1382 | /// |
1383 | /// let one = 1.0; |
1384 | /// // e^1 |
1385 | /// let e = one.exp(); |
1386 | /// |
1387 | /// // ln(e) - 1 == 0 |
1388 | /// let abs_difference = (e.ln() - 1.0).abs(); |
1389 | /// |
1390 | /// assert!(abs_difference < 1e-10); |
1391 | /// ``` |
1392 | fn exp(self) -> Self; |
1393 | |
1394 | /// Returns `2^(self)`. |
1395 | /// |
1396 | /// ``` |
1397 | /// use num_traits::Float; |
1398 | /// |
1399 | /// let f = 2.0; |
1400 | /// |
1401 | /// // 2^2 - 4 == 0 |
1402 | /// let abs_difference = (f.exp2() - 4.0).abs(); |
1403 | /// |
1404 | /// assert!(abs_difference < 1e-10); |
1405 | /// ``` |
1406 | fn exp2(self) -> Self; |
1407 | |
1408 | /// Returns the natural logarithm of the number. |
1409 | /// |
1410 | /// ``` |
1411 | /// use num_traits::Float; |
1412 | /// |
1413 | /// let one = 1.0; |
1414 | /// // e^1 |
1415 | /// let e = one.exp(); |
1416 | /// |
1417 | /// // ln(e) - 1 == 0 |
1418 | /// let abs_difference = (e.ln() - 1.0).abs(); |
1419 | /// |
1420 | /// assert!(abs_difference < 1e-10); |
1421 | /// ``` |
1422 | fn ln(self) -> Self; |
1423 | |
1424 | /// Returns the logarithm of the number with respect to an arbitrary base. |
1425 | /// |
1426 | /// ``` |
1427 | /// use num_traits::Float; |
1428 | /// |
1429 | /// let ten = 10.0; |
1430 | /// let two = 2.0; |
1431 | /// |
1432 | /// // log10(10) - 1 == 0 |
1433 | /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs(); |
1434 | /// |
1435 | /// // log2(2) - 1 == 0 |
1436 | /// let abs_difference_2 = (two.log(2.0) - 1.0).abs(); |
1437 | /// |
1438 | /// assert!(abs_difference_10 < 1e-10); |
1439 | /// assert!(abs_difference_2 < 1e-10); |
1440 | /// ``` |
1441 | fn log(self, base: Self) -> Self; |
1442 | |
1443 | /// Returns the base 2 logarithm of the number. |
1444 | /// |
1445 | /// ``` |
1446 | /// use num_traits::Float; |
1447 | /// |
1448 | /// let two = 2.0; |
1449 | /// |
1450 | /// // log2(2) - 1 == 0 |
1451 | /// let abs_difference = (two.log2() - 1.0).abs(); |
1452 | /// |
1453 | /// assert!(abs_difference < 1e-10); |
1454 | /// ``` |
1455 | fn log2(self) -> Self; |
1456 | |
1457 | /// Returns the base 10 logarithm of the number. |
1458 | /// |
1459 | /// ``` |
1460 | /// use num_traits::Float; |
1461 | /// |
1462 | /// let ten = 10.0; |
1463 | /// |
1464 | /// // log10(10) - 1 == 0 |
1465 | /// let abs_difference = (ten.log10() - 1.0).abs(); |
1466 | /// |
1467 | /// assert!(abs_difference < 1e-10); |
1468 | /// ``` |
1469 | fn log10(self) -> Self; |
1470 | |
1471 | /// Converts radians to degrees. |
1472 | /// |
1473 | /// ``` |
1474 | /// use std::f64::consts; |
1475 | /// |
1476 | /// let angle = consts::PI; |
1477 | /// |
1478 | /// let abs_difference = (angle.to_degrees() - 180.0).abs(); |
1479 | /// |
1480 | /// assert!(abs_difference < 1e-10); |
1481 | /// ``` |
1482 | #[inline ] |
1483 | fn to_degrees(self) -> Self { |
1484 | let halfpi = Self::zero().acos(); |
1485 | let ninety = Self::from(90u8).unwrap(); |
1486 | self * ninety / halfpi |
1487 | } |
1488 | |
1489 | /// Converts degrees to radians. |
1490 | /// |
1491 | /// ``` |
1492 | /// use std::f64::consts; |
1493 | /// |
1494 | /// let angle = 180.0_f64; |
1495 | /// |
1496 | /// let abs_difference = (angle.to_radians() - consts::PI).abs(); |
1497 | /// |
1498 | /// assert!(abs_difference < 1e-10); |
1499 | /// ``` |
1500 | #[inline ] |
1501 | fn to_radians(self) -> Self { |
1502 | let halfpi = Self::zero().acos(); |
1503 | let ninety = Self::from(90u8).unwrap(); |
1504 | self * halfpi / ninety |
1505 | } |
1506 | |
1507 | /// Returns the maximum of the two numbers. |
1508 | /// |
1509 | /// ``` |
1510 | /// use num_traits::Float; |
1511 | /// |
1512 | /// let x = 1.0; |
1513 | /// let y = 2.0; |
1514 | /// |
1515 | /// assert_eq!(x.max(y), y); |
1516 | /// ``` |
1517 | fn max(self, other: Self) -> Self; |
1518 | |
1519 | /// Returns the minimum of the two numbers. |
1520 | /// |
1521 | /// ``` |
1522 | /// use num_traits::Float; |
1523 | /// |
1524 | /// let x = 1.0; |
1525 | /// let y = 2.0; |
1526 | /// |
1527 | /// assert_eq!(x.min(y), x); |
1528 | /// ``` |
1529 | fn min(self, other: Self) -> Self; |
1530 | |
1531 | /// The positive difference of two numbers. |
1532 | /// |
1533 | /// * If `self <= other`: `0:0` |
1534 | /// * Else: `self - other` |
1535 | /// |
1536 | /// ``` |
1537 | /// use num_traits::Float; |
1538 | /// |
1539 | /// let x = 3.0; |
1540 | /// let y = -3.0; |
1541 | /// |
1542 | /// let abs_difference_x = (x.abs_sub(1.0) - 2.0).abs(); |
1543 | /// let abs_difference_y = (y.abs_sub(1.0) - 0.0).abs(); |
1544 | /// |
1545 | /// assert!(abs_difference_x < 1e-10); |
1546 | /// assert!(abs_difference_y < 1e-10); |
1547 | /// ``` |
1548 | fn abs_sub(self, other: Self) -> Self; |
1549 | |
1550 | /// Take the cubic root of a number. |
1551 | /// |
1552 | /// ``` |
1553 | /// use num_traits::Float; |
1554 | /// |
1555 | /// let x = 8.0; |
1556 | /// |
1557 | /// // x^(1/3) - 2 == 0 |
1558 | /// let abs_difference = (x.cbrt() - 2.0).abs(); |
1559 | /// |
1560 | /// assert!(abs_difference < 1e-10); |
1561 | /// ``` |
1562 | fn cbrt(self) -> Self; |
1563 | |
1564 | /// Calculate the length of the hypotenuse of a right-angle triangle given |
1565 | /// legs of length `x` and `y`. |
1566 | /// |
1567 | /// ``` |
1568 | /// use num_traits::Float; |
1569 | /// |
1570 | /// let x = 2.0; |
1571 | /// let y = 3.0; |
1572 | /// |
1573 | /// // sqrt(x^2 + y^2) |
1574 | /// let abs_difference = (x.hypot(y) - (x.powi(2) + y.powi(2)).sqrt()).abs(); |
1575 | /// |
1576 | /// assert!(abs_difference < 1e-10); |
1577 | /// ``` |
1578 | fn hypot(self, other: Self) -> Self; |
1579 | |
1580 | /// Computes the sine of a number (in radians). |
1581 | /// |
1582 | /// ``` |
1583 | /// use num_traits::Float; |
1584 | /// use std::f64; |
1585 | /// |
1586 | /// let x = f64::consts::PI/2.0; |
1587 | /// |
1588 | /// let abs_difference = (x.sin() - 1.0).abs(); |
1589 | /// |
1590 | /// assert!(abs_difference < 1e-10); |
1591 | /// ``` |
1592 | fn sin(self) -> Self; |
1593 | |
1594 | /// Computes the cosine of a number (in radians). |
1595 | /// |
1596 | /// ``` |
1597 | /// use num_traits::Float; |
1598 | /// use std::f64; |
1599 | /// |
1600 | /// let x = 2.0*f64::consts::PI; |
1601 | /// |
1602 | /// let abs_difference = (x.cos() - 1.0).abs(); |
1603 | /// |
1604 | /// assert!(abs_difference < 1e-10); |
1605 | /// ``` |
1606 | fn cos(self) -> Self; |
1607 | |
1608 | /// Computes the tangent of a number (in radians). |
1609 | /// |
1610 | /// ``` |
1611 | /// use num_traits::Float; |
1612 | /// use std::f64; |
1613 | /// |
1614 | /// let x = f64::consts::PI/4.0; |
1615 | /// let abs_difference = (x.tan() - 1.0).abs(); |
1616 | /// |
1617 | /// assert!(abs_difference < 1e-14); |
1618 | /// ``` |
1619 | fn tan(self) -> Self; |
1620 | |
1621 | /// Computes the arcsine of a number. Return value is in radians in |
1622 | /// the range [-pi/2, pi/2] or NaN if the number is outside the range |
1623 | /// [-1, 1]. |
1624 | /// |
1625 | /// ``` |
1626 | /// use num_traits::Float; |
1627 | /// use std::f64; |
1628 | /// |
1629 | /// let f = f64::consts::PI / 2.0; |
1630 | /// |
1631 | /// // asin(sin(pi/2)) |
1632 | /// let abs_difference = (f.sin().asin() - f64::consts::PI / 2.0).abs(); |
1633 | /// |
1634 | /// assert!(abs_difference < 1e-10); |
1635 | /// ``` |
1636 | fn asin(self) -> Self; |
1637 | |
1638 | /// Computes the arccosine of a number. Return value is in radians in |
1639 | /// the range [0, pi] or NaN if the number is outside the range |
1640 | /// [-1, 1]. |
1641 | /// |
1642 | /// ``` |
1643 | /// use num_traits::Float; |
1644 | /// use std::f64; |
1645 | /// |
1646 | /// let f = f64::consts::PI / 4.0; |
1647 | /// |
1648 | /// // acos(cos(pi/4)) |
1649 | /// let abs_difference = (f.cos().acos() - f64::consts::PI / 4.0).abs(); |
1650 | /// |
1651 | /// assert!(abs_difference < 1e-10); |
1652 | /// ``` |
1653 | fn acos(self) -> Self; |
1654 | |
1655 | /// Computes the arctangent of a number. Return value is in radians in the |
1656 | /// range [-pi/2, pi/2]; |
1657 | /// |
1658 | /// ``` |
1659 | /// use num_traits::Float; |
1660 | /// |
1661 | /// let f = 1.0; |
1662 | /// |
1663 | /// // atan(tan(1)) |
1664 | /// let abs_difference = (f.tan().atan() - 1.0).abs(); |
1665 | /// |
1666 | /// assert!(abs_difference < 1e-10); |
1667 | /// ``` |
1668 | fn atan(self) -> Self; |
1669 | |
1670 | /// Computes the four quadrant arctangent of `self` (`y`) and `other` (`x`). |
1671 | /// |
1672 | /// * `x = 0`, `y = 0`: `0` |
1673 | /// * `x >= 0`: `arctan(y/x)` -> `[-pi/2, pi/2]` |
1674 | /// * `y >= 0`: `arctan(y/x) + pi` -> `(pi/2, pi]` |
1675 | /// * `y < 0`: `arctan(y/x) - pi` -> `(-pi, -pi/2)` |
1676 | /// |
1677 | /// ``` |
1678 | /// use num_traits::Float; |
1679 | /// use std::f64; |
1680 | /// |
1681 | /// let pi = f64::consts::PI; |
1682 | /// // All angles from horizontal right (+x) |
1683 | /// // 45 deg counter-clockwise |
1684 | /// let x1 = 3.0; |
1685 | /// let y1 = -3.0; |
1686 | /// |
1687 | /// // 135 deg clockwise |
1688 | /// let x2 = -3.0; |
1689 | /// let y2 = 3.0; |
1690 | /// |
1691 | /// let abs_difference_1 = (y1.atan2(x1) - (-pi/4.0)).abs(); |
1692 | /// let abs_difference_2 = (y2.atan2(x2) - 3.0*pi/4.0).abs(); |
1693 | /// |
1694 | /// assert!(abs_difference_1 < 1e-10); |
1695 | /// assert!(abs_difference_2 < 1e-10); |
1696 | /// ``` |
1697 | fn atan2(self, other: Self) -> Self; |
1698 | |
1699 | /// Simultaneously computes the sine and cosine of the number, `x`. Returns |
1700 | /// `(sin(x), cos(x))`. |
1701 | /// |
1702 | /// ``` |
1703 | /// use num_traits::Float; |
1704 | /// use std::f64; |
1705 | /// |
1706 | /// let x = f64::consts::PI/4.0; |
1707 | /// let f = x.sin_cos(); |
1708 | /// |
1709 | /// let abs_difference_0 = (f.0 - x.sin()).abs(); |
1710 | /// let abs_difference_1 = (f.1 - x.cos()).abs(); |
1711 | /// |
1712 | /// assert!(abs_difference_0 < 1e-10); |
1713 | /// assert!(abs_difference_0 < 1e-10); |
1714 | /// ``` |
1715 | fn sin_cos(self) -> (Self, Self); |
1716 | |
1717 | /// Returns `e^(self) - 1` in a way that is accurate even if the |
1718 | /// number is close to zero. |
1719 | /// |
1720 | /// ``` |
1721 | /// use num_traits::Float; |
1722 | /// |
1723 | /// let x = 7.0; |
1724 | /// |
1725 | /// // e^(ln(7)) - 1 |
1726 | /// let abs_difference = (x.ln().exp_m1() - 6.0).abs(); |
1727 | /// |
1728 | /// assert!(abs_difference < 1e-10); |
1729 | /// ``` |
1730 | fn exp_m1(self) -> Self; |
1731 | |
1732 | /// Returns `ln(1+n)` (natural logarithm) more accurately than if |
1733 | /// the operations were performed separately. |
1734 | /// |
1735 | /// ``` |
1736 | /// use num_traits::Float; |
1737 | /// use std::f64; |
1738 | /// |
1739 | /// let x = f64::consts::E - 1.0; |
1740 | /// |
1741 | /// // ln(1 + (e - 1)) == ln(e) == 1 |
1742 | /// let abs_difference = (x.ln_1p() - 1.0).abs(); |
1743 | /// |
1744 | /// assert!(abs_difference < 1e-10); |
1745 | /// ``` |
1746 | fn ln_1p(self) -> Self; |
1747 | |
1748 | /// Hyperbolic sine function. |
1749 | /// |
1750 | /// ``` |
1751 | /// use num_traits::Float; |
1752 | /// use std::f64; |
1753 | /// |
1754 | /// let e = f64::consts::E; |
1755 | /// let x = 1.0; |
1756 | /// |
1757 | /// let f = x.sinh(); |
1758 | /// // Solving sinh() at 1 gives `(e^2-1)/(2e)` |
1759 | /// let g = (e*e - 1.0)/(2.0*e); |
1760 | /// let abs_difference = (f - g).abs(); |
1761 | /// |
1762 | /// assert!(abs_difference < 1e-10); |
1763 | /// ``` |
1764 | fn sinh(self) -> Self; |
1765 | |
1766 | /// Hyperbolic cosine function. |
1767 | /// |
1768 | /// ``` |
1769 | /// use num_traits::Float; |
1770 | /// use std::f64; |
1771 | /// |
1772 | /// let e = f64::consts::E; |
1773 | /// let x = 1.0; |
1774 | /// let f = x.cosh(); |
1775 | /// // Solving cosh() at 1 gives this result |
1776 | /// let g = (e*e + 1.0)/(2.0*e); |
1777 | /// let abs_difference = (f - g).abs(); |
1778 | /// |
1779 | /// // Same result |
1780 | /// assert!(abs_difference < 1.0e-10); |
1781 | /// ``` |
1782 | fn cosh(self) -> Self; |
1783 | |
1784 | /// Hyperbolic tangent function. |
1785 | /// |
1786 | /// ``` |
1787 | /// use num_traits::Float; |
1788 | /// use std::f64; |
1789 | /// |
1790 | /// let e = f64::consts::E; |
1791 | /// let x = 1.0; |
1792 | /// |
1793 | /// let f = x.tanh(); |
1794 | /// // Solving tanh() at 1 gives `(1 - e^(-2))/(1 + e^(-2))` |
1795 | /// let g = (1.0 - e.powi(-2))/(1.0 + e.powi(-2)); |
1796 | /// let abs_difference = (f - g).abs(); |
1797 | /// |
1798 | /// assert!(abs_difference < 1.0e-10); |
1799 | /// ``` |
1800 | fn tanh(self) -> Self; |
1801 | |
1802 | /// Inverse hyperbolic sine function. |
1803 | /// |
1804 | /// ``` |
1805 | /// use num_traits::Float; |
1806 | /// |
1807 | /// let x = 1.0; |
1808 | /// let f = x.sinh().asinh(); |
1809 | /// |
1810 | /// let abs_difference = (f - x).abs(); |
1811 | /// |
1812 | /// assert!(abs_difference < 1.0e-10); |
1813 | /// ``` |
1814 | fn asinh(self) -> Self; |
1815 | |
1816 | /// Inverse hyperbolic cosine function. |
1817 | /// |
1818 | /// ``` |
1819 | /// use num_traits::Float; |
1820 | /// |
1821 | /// let x = 1.0; |
1822 | /// let f = x.cosh().acosh(); |
1823 | /// |
1824 | /// let abs_difference = (f - x).abs(); |
1825 | /// |
1826 | /// assert!(abs_difference < 1.0e-10); |
1827 | /// ``` |
1828 | fn acosh(self) -> Self; |
1829 | |
1830 | /// Inverse hyperbolic tangent function. |
1831 | /// |
1832 | /// ``` |
1833 | /// use num_traits::Float; |
1834 | /// use std::f64; |
1835 | /// |
1836 | /// let e = f64::consts::E; |
1837 | /// let f = e.tanh().atanh(); |
1838 | /// |
1839 | /// let abs_difference = (f - e).abs(); |
1840 | /// |
1841 | /// assert!(abs_difference < 1.0e-10); |
1842 | /// ``` |
1843 | fn atanh(self) -> Self; |
1844 | |
1845 | /// Returns the mantissa, base 2 exponent, and sign as integers, respectively. |
1846 | /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. |
1847 | /// |
1848 | /// ``` |
1849 | /// use num_traits::Float; |
1850 | /// |
1851 | /// let num = 2.0f32; |
1852 | /// |
1853 | /// // (8388608, -22, 1) |
1854 | /// let (mantissa, exponent, sign) = Float::integer_decode(num); |
1855 | /// let sign_f = sign as f32; |
1856 | /// let mantissa_f = mantissa as f32; |
1857 | /// let exponent_f = num.powf(exponent as f32); |
1858 | /// |
1859 | /// // 1 * 8388608 * 2^(-22) == 2 |
1860 | /// let abs_difference = (sign_f * mantissa_f * exponent_f - num).abs(); |
1861 | /// |
1862 | /// assert!(abs_difference < 1e-10); |
1863 | /// ``` |
1864 | fn integer_decode(self) -> (u64, i16, i8); |
1865 | |
1866 | /// Returns a number composed of the magnitude of `self` and the sign of |
1867 | /// `sign`. |
1868 | /// |
1869 | /// Equal to `self` if the sign of `self` and `sign` are the same, otherwise |
1870 | /// equal to `-self`. If `self` is a `NAN`, then a `NAN` with the sign of |
1871 | /// `sign` is returned. |
1872 | /// |
1873 | /// # Examples |
1874 | /// |
1875 | /// ``` |
1876 | /// use num_traits::Float; |
1877 | /// |
1878 | /// let f = 3.5_f32; |
1879 | /// |
1880 | /// assert_eq!(f.copysign(0.42), 3.5_f32); |
1881 | /// assert_eq!(f.copysign(-0.42), -3.5_f32); |
1882 | /// assert_eq!((-f).copysign(0.42), 3.5_f32); |
1883 | /// assert_eq!((-f).copysign(-0.42), -3.5_f32); |
1884 | /// |
1885 | /// assert!(f32::nan().copysign(1.0).is_nan()); |
1886 | /// ``` |
1887 | fn copysign(self, sign: Self) -> Self { |
1888 | if self.is_sign_negative() == sign.is_sign_negative() { |
1889 | self |
1890 | } else { |
1891 | self.neg() |
1892 | } |
1893 | } |
1894 | } |
1895 | |
1896 | #[cfg (feature = "std" )] |
1897 | macro_rules! float_impl_std { |
1898 | ($T:ident $decode:ident) => { |
1899 | impl Float for $T { |
1900 | constant! { |
1901 | nan() -> $T::NAN; |
1902 | infinity() -> $T::INFINITY; |
1903 | neg_infinity() -> $T::NEG_INFINITY; |
1904 | neg_zero() -> -0.0; |
1905 | min_value() -> $T::MIN; |
1906 | min_positive_value() -> $T::MIN_POSITIVE; |
1907 | epsilon() -> $T::EPSILON; |
1908 | max_value() -> $T::MAX; |
1909 | } |
1910 | |
1911 | #[inline] |
1912 | #[allow(deprecated)] |
1913 | fn abs_sub(self, other: Self) -> Self { |
1914 | <$T>::abs_sub(self, other) |
1915 | } |
1916 | |
1917 | #[inline] |
1918 | fn integer_decode(self) -> (u64, i16, i8) { |
1919 | $decode(self) |
1920 | } |
1921 | |
1922 | forward! { |
1923 | Self::is_nan(self) -> bool; |
1924 | Self::is_infinite(self) -> bool; |
1925 | Self::is_finite(self) -> bool; |
1926 | Self::is_normal(self) -> bool; |
1927 | Self::classify(self) -> FpCategory; |
1928 | Self::floor(self) -> Self; |
1929 | Self::ceil(self) -> Self; |
1930 | Self::round(self) -> Self; |
1931 | Self::trunc(self) -> Self; |
1932 | Self::fract(self) -> Self; |
1933 | Self::abs(self) -> Self; |
1934 | Self::signum(self) -> Self; |
1935 | Self::is_sign_positive(self) -> bool; |
1936 | Self::is_sign_negative(self) -> bool; |
1937 | Self::mul_add(self, a: Self, b: Self) -> Self; |
1938 | Self::recip(self) -> Self; |
1939 | Self::powi(self, n: i32) -> Self; |
1940 | Self::powf(self, n: Self) -> Self; |
1941 | Self::sqrt(self) -> Self; |
1942 | Self::exp(self) -> Self; |
1943 | Self::exp2(self) -> Self; |
1944 | Self::ln(self) -> Self; |
1945 | Self::log(self, base: Self) -> Self; |
1946 | Self::log2(self) -> Self; |
1947 | Self::log10(self) -> Self; |
1948 | Self::to_degrees(self) -> Self; |
1949 | Self::to_radians(self) -> Self; |
1950 | Self::max(self, other: Self) -> Self; |
1951 | Self::min(self, other: Self) -> Self; |
1952 | Self::cbrt(self) -> Self; |
1953 | Self::hypot(self, other: Self) -> Self; |
1954 | Self::sin(self) -> Self; |
1955 | Self::cos(self) -> Self; |
1956 | Self::tan(self) -> Self; |
1957 | Self::asin(self) -> Self; |
1958 | Self::acos(self) -> Self; |
1959 | Self::atan(self) -> Self; |
1960 | Self::atan2(self, other: Self) -> Self; |
1961 | Self::sin_cos(self) -> (Self, Self); |
1962 | Self::exp_m1(self) -> Self; |
1963 | Self::ln_1p(self) -> Self; |
1964 | Self::sinh(self) -> Self; |
1965 | Self::cosh(self) -> Self; |
1966 | Self::tanh(self) -> Self; |
1967 | Self::asinh(self) -> Self; |
1968 | Self::acosh(self) -> Self; |
1969 | Self::atanh(self) -> Self; |
1970 | } |
1971 | |
1972 | #[cfg(has_copysign)] |
1973 | #[inline] |
1974 | fn copysign(self, sign: Self) -> Self { |
1975 | Self::copysign(self, sign) |
1976 | } |
1977 | } |
1978 | }; |
1979 | } |
1980 | |
1981 | #[cfg (all(not(feature = "std" ), feature = "libm" ))] |
1982 | macro_rules! float_impl_libm { |
1983 | ($T:ident $decode:ident) => { |
1984 | constant! { |
1985 | nan() -> $T::NAN; |
1986 | infinity() -> $T::INFINITY; |
1987 | neg_infinity() -> $T::NEG_INFINITY; |
1988 | neg_zero() -> -0.0; |
1989 | min_value() -> $T::MIN; |
1990 | min_positive_value() -> $T::MIN_POSITIVE; |
1991 | epsilon() -> $T::EPSILON; |
1992 | max_value() -> $T::MAX; |
1993 | } |
1994 | |
1995 | #[inline] |
1996 | fn integer_decode(self) -> (u64, i16, i8) { |
1997 | $decode(self) |
1998 | } |
1999 | |
2000 | #[inline] |
2001 | fn fract(self) -> Self { |
2002 | self - Float::trunc(self) |
2003 | } |
2004 | |
2005 | #[inline] |
2006 | fn log(self, base: Self) -> Self { |
2007 | self.ln() / base.ln() |
2008 | } |
2009 | |
2010 | forward! { |
2011 | FloatCore::is_nan(self) -> bool; |
2012 | FloatCore::is_infinite(self) -> bool; |
2013 | FloatCore::is_finite(self) -> bool; |
2014 | FloatCore::is_normal(self) -> bool; |
2015 | FloatCore::classify(self) -> FpCategory; |
2016 | FloatCore::signum(self) -> Self; |
2017 | FloatCore::is_sign_positive(self) -> bool; |
2018 | FloatCore::is_sign_negative(self) -> bool; |
2019 | FloatCore::recip(self) -> Self; |
2020 | FloatCore::powi(self, n: i32) -> Self; |
2021 | FloatCore::to_degrees(self) -> Self; |
2022 | FloatCore::to_radians(self) -> Self; |
2023 | } |
2024 | }; |
2025 | } |
2026 | |
2027 | fn integer_decode_f32(f: f32) -> (u64, i16, i8) { |
2028 | // Safety: this identical to the implementation of f32::to_bits(), |
2029 | // which is only available starting at Rust 1.20 |
2030 | let bits: u32 = unsafe { mem::transmute(src:f) }; |
2031 | let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 }; |
2032 | let mut exponent: i16 = ((bits >> 23) & 0xff) as i16; |
2033 | let mantissa: u32 = if exponent == 0 { |
2034 | (bits & 0x7fffff) << 1 |
2035 | } else { |
2036 | (bits & 0x7fffff) | 0x800000 |
2037 | }; |
2038 | // Exponent bias + mantissa shift |
2039 | exponent -= 127 + 23; |
2040 | (mantissa as u64, exponent, sign) |
2041 | } |
2042 | |
2043 | fn integer_decode_f64(f: f64) -> (u64, i16, i8) { |
2044 | // Safety: this identical to the implementation of f64::to_bits(), |
2045 | // which is only available starting at Rust 1.20 |
2046 | let bits: u64 = unsafe { mem::transmute(src:f) }; |
2047 | let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 }; |
2048 | let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16; |
2049 | let mantissa: u64 = if exponent == 0 { |
2050 | (bits & 0xfffffffffffff) << 1 |
2051 | } else { |
2052 | (bits & 0xfffffffffffff) | 0x10000000000000 |
2053 | }; |
2054 | // Exponent bias + mantissa shift |
2055 | exponent -= 1023 + 52; |
2056 | (mantissa, exponent, sign) |
2057 | } |
2058 | |
2059 | #[cfg (feature = "std" )] |
2060 | float_impl_std!(f32 integer_decode_f32); |
2061 | #[cfg (feature = "std" )] |
2062 | float_impl_std!(f64 integer_decode_f64); |
2063 | |
2064 | #[cfg (all(not(feature = "std" ), feature = "libm" ))] |
2065 | impl Float for f32 { |
2066 | float_impl_libm!(f32 integer_decode_f32); |
2067 | |
2068 | #[inline ] |
2069 | #[allow (deprecated)] |
2070 | fn abs_sub(self, other: Self) -> Self { |
2071 | libm::fdimf(self, other) |
2072 | } |
2073 | |
2074 | forward! { |
2075 | libm::floorf as floor(self) -> Self; |
2076 | libm::ceilf as ceil(self) -> Self; |
2077 | libm::roundf as round(self) -> Self; |
2078 | libm::truncf as trunc(self) -> Self; |
2079 | libm::fabsf as abs(self) -> Self; |
2080 | libm::fmaf as mul_add(self, a: Self, b: Self) -> Self; |
2081 | libm::powf as powf(self, n: Self) -> Self; |
2082 | libm::sqrtf as sqrt(self) -> Self; |
2083 | libm::expf as exp(self) -> Self; |
2084 | libm::exp2f as exp2(self) -> Self; |
2085 | libm::logf as ln(self) -> Self; |
2086 | libm::log2f as log2(self) -> Self; |
2087 | libm::log10f as log10(self) -> Self; |
2088 | libm::cbrtf as cbrt(self) -> Self; |
2089 | libm::hypotf as hypot(self, other: Self) -> Self; |
2090 | libm::sinf as sin(self) -> Self; |
2091 | libm::cosf as cos(self) -> Self; |
2092 | libm::tanf as tan(self) -> Self; |
2093 | libm::asinf as asin(self) -> Self; |
2094 | libm::acosf as acos(self) -> Self; |
2095 | libm::atanf as atan(self) -> Self; |
2096 | libm::atan2f as atan2(self, other: Self) -> Self; |
2097 | libm::sincosf as sin_cos(self) -> (Self, Self); |
2098 | libm::expm1f as exp_m1(self) -> Self; |
2099 | libm::log1pf as ln_1p(self) -> Self; |
2100 | libm::sinhf as sinh(self) -> Self; |
2101 | libm::coshf as cosh(self) -> Self; |
2102 | libm::tanhf as tanh(self) -> Self; |
2103 | libm::asinhf as asinh(self) -> Self; |
2104 | libm::acoshf as acosh(self) -> Self; |
2105 | libm::atanhf as atanh(self) -> Self; |
2106 | libm::fmaxf as max(self, other: Self) -> Self; |
2107 | libm::fminf as min(self, other: Self) -> Self; |
2108 | libm::copysignf as copysign(self, other: Self) -> Self; |
2109 | } |
2110 | } |
2111 | |
2112 | #[cfg (all(not(feature = "std" ), feature = "libm" ))] |
2113 | impl Float for f64 { |
2114 | float_impl_libm!(f64 integer_decode_f64); |
2115 | |
2116 | #[inline ] |
2117 | #[allow (deprecated)] |
2118 | fn abs_sub(self, other: Self) -> Self { |
2119 | libm::fdim(self, other) |
2120 | } |
2121 | |
2122 | forward! { |
2123 | libm::floor as floor(self) -> Self; |
2124 | libm::ceil as ceil(self) -> Self; |
2125 | libm::round as round(self) -> Self; |
2126 | libm::trunc as trunc(self) -> Self; |
2127 | libm::fabs as abs(self) -> Self; |
2128 | libm::fma as mul_add(self, a: Self, b: Self) -> Self; |
2129 | libm::pow as powf(self, n: Self) -> Self; |
2130 | libm::sqrt as sqrt(self) -> Self; |
2131 | libm::exp as exp(self) -> Self; |
2132 | libm::exp2 as exp2(self) -> Self; |
2133 | libm::log as ln(self) -> Self; |
2134 | libm::log2 as log2(self) -> Self; |
2135 | libm::log10 as log10(self) -> Self; |
2136 | libm::cbrt as cbrt(self) -> Self; |
2137 | libm::hypot as hypot(self, other: Self) -> Self; |
2138 | libm::sin as sin(self) -> Self; |
2139 | libm::cos as cos(self) -> Self; |
2140 | libm::tan as tan(self) -> Self; |
2141 | libm::asin as asin(self) -> Self; |
2142 | libm::acos as acos(self) -> Self; |
2143 | libm::atan as atan(self) -> Self; |
2144 | libm::atan2 as atan2(self, other: Self) -> Self; |
2145 | libm::sincos as sin_cos(self) -> (Self, Self); |
2146 | libm::expm1 as exp_m1(self) -> Self; |
2147 | libm::log1p as ln_1p(self) -> Self; |
2148 | libm::sinh as sinh(self) -> Self; |
2149 | libm::cosh as cosh(self) -> Self; |
2150 | libm::tanh as tanh(self) -> Self; |
2151 | libm::asinh as asinh(self) -> Self; |
2152 | libm::acosh as acosh(self) -> Self; |
2153 | libm::atanh as atanh(self) -> Self; |
2154 | libm::fmax as max(self, other: Self) -> Self; |
2155 | libm::fmin as min(self, other: Self) -> Self; |
2156 | libm::copysign as copysign(self, sign: Self) -> Self; |
2157 | } |
2158 | } |
2159 | |
2160 | macro_rules! float_const_impl { |
2161 | ($(#[$doc:meta] $constant:ident,)+) => ( |
2162 | #[allow(non_snake_case)] |
2163 | pub trait FloatConst { |
2164 | $(#[$doc] fn $constant() -> Self;)+ |
2165 | #[doc = "Return the full circle constant `τ`." ] |
2166 | #[inline] |
2167 | fn TAU() -> Self where Self: Sized + Add<Self, Output = Self> { |
2168 | Self::PI() + Self::PI() |
2169 | } |
2170 | #[doc = "Return `log10(2.0)`." ] |
2171 | #[inline] |
2172 | fn LOG10_2() -> Self where Self: Sized + Div<Self, Output = Self> { |
2173 | Self::LN_2() / Self::LN_10() |
2174 | } |
2175 | #[doc = "Return `log2(10.0)`." ] |
2176 | #[inline] |
2177 | fn LOG2_10() -> Self where Self: Sized + Div<Self, Output = Self> { |
2178 | Self::LN_10() / Self::LN_2() |
2179 | } |
2180 | } |
2181 | float_const_impl! { @float f32, $($constant,)+ } |
2182 | float_const_impl! { @float f64, $($constant,)+ } |
2183 | ); |
2184 | (@float $T:ident, $($constant:ident,)+) => ( |
2185 | impl FloatConst for $T { |
2186 | constant! { |
2187 | $( $constant() -> $T::consts::$constant; )+ |
2188 | TAU() -> 6.28318530717958647692528676655900577; |
2189 | LOG10_2() -> 0.301029995663981195213738894724493027; |
2190 | LOG2_10() -> 3.32192809488736234787031942948939018; |
2191 | } |
2192 | } |
2193 | ); |
2194 | } |
2195 | |
2196 | float_const_impl! { |
2197 | #[doc = "Return Euler’s number." ] |
2198 | E, |
2199 | #[doc = "Return `1.0 / π`." ] |
2200 | FRAC_1_PI, |
2201 | #[doc = "Return `1.0 / sqrt(2.0)`." ] |
2202 | FRAC_1_SQRT_2, |
2203 | #[doc = "Return `2.0 / π`." ] |
2204 | FRAC_2_PI, |
2205 | #[doc = "Return `2.0 / sqrt(π)`." ] |
2206 | FRAC_2_SQRT_PI, |
2207 | #[doc = "Return `π / 2.0`." ] |
2208 | FRAC_PI_2, |
2209 | #[doc = "Return `π / 3.0`." ] |
2210 | FRAC_PI_3, |
2211 | #[doc = "Return `π / 4.0`." ] |
2212 | FRAC_PI_4, |
2213 | #[doc = "Return `π / 6.0`." ] |
2214 | FRAC_PI_6, |
2215 | #[doc = "Return `π / 8.0`." ] |
2216 | FRAC_PI_8, |
2217 | #[doc = "Return `ln(10.0)`." ] |
2218 | LN_10, |
2219 | #[doc = "Return `ln(2.0)`." ] |
2220 | LN_2, |
2221 | #[doc = "Return `log10(e)`." ] |
2222 | LOG10_E, |
2223 | #[doc = "Return `log2(e)`." ] |
2224 | LOG2_E, |
2225 | #[doc = "Return Archimedes’ constant `π`." ] |
2226 | PI, |
2227 | #[doc = "Return `sqrt(2.0)`." ] |
2228 | SQRT_2, |
2229 | } |
2230 | |
2231 | #[cfg (test)] |
2232 | mod tests { |
2233 | use core::f64::consts; |
2234 | |
2235 | const DEG_RAD_PAIRS: [(f64, f64); 7] = [ |
2236 | (0.0, 0.), |
2237 | (22.5, consts::FRAC_PI_8), |
2238 | (30.0, consts::FRAC_PI_6), |
2239 | (45.0, consts::FRAC_PI_4), |
2240 | (60.0, consts::FRAC_PI_3), |
2241 | (90.0, consts::FRAC_PI_2), |
2242 | (180.0, consts::PI), |
2243 | ]; |
2244 | |
2245 | #[test ] |
2246 | fn convert_deg_rad() { |
2247 | use float::FloatCore; |
2248 | |
2249 | for &(deg, rad) in &DEG_RAD_PAIRS { |
2250 | assert!((FloatCore::to_degrees(rad) - deg).abs() < 1e-6); |
2251 | assert!((FloatCore::to_radians(deg) - rad).abs() < 1e-6); |
2252 | |
2253 | let (deg, rad) = (deg as f32, rad as f32); |
2254 | assert!((FloatCore::to_degrees(rad) - deg).abs() < 1e-5); |
2255 | assert!((FloatCore::to_radians(deg) - rad).abs() < 1e-5); |
2256 | } |
2257 | } |
2258 | |
2259 | #[cfg (any(feature = "std" , feature = "libm" ))] |
2260 | #[test ] |
2261 | fn convert_deg_rad_std() { |
2262 | for &(deg, rad) in &DEG_RAD_PAIRS { |
2263 | use Float; |
2264 | |
2265 | assert!((Float::to_degrees(rad) - deg).abs() < 1e-6); |
2266 | assert!((Float::to_radians(deg) - rad).abs() < 1e-6); |
2267 | |
2268 | let (deg, rad) = (deg as f32, rad as f32); |
2269 | assert!((Float::to_degrees(rad) - deg).abs() < 1e-5); |
2270 | assert!((Float::to_radians(deg) - rad).abs() < 1e-5); |
2271 | } |
2272 | } |
2273 | |
2274 | #[test ] |
2275 | // This fails with the forwarded `std` implementation in Rust 1.8. |
2276 | // To avoid the failure, the test is limited to `no_std` builds. |
2277 | #[cfg (not(feature = "std" ))] |
2278 | fn to_degrees_rounding() { |
2279 | use float::FloatCore; |
2280 | |
2281 | assert_eq!( |
2282 | FloatCore::to_degrees(1_f32), |
2283 | 57.2957795130823208767981548141051703 |
2284 | ); |
2285 | } |
2286 | |
2287 | #[test ] |
2288 | #[cfg (any(feature = "std" , feature = "libm" ))] |
2289 | fn extra_logs() { |
2290 | use float::{Float, FloatConst}; |
2291 | |
2292 | fn check<F: Float + FloatConst>(diff: F) { |
2293 | let _2 = F::from(2.0).unwrap(); |
2294 | assert!((F::LOG10_2() - F::log10(_2)).abs() < diff); |
2295 | assert!((F::LOG10_2() - F::LN_2() / F::LN_10()).abs() < diff); |
2296 | |
2297 | let _10 = F::from(10.0).unwrap(); |
2298 | assert!((F::LOG2_10() - F::log2(_10)).abs() < diff); |
2299 | assert!((F::LOG2_10() - F::LN_10() / F::LN_2()).abs() < diff); |
2300 | } |
2301 | |
2302 | check::<f32>(1e-6); |
2303 | check::<f64>(1e-12); |
2304 | } |
2305 | |
2306 | #[test ] |
2307 | #[cfg (any(feature = "std" , feature = "libm" ))] |
2308 | fn copysign() { |
2309 | use float::Float; |
2310 | test_copysign_generic(2.0_f32, -2.0_f32, f32::nan()); |
2311 | test_copysign_generic(2.0_f64, -2.0_f64, f64::nan()); |
2312 | test_copysignf(2.0_f32, -2.0_f32, f32::nan()); |
2313 | } |
2314 | |
2315 | #[cfg (any(feature = "std" , feature = "libm" ))] |
2316 | fn test_copysignf(p: f32, n: f32, nan: f32) { |
2317 | use core::ops::Neg; |
2318 | use float::Float; |
2319 | |
2320 | assert!(p.is_sign_positive()); |
2321 | assert!(n.is_sign_negative()); |
2322 | assert!(nan.is_nan()); |
2323 | |
2324 | assert_eq!(p, Float::copysign(p, p)); |
2325 | assert_eq!(p.neg(), Float::copysign(p, n)); |
2326 | |
2327 | assert_eq!(n, Float::copysign(n, n)); |
2328 | assert_eq!(n.neg(), Float::copysign(n, p)); |
2329 | |
2330 | // FIXME: is_sign... only works on NaN starting in Rust 1.20 |
2331 | // assert!(Float::copysign(nan, p).is_sign_positive()); |
2332 | // assert!(Float::copysign(nan, n).is_sign_negative()); |
2333 | } |
2334 | |
2335 | #[cfg (any(feature = "std" , feature = "libm" ))] |
2336 | fn test_copysign_generic<F: ::float::Float + ::core::fmt::Debug>(p: F, n: F, nan: F) { |
2337 | assert!(p.is_sign_positive()); |
2338 | assert!(n.is_sign_negative()); |
2339 | assert!(nan.is_nan()); |
2340 | |
2341 | assert_eq!(p, p.copysign(p)); |
2342 | assert_eq!(p.neg(), p.copysign(n)); |
2343 | |
2344 | assert_eq!(n, n.copysign(n)); |
2345 | assert_eq!(n.neg(), n.copysign(p)); |
2346 | |
2347 | // FIXME: is_sign... only works on NaN starting in Rust 1.20 |
2348 | // assert!(nan.copysign(p).is_sign_positive()); |
2349 | // assert!(nan.copysign(n).is_sign_negative()); |
2350 | } |
2351 | } |
2352 | |