From 8f47f7c9e239a716b2c491d5982c9d6bd4ff39f5 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 9 Feb 2021 15:57:38 +0800 Subject: [PATCH] Add new floating-point functions. --- RELEASES.md | 1 + src/packages/math_basic.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/RELEASES.md b/RELEASES.md index c647dcd0..473ad1d5 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -55,6 +55,7 @@ Enhancements * `ahash` is used to hash function call parameters. This should yield speed improvements. * `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. +* 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 diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 1199d4cf..06cd52ab 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -154,6 +154,26 @@ mod trig_functions { mod float_functions { 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 { x.sqrt() }