Add is_odd, is_even and is_zero.

This commit is contained in:
Stephen Chung 2021-06-27 22:10:52 +08:00
parent 1247b89352
commit 7196b017f5
2 changed files with 37 additions and 0 deletions

View File

@ -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
==============

View File

@ -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<f32, Box<EvalAltResult>> {
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()
}
}