From 7196b017f501a68e4eba3353d789cc33139b6009 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sun, 27 Jun 2021 22:10:52 +0800 Subject: [PATCH] Add is_odd, is_even and is_zero. --- CHANGELOG.md | 5 +++++ src/packages/arithmetic.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0be17a00..2c1f9ffe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ The official version `1.0`. Almost the same version as `0.20.3` but with deprecated API removed. +Enhancements +------------ + +* New methods `is_odd`, `is_even` for integers, and `is_zero` for all numbers. + Version 0.20.3 ============== diff --git a/src/packages/arithmetic.rs b/src/packages/arithmetic.rs index 27d639c1..074b19cf 100644 --- a/src/packages/arithmetic.rs +++ b/src/packages/arithmetic.rs @@ -121,6 +121,15 @@ macro_rules! gen_arithmetic_functions { pub fn binary_xor(x: $arg_type, y: $arg_type) -> $arg_type { x ^ y } + pub fn is_zero(x: $arg_type) -> bool { + x == 0 + } + pub fn is_odd(x: $arg_type) -> bool { + x % 2 != 0 + } + pub fn is_even(x: $arg_type) -> bool { + x % 2 == 0 + } } })* } } @@ -174,6 +183,7 @@ macro_rules! reg_functions { } def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { + combine_with_exported_module!(lib, "int", int_functions); reg_functions!(lib += signed_basic; INT); #[cfg(not(feature = "only_i32"))] @@ -201,6 +211,19 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, { combine_with_exported_module!(lib, "decimal", decimal_functions); }); +#[export_module] +mod int_functions { + pub fn is_zero(x: INT) -> bool { + x == 0 + } + pub fn is_odd(x: INT) -> bool { + x % 2 != 0 + } + pub fn is_even(x: INT) -> bool { + x % 2 == 0 + } +} + gen_arithmetic_functions!(arith_basic => INT); #[cfg(not(feature = "only_i32"))] @@ -315,6 +338,9 @@ mod f32_functions { 1 } } + pub fn is_zero(x: f32) -> bool { + x == 0.0 + } #[rhai_fn(name = "**", return_raw)] pub fn pow_f_i(x: f32, y: INT) -> Result> { if cfg!(not(feature = "unchecked")) && y > (i32::MAX as INT) { @@ -420,6 +446,9 @@ mod f64_functions { 1 } } + pub fn is_zero(x: f64) -> bool { + x == 0.0 + } } #[cfg(feature = "decimal")] @@ -511,4 +540,7 @@ pub mod decimal_functions { 1 } } + pub fn is_zero(x: Decimal) -> bool { + x.is_zero() + } }