From a2277d07f3d9b6326fc2fe7f95e048035636c603 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 17 Feb 2021 13:06:47 +0800 Subject: [PATCH] Add arithmetic operators between FLOAT and INT. --- RELEASES.md | 1 + src/packages/arithmetic.rs | 88 +++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index e364c74b..9e80f364 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -33,6 +33,7 @@ Enhancements * Functions resolution cache is used in more cases, making repeated function calls faster. * Added `atan(x, y)` and `hypot(x, y)` to `BasicMathPackage`. +* Added standard arithmetic operators between `FLOAT` and `INT`. Version 0.19.11 diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 60837813..39cae250 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -255,6 +255,48 @@ mod f32_functions { Ok(Dynamic::from(x.powf(y))) } } + + #[rhai_fn(name = "+")] + pub fn add_if(x: INT, y: f32) -> f32 { + (x as f32) + (y as f32) + } + #[rhai_fn(name = "+")] + pub fn add_fi(x: f32, y: INT) -> f32 { + (x as f32) + (y as f32) + } + #[rhai_fn(name = "-")] + pub fn subtract_if(x: INT, y: f32) -> f32 { + (x as f32) - (y as f32) + } + #[rhai_fn(name = "-")] + pub fn subtract_fi(x: f32, y: INT) -> f32 { + (x as f32) - (y as f32) + } + #[rhai_fn(name = "*")] + pub fn multiply_if(x: INT, y: f32) -> f32 { + (x as f32) * (y as f32) + } + #[rhai_fn(name = "*")] + pub fn multiply_fi(x: f32, y: INT) -> f32 { + (x as f32) * (y as f32) + } + #[rhai_fn(name = "/")] + pub fn divide_if(x: INT, y: f32) -> f32 { + (x as f32) / (y as f32) + } + #[rhai_fn(name = "/")] + pub fn divide_fi(x: f32, y: INT) -> f32 { + (x as f32) / (y as f32) + } + #[rhai_fn(name = "%")] + pub fn modulo_if(x: INT, y: f32) -> f32 { + (x as f32) % (y as f32) + } + #[rhai_fn(name = "%")] + pub fn modulo_fi(x: f32, y: INT) -> f32 { + (x as f32) % (y as f32) + } + #[rhai_fn(name = "-")] pub fn neg(x: f32) -> f32 { -x @@ -314,10 +356,52 @@ mod f64_functions { x % y } #[rhai_fn(name = "**", return_raw)] - pub fn pow_f_f(x: f64, y: f64) -> Result> { - Ok(Dynamic::from(x.powf(y))) + pub fn pow_f_f(x: f64, y: f64) -> f64 { + x.powf(y) } } + + #[rhai_fn(name = "+")] + pub fn add_if(x: INT, y: f64) -> f64 { + (x as f64) + (y as f64) + } + #[rhai_fn(name = "+")] + pub fn add_fi(x: f64, y: INT) -> f64 { + (x as f64) + (y as f64) + } + #[rhai_fn(name = "-")] + pub fn subtract_if(x: INT, y: f64) -> f64 { + (x as f64) - (y as f64) + } + #[rhai_fn(name = "-")] + pub fn subtract_fi(x: f64, y: INT) -> f64 { + (x as f64) - (y as f64) + } + #[rhai_fn(name = "*")] + pub fn multiply_if(x: INT, y: f64) -> f64 { + (x as f64) * (y as f64) + } + #[rhai_fn(name = "*")] + pub fn multiply_fi(x: f64, y: INT) -> f64 { + (x as f64) * (y as f64) + } + #[rhai_fn(name = "/")] + pub fn divide_if(x: INT, y: f64) -> f64 { + (x as f64) / (y as f64) + } + #[rhai_fn(name = "/")] + pub fn divide_fi(x: f64, y: INT) -> f64 { + (x as f64) / (y as f64) + } + #[rhai_fn(name = "%")] + pub fn modulo_if(x: INT, y: f64) -> f64 { + (x as f64) % (y as f64) + } + #[rhai_fn(name = "%")] + pub fn modulo_fi(x: f64, y: INT) -> f64 { + (x as f64) % (y as f64) + } + #[rhai_fn(name = "-")] pub fn neg(x: f64) -> f64 { -x