1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Lossy (f64-bridge) trig methods for D38.
//!
//! Companion to `trig_strict.rs`. The plain methods here are the
//! f64-bridge variants, gated on std + (no strict feature or
//! fast set). When strict is on, the dispatcher in the
//! _strict file shadows these.
use crate::core_type::D38;
impl<const SCALE: u32> D38<SCALE> {
// ── Forward trig (radians input) ──────────────────────────────────
/// Sine of `self`, where `self` is in radians.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // sin(0) == 0 (bit-exact: f64::sin(0.0) == 0.0).
/// assert_eq!(D38s12::ZERO.sin(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn sin_fast(self) -> Self {
Self::from_f64(self.to_f64().sin())
}
/// Cosine of `self`, where `self` is in radians.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // cos(0) == 1 (bit-exact: f64::cos(0.0) == 1.0).
/// assert_eq!(D38s12::ZERO.cos(), D38s12::ONE);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn cos_fast(self) -> Self {
Self::from_f64(self.to_f64().cos())
}
/// Tangent of `self`, where `self` is in radians.
///
/// `f64::tan` returns very large magnitudes near odd multiples of
/// `pi/2` and infinity at the limit. Inputs that drive the f64
/// result outside `[D38::MIN, D38::MAX]` saturate per
/// [`Self::from_f64`].
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // tan(0) == 0 (bit-exact: f64::tan(0.0) == 0.0).
/// assert_eq!(D38s12::ZERO.tan(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn tan_fast(self) -> Self {
Self::from_f64(self.to_f64().tan())
}
// ── Inverse trig (returns radians) ────────────────────────────────
/// Arcsine of `self`. Returns radians in `[-pi/2, pi/2]`.
///
/// `f64::asin` returns NaN for inputs outside `[-1, 1]`, which
/// [`Self::from_f64`] maps to `D38::ZERO`.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // asin(0) == 0.
/// assert_eq!(D38s12::ZERO.asin(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn asin_fast(self) -> Self {
Self::from_f64(self.to_f64().asin())
}
/// Arccosine of `self`. Returns radians in `[0, pi]`.
///
/// `f64::acos` returns NaN for inputs outside `[-1, 1]`, which
/// [`Self::from_f64`] maps to `D38::ZERO`.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::{D38s12, DecimalConsts};
/// // acos(1) == 0.
/// assert_eq!(D38s12::ONE.acos(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn acos_fast(self) -> Self {
Self::from_f64(self.to_f64().acos())
}
/// Arctangent of `self`. Returns radians in `(-pi/2, pi/2)`.
///
/// Defined for the entire real line.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // atan(0) == 0.
/// assert_eq!(D38s12::ZERO.atan(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn atan_fast(self) -> Self {
Self::from_f64(self.to_f64().atan())
}
/// Four-quadrant arctangent of `self` (`y`) over `other` (`x`).
/// Returns radians in `(-pi, pi]`.
///
/// Signature matches `f64::atan2(self, other)`: the receiver is
/// `y` and the argument is `x`.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::{D38s12, DecimalConsts};
/// // atan2(1, 1) ~= pi/4 (45 degrees, first quadrant).
/// let one = D38s12::ONE;
/// let result = one.atan2(one); // approximately D38s12::quarter_pi()
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn atan2_fast(self, other: Self) -> Self {
Self::from_f64(self.to_f64().atan2(other.to_f64()))
}
// ── Hyperbolic ────────────────────────────────────────────────────
/// Hyperbolic sine of `self`.
///
/// Defined for the entire real line. Saturates at large magnitudes
/// per [`Self::from_f64`].
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // sinh(0) == 0.
/// assert_eq!(D38s12::ZERO.sinh(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn sinh_fast(self) -> Self {
Self::from_f64(self.to_f64().sinh())
}
/// Hyperbolic cosine of `self`.
///
/// Defined for the entire real line; result is always >= 1.
/// Saturates at large magnitudes per [`Self::from_f64`].
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // cosh(0) == 1.
/// assert_eq!(D38s12::ZERO.cosh(), D38s12::ONE);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn cosh_fast(self) -> Self {
Self::from_f64(self.to_f64().cosh())
}
/// Hyperbolic tangent of `self`.
///
/// Defined for the entire real line; range is `(-1, 1)`.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // tanh(0) == 0.
/// assert_eq!(D38s12::ZERO.tanh(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn tanh_fast(self) -> Self {
Self::from_f64(self.to_f64().tanh())
}
/// Inverse hyperbolic sine of `self`.
///
/// Defined for the entire real line.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // asinh(0) == 0.
/// assert_eq!(D38s12::ZERO.asinh(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn asinh_fast(self) -> Self {
Self::from_f64(self.to_f64().asinh())
}
/// Inverse hyperbolic cosine of `self`.
///
/// `f64::acosh` returns NaN for inputs less than 1, which
/// [`Self::from_f64`] maps to `D38::ZERO`.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // acosh(1) == 0.
/// assert_eq!(D38s12::ONE.acosh(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn acosh_fast(self) -> Self {
Self::from_f64(self.to_f64().acosh())
}
/// Inverse hyperbolic tangent of `self`.
///
/// `f64::atanh` returns NaN for inputs outside `(-1, 1)`, which
/// [`Self::from_f64`] maps to `D38::ZERO`.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // atanh(0) == 0.
/// assert_eq!(D38s12::ZERO.atanh(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn atanh_fast(self) -> Self {
Self::from_f64(self.to_f64().atanh())
}
// ── Angle conversions ─────────────────────────────────────────────
/// Convert radians to degrees: `self * (180 / pi)`.
///
/// Routed through `f64::to_degrees` so results match the de facto
/// reference produced by the rest of the Rust ecosystem. Multiplying
/// by a precomputed `D38` factor derived from `D38::pi()` would
/// diverge from f64 by a 1-LSB rescale rounding without any
/// practical determinism gain, since the f64 bridge is already the
/// precision floor.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // to_degrees(0) == 0.
/// assert_eq!(D38s12::ZERO.to_degrees(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn to_degrees_fast(self) -> Self {
Self::from_f64(self.to_f64().to_degrees())
}
/// Convert degrees to radians: `self * (pi / 180)`.
///
/// Routed through `f64::to_radians`. See [`Self::to_degrees`] for
/// the rationale.
///
/// # Precision
///
/// Lossy: involves f64 at some point; result may lose precision.
///
/// # Examples
///
/// ```ignore
/// # #[cfg(feature = "std")]
/// # {
/// use decimal_scaled::D38s12;
/// // to_radians(0) == 0.
/// assert_eq!(D38s12::ZERO.to_radians(), D38s12::ZERO);
/// # }
/// ```
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn to_radians_fast(self) -> Self {
Self::from_f64(self.to_f64().to_radians())
}
}
#[cfg(all(feature = "std", any(not(feature = "strict"), feature = "fast")))]
impl<const SCALE: u32> D38<SCALE> {
/// Plain dispatcher: forwards to [`Self::sin_fast`] in this feature mode.
#[inline] #[must_use] pub fn sin(self) -> Self { self.sin_fast() }
/// Plain dispatcher: forwards to [`Self::cos_fast`] in this feature mode.
#[inline] #[must_use] pub fn cos(self) -> Self { self.cos_fast() }
/// Plain dispatcher: forwards to [`Self::tan_fast`] in this feature mode.
#[inline] #[must_use] pub fn tan(self) -> Self { self.tan_fast() }
/// Plain dispatcher: forwards to [`Self::asin_fast`] in this feature mode.
#[inline] #[must_use] pub fn asin(self) -> Self { self.asin_fast() }
/// Plain dispatcher: forwards to [`Self::acos_fast`] in this feature mode.
#[inline] #[must_use] pub fn acos(self) -> Self { self.acos_fast() }
/// Plain dispatcher: forwards to [`Self::atan_fast`] in this feature mode.
#[inline] #[must_use] pub fn atan(self) -> Self { self.atan_fast() }
/// Plain dispatcher: forwards to [`Self::atan2_fast`] in this feature mode.
#[inline] #[must_use] pub fn atan2(self, other: Self) -> Self { self.atan2_fast(other) }
/// Plain dispatcher: forwards to [`Self::sinh_fast`] in this feature mode.
#[inline] #[must_use] pub fn sinh(self) -> Self { self.sinh_fast() }
/// Plain dispatcher: forwards to [`Self::cosh_fast`] in this feature mode.
#[inline] #[must_use] pub fn cosh(self) -> Self { self.cosh_fast() }
/// Plain dispatcher: forwards to [`Self::tanh_fast`] in this feature mode.
#[inline] #[must_use] pub fn tanh(self) -> Self { self.tanh_fast() }
/// Plain dispatcher: forwards to [`Self::asinh_fast`] in this feature mode.
#[inline] #[must_use] pub fn asinh(self) -> Self { self.asinh_fast() }
/// Plain dispatcher: forwards to [`Self::acosh_fast`] in this feature mode.
#[inline] #[must_use] pub fn acosh(self) -> Self { self.acosh_fast() }
/// Plain dispatcher: forwards to [`Self::atanh_fast`] in this feature mode.
#[inline] #[must_use] pub fn atanh(self) -> Self { self.atanh_fast() }
/// Plain dispatcher: forwards to [`Self::to_degrees_fast`] in this feature mode.
#[inline] #[must_use] pub fn to_degrees(self) -> Self { self.to_degrees_fast() }
/// Plain dispatcher: forwards to [`Self::to_radians_fast`] in this feature mode.
#[inline] #[must_use] pub fn to_radians(self) -> Self { self.to_radians_fast() }
}