embedded_charts/math/traits.rs
1//! Math operation traits for different numeric backends.
2
3use core::fmt::Debug;
4
5/// Core mathematical operations that all numeric types should support
6pub trait MathOps<T>: Copy + Clone + Debug {
7 /// Calculate the square root
8 fn sqrt(self) -> T;
9
10 /// Calculate the absolute value
11 fn abs(self) -> T;
12
13 /// Return the minimum of two values
14 fn min(self, other: T) -> T;
15
16 /// Return the maximum of two values
17 fn max(self, other: T) -> T;
18
19 /// Calculate the floor (largest integer less than or equal to the value)
20 fn floor(self) -> T;
21
22 /// Calculate the ceiling (smallest integer greater than or equal to the value)
23 fn ceil(self) -> T;
24
25 /// Raise to a power
26 fn pow(self, exp: T) -> T;
27
28 /// Natural logarithm
29 fn ln(self) -> T;
30
31 /// Base-10 logarithm
32 fn log10(self) -> T;
33}
34
35/// Trigonometric operations
36pub trait TrigOps<T>: Copy + Clone + Debug {
37 /// Sine function (input in radians)
38 fn sin(self) -> T;
39
40 /// Cosine function (input in radians)
41 fn cos(self) -> T;
42
43 /// Tangent function (input in radians)
44 fn tan(self) -> T;
45
46 /// Convert degrees to radians
47 fn to_radians(self) -> T;
48
49 /// Convert radians to degrees
50 fn to_degrees(self) -> T;
51}
52
53/// Trait for types that can behave like floating-point numbers
54pub trait FloatLike<T>: MathOps<T> + TrigOps<T> + PartialOrd + PartialEq {
55 /// Zero value
56 fn zero() -> T;
57
58 /// One value
59 fn one() -> T;
60
61 /// Pi constant
62 fn pi() -> T;
63
64 /// Check if the value is finite
65 fn is_finite(self) -> bool;
66
67 /// Check if the value is NaN
68 fn is_nan(self) -> bool;
69
70 /// Check if the value is infinite
71 fn is_infinite(self) -> bool;
72}
73
74/// Backend trait that provides math operations for a specific numeric type
75pub trait MathBackend<T> {
76 /// Calculate the square root
77 fn sqrt(&self, x: T) -> T;
78
79 /// Calculate the absolute value
80 fn abs(&self, x: T) -> T;
81
82 /// Return the minimum of two values
83 fn min(&self, a: T, b: T) -> T;
84
85 /// Return the maximum of two values
86 fn max(&self, a: T, b: T) -> T;
87
88 /// Calculate the floor
89 fn floor(&self, x: T) -> T;
90
91 /// Calculate the ceiling
92 fn ceil(&self, x: T) -> T;
93
94 /// Raise to a power
95 fn pow(&self, x: T, y: T) -> T;
96
97 /// Natural logarithm
98 fn ln(&self, x: T) -> T;
99
100 /// Base-10 logarithm
101 fn log10(&self, x: T) -> T;
102
103 /// Sine function
104 fn sin(&self, x: T) -> T;
105
106 /// Cosine function
107 fn cos(&self, x: T) -> T;
108
109 /// Tangent function
110 fn tan(&self, x: T) -> T;
111
112 /// Convert degrees to radians
113 fn to_radians(&self, degrees: T) -> T;
114
115 /// Convert radians to degrees
116 fn to_degrees(&self, radians: T) -> T;
117
118 /// Calculate atan2(y, x) - the angle from the positive x-axis to the point (x, y)
119 fn atan2(&self, y: T, x: T) -> T;
120}