Skip to content

Commit

Permalink
Add missing min/max functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed May 21, 2024
1 parent 06c2dfa commit c014343
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Bug fixes
macro. This is to allow for `rust_2018_idioms` lints (thanks [`@ltabis`](https://github.com/ltabis) [864](https://github.com/rhaiscript/rhai/issues/864)).
* The `sync` feature now works properly in `no-std` builds (thanks [`@misssonder`](https://github.com/misssonder) [874](https://github.com/rhaiscript/rhai/pull/874)).
* More data-race conditions are caught and returned as errors instead of panicking.
* Missing `min` and `max` functions where both operands are floats or `Decimal` are added.

New features
------------
Expand Down
48 changes: 48 additions & 0 deletions src/packages/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ mod min_max_functions {
mod float_functions {
use crate::INT;

#[rhai_fn(name = "max")]
pub fn max_ff_32(x: f32, y: f32) -> f32 {
if x >= y {
x
} else {
y
}
}
#[rhai_fn(name = "max")]
pub fn max_if_32(x: INT, y: f32) -> f32 {
let (x, y) = (x as f32, y);
Expand All @@ -134,6 +142,14 @@ mod float_functions {
}
}
#[rhai_fn(name = "min")]
pub fn min_ff_32(x: f32, y: f32) -> f32 {
if x <= y {
x
} else {
y
}
}
#[rhai_fn(name = "min")]
pub fn min_if_32(x: INT, y: f32) -> f32 {
let (x, y) = (x as f32, y);
if x <= y {
Expand All @@ -152,6 +168,14 @@ mod float_functions {
}
}
#[rhai_fn(name = "max")]
pub fn max_ff_64(x: f64, y: f64) -> f64 {
if x >= y {
x
} else {
y
}
}
#[rhai_fn(name = "max")]
pub fn max_if_64(x: INT, y: f64) -> f64 {
let (x, y) = (x as f64, y);
if x >= y {
Expand All @@ -170,6 +194,14 @@ mod float_functions {
}
}
#[rhai_fn(name = "min")]
pub fn min_ff_64(x: f64, y: f64) -> f64 {
if x <= y {
x
} else {
y
}
}
#[rhai_fn(name = "min")]
pub fn min_if_64(x: INT, y: f64) -> f64 {
let (x, y) = (x as f64, y);
if x <= y {
Expand Down Expand Up @@ -419,6 +451,14 @@ mod decimal_functions {
use crate::INT;
use rust_decimal::Decimal;

#[rhai_fn(name = "max")]
pub fn max_dd(x: Decimal, y: Decimal) -> Decimal {
if x >= y {
x
} else {
y
}
}
#[rhai_fn(name = "max")]
pub fn max_id(x: INT, y: Decimal) -> Decimal {
let x = x.into();
Expand All @@ -438,6 +478,14 @@ mod decimal_functions {
}
}
#[rhai_fn(name = "min")]
pub fn min_dd(x: Decimal, y: Decimal) -> Decimal {
if x <= y {
x
} else {
y
}
}
#[rhai_fn(name = "min")]
pub fn min_id(x: INT, y: Decimal) -> Decimal {
let x = x.into();
if x <= y {
Expand Down

0 comments on commit c014343

Please sign in to comment.