Add new floating-point functions.

This commit is contained in:
Stephen Chung 2021-02-09 15:57:38 +08:00
parent 4815de3634
commit 8f47f7c9e2
2 changed files with 21 additions and 0 deletions

View File

@ -55,6 +55,7 @@ Enhancements
* `ahash` is used to hash function call parameters. This should yield speed improvements. * `ahash` is used to hash function call parameters. This should yield speed improvements.
* `Dynamic` and `ImmutableString` now implement `serde::Serialize` and `serde::Deserialize`. * `Dynamic` and `ImmutableString` now implement `serde::Serialize` and `serde::Deserialize`.
* `NativeCallContext` has a new field containing the name of the function called, useful when the same Rust function is registered under multiple names in Rhai. * `NativeCallContext` has a new field containing the name of the function called, useful when the same Rust function is registered under multiple names in Rhai.
* New functions `PI()` and `E()` to return mathematical constants, and `to_radians` and `to_degrees` to convert between radians and degrees.
Version 0.19.10 Version 0.19.10

View File

@ -154,6 +154,26 @@ mod trig_functions {
mod float_functions { mod float_functions {
use crate::FLOAT; use crate::FLOAT;
#[rhai_fn(name = "E")]
pub fn e() -> FLOAT {
#[cfg(not(feature = "f32_float"))]
return crate::stdlib::f64::consts::E;
#[cfg(feature = "f32_float")]
return crate::stdlib::f32::consts::E;
}
#[rhai_fn(name = "PI")]
pub fn pi() -> FLOAT {
#[cfg(not(feature = "f32_float"))]
return crate::stdlib::f64::consts::PI;
#[cfg(feature = "f32_float")]
return crate::stdlib::f32::consts::PI;
}
pub fn to_radians(x: FLOAT) -> FLOAT {
x.to_radians()
}
pub fn to_degrees(x: FLOAT) -> FLOAT {
x.to_degrees()
}
pub fn sqrt(x: FLOAT) -> FLOAT { pub fn sqrt(x: FLOAT) -> FLOAT {
x.sqrt() x.sqrt()
} }