From b25000a31579ee319b6116b898cb904e0981158b Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Fri, 8 Nov 2024 09:19:06 +0800 Subject: [PATCH] feat: implement Exp trait for generic exponentiation --- math/exp.mbt | 10 +++++++++- math/math.mbti | 7 +++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/math/exp.mbt b/math/exp.mbt index f539fae94..5d0ae8e88 100644 --- a/math/exp.mbt +++ b/math/exp.mbt @@ -12,6 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub fn exp(input : Double) -> Double { +trait Exp { + exp(Self) -> Double +} + +pub impl Exp for Double with exp(inp) { inp.exp() } + +pub impl Exp for Int with exp(inp) { inp.to_double().exp() } + +pub fn exp[T : Exp](input : T) -> Double { input.exp() } diff --git a/math/math.mbti b/math/math.mbti index 28c979bfa..78925d9e5 100644 --- a/math/math.mbti +++ b/math/math.mbti @@ -15,7 +15,7 @@ fn ceil(Double) -> Double fn cos(Double) -> Double -fn exp(Double) -> Double +fn exp[T : Exp](T) -> Double fn floor(Double) -> Double @@ -44,6 +44,9 @@ fn trunc(Double) -> Double // Type aliases // Traits - +trait Exp // Extension Methods +impl Exp for Int + +impl Exp for Double