float_polyfills/
float_32.rs

1/// Polyfills for various `std`-only `f32` methods.
2pub trait F32Polyfill: Sized {
3    /// Returns absolute value of `self`.
4    fn abs(self) -> f32;
5
6    /// Returns arc cosine of `self`.
7    fn acos(self) -> f32;
8
9    /// Returns arc sine of `self`.
10    fn asin(self) -> f32;
11
12    /// Returns arc tangent of `self`.
13    fn atan(self) -> f32;
14
15    /// Return arc tangent of `self/other`.
16    fn atan2(self, other: f32) -> f32;
17
18    /// Returns the smallest integer greater than or equal to `self`.
19    fn ceil(self) -> f32;
20
21    /// Returns a number with magnitude of `self` and sign of `sign`.
22    fn copysign(self, sign: f32) -> f32;
23
24    /// Returns cosine of `self`.
25    fn cos(self) -> f32;
26
27    /// Returns `e^(self)`.
28    fn exp(self) -> f32;
29
30    /// Returns the largest integer less than or equal to `self`.
31    fn floor(self) -> f32;
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: f32) -> f32;
37
38    /// Return natural logarithm of `self`.
39    fn ln(self) -> f32;
40
41    /// Returns base 2 logarithm of `self`.
42    fn log2(self) -> f32;
43
44    /// Returns base 10 logarithm of `self`.
45    fn log10(self) -> f32;
46
47    /// Returns `(self * a) + b`.
48    fn mul_add(self, a: f32, b: f32) -> f32;
49
50    /// Returns `(self)^n`.
51    fn powf(self, n: f32) -> f32;
52
53    /// Returns `(self)^n`.
54    fn powi(self, n: i32) -> f32;
55
56    /// Returns `1/(self)`.
57    fn recip(self) -> f32;
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) -> f32;
62
63    /// Returns sine of `self`.
64    fn sin(self) -> f32;
65
66    /// Returns `(sin(self), cos(self))`.
67    fn sin_cos(self) -> (f32, f32);
68
69    /// Returns square root of `self`.
70    fn sqrt(self) -> f32;
71
72    /// Returns tangent of `self`.
73    fn tan(self) -> f32;
74
75    /// Returns the integer part of `self`.
76    /// This means that non-integer numbers are always truncated towards zero.
77    fn trunc(self) -> f32;
78}
79
80
81impl F32Polyfill for f32 {
82    fn abs(self) -> f32 {
83        libm::fabsf(self)
84    }
85
86    fn acos(self) -> f32 {
87        libm::acosf(self)
88    }
89
90    fn asin(self) -> f32 {
91        libm::asinf(self)
92    }
93
94    fn atan(self) -> f32 {
95        libm::atanf(self)
96    }
97
98    fn atan2(self, other: f32) -> f32 {
99        libm::atan2f(self, other)
100    }
101
102    fn ceil(self) -> f32 {
103        libm::ceilf(self)
104    }
105
106    fn copysign(self, sign: f32) -> f32 {
107        libm::copysignf(self, sign)
108    }
109
110    fn cos(self) -> f32 {
111        libm::cosf(self)
112    }
113
114    fn exp(self) -> f32 {
115        libm::expf(self)
116    }
117
118    fn floor(self) -> f32 {
119        libm::floorf(self)
120    }
121
122    fn hypot(self, other: f32) -> f32 {
123        libm::hypotf(self, other)
124    }
125
126    fn ln(self) -> f32 {
127        libm::logf(self)
128    }
129
130    fn log2(self) -> f32 {
131        libm::log2f(self)
132    }
133
134    fn log10(self) -> f32 {
135        libm::log10f(self)
136    }
137
138    fn mul_add(self, a: f32, b: f32) -> f32 {
139        (self * a) + b
140    }
141
142    fn powf(self, n: f32) -> f32 {
143        libm::powf(self, n)
144    }
145
146    fn powi(self, n: i32) -> f32 {
147        libm::powf(self, n as f32)
148    }
149
150    fn recip(self) -> f32 {
151        1.0/self
152    }
153
154    fn round(self) -> f32 {
155        libm::roundf(self)
156    }
157
158    fn sin(self) -> f32 {
159        libm::sinf(self)
160    }
161
162    fn sin_cos(self) -> (f32, f32) {
163        libm::sincosf(self)
164    }
165
166    fn sqrt(self) -> f32 {
167        libm::sqrtf(self)
168    }
169
170    fn tan(self) -> f32 {
171        libm::tanf(self)
172    }
173
174    fn trunc(self) -> f32 {
175        libm::truncf(self)
176    }
177}