float_polyfills/
float_64.rs

1/// Polyfills for various `std`-only `f64` methods.
2pub trait F64Polyfill: Sized {
3    /// Returns absolute value of `self`.
4    fn abs(self) -> f64;
5
6    /// Returns arc cosine of `self`.
7    fn acos(self) -> f64;
8
9    /// Returns arc sine of `self`.
10    fn asin(self) -> f64;
11
12    /// Returns arc tangent of `self`.
13    fn atan(self) -> f64;
14
15    /// Return arc tangent of `self/other`.
16    fn atan2(self, other: f64) -> f64;
17
18    /// Returns the smallest integer greater than or equal to `self`.
19    fn ceil(self) -> f64;
20
21    /// Returns a number with magnitude of `self` and sign of `sign`.
22    fn copysign(self, sign: f64) -> f64;
23
24    /// Returns cosine of `self`.
25    fn cos(self) -> f64;
26
27    /// Returns `e^(self)`.
28    fn exp(self) -> f64;
29
30    /// Returns the largest integer less than or equal to `self`.
31    fn floor(self) -> f64;
32
33    /// Compute the distance between the origin and a point (`self`, `other`) on the Euclidean plane.
34    /// Equivalently, compute the length of the hypotenuse of a right-angle triangle
35    /// with other sides having length `self.abs()` and `other.abs()`.
36    fn hypot(self, other: f64) -> f64;
37
38    /// Return natural logarithm of `self`.
39    fn ln(self) -> f64;
40
41    /// Returns base 2 logarithm of `self`.
42    fn log2(self) -> f64;
43
44    /// Returns base 10 logarithm of `self`.
45    fn log10(self) -> f64;
46
47    /// Returns `(self * a) + b`.
48    fn mul_add(self, a: f64, b: f64) -> f64;
49
50    /// Returns `(self)^n`.
51    fn powf(self, n: f64) -> f64;
52
53    /// Returns `(self)^n`.
54    fn powi(self, n: i64) -> f64;
55
56    /// Returns `1/(self)`.
57    fn recip(self) -> f64;
58
59    /// Returns the nearest integer to `self`.
60    /// If a value is half-way between two integers, round away from `0.0`.
61    fn round(self) -> f64;
62
63    /// Returns sine of `self`.
64    fn sin(self) -> f64;
65
66    /// Returns `(sin(self), cos(self))`.
67    fn sin_cos(self) -> (f64, f64);
68
69    /// Returns square root of `self`.
70    fn sqrt(self) -> f64;
71
72    /// Returns tangent of `self`.
73    fn tan(self) -> f64;
74
75    /// Returns the integer part of `self`.
76    /// This means that non-integer numbers are always truncated towards zero.
77    fn trunc(self) -> f64;
78}
79
80
81impl F64Polyfill for f64 {
82    fn abs(self) -> f64 {
83        libm::fabs(self)
84    }
85
86    fn acos(self) -> f64 {
87        libm::acos(self)
88    }
89
90    fn asin(self) -> f64 {
91        libm::asin(self)
92    }
93
94    fn atan(self) -> f64 {
95        libm::atan(self)
96    }
97
98    fn atan2(self, other: f64) -> f64 {
99        libm::atan2(self, other)
100    }
101
102    fn ceil(self) -> f64 {
103        libm::ceil(self)
104    }
105
106    fn copysign(self, sign: f64) -> f64 {
107        libm::copysign(self, sign)
108    }
109
110    fn cos(self) -> f64 {
111        libm::cos(self)
112    }
113
114    fn exp(self) -> f64 {
115        libm::exp(self)
116    }
117
118    fn floor(self) -> f64 {
119        libm::floor(self)
120    }
121
122    fn hypot(self, other: f64) -> f64 {
123        libm::hypot(self, other)
124    }
125
126    fn ln(self) -> f64 {
127        libm::log(self)
128    }
129
130    fn log2(self) -> f64 {
131        libm::log2(self)
132    }
133
134    fn log10(self) -> f64 {
135        libm::log10(self)
136    }
137
138    fn mul_add(self, a: f64, b: f64) -> f64 {
139        (self * a) + b
140    }
141
142    fn powf(self, n: f64) -> f64 {
143        libm::pow(self, n)
144    }
145
146    fn powi(self, n: i64) -> f64 {
147        libm::pow(self, n as f64)
148    }
149
150    fn recip(self) -> f64 {
151        1.0/self
152    }
153
154    fn round(self) -> f64 {
155        libm::round(self)
156    }
157
158    fn sin(self) -> f64 {
159        libm::sin(self)
160    }
161
162    fn sin_cos(self) -> (f64, f64) {
163        libm::sincos(self)
164    }
165
166    fn sqrt(self) -> f64 {
167        libm::sqrt(self)
168    }
169
170    fn tan(self) -> f64 {
171        libm::tan(self)
172    }
173
174    fn trunc(self) -> f64 {
175        libm::trunc(self)
176    }
177}