adds more math functions and fixes parens (#558)
* nested parens fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * e, tau Signed-off-by: Jess Frazelle <github@jessfraz.com> * docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove test w log since that is a stdlib fn now Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
@ -58,14 +58,14 @@ fn inner_tan(num: f64) -> Result<f64, KclError> {
|
||||
Ok(num.tan())
|
||||
}
|
||||
|
||||
/// Return the value of `pi`.
|
||||
/// Return the value of `pi`. Archimedes’ constant (π).
|
||||
pub fn pi(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let result = inner_pi()?;
|
||||
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Return the value of `pi`.
|
||||
/// Return the value of `pi`. Archimedes’ constant (π).
|
||||
#[stdlib {
|
||||
name = "pi",
|
||||
}]
|
||||
@ -137,7 +137,7 @@ fn inner_ceil(num: f64) -> Result<f64, KclError> {
|
||||
Ok(num.ceil())
|
||||
}
|
||||
|
||||
/// Returns the minimum of the given arguments.
|
||||
/// Computes the minimum of the given arguments.
|
||||
pub fn min(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let nums = args.get_number_array()?;
|
||||
let result = inner_min(nums);
|
||||
@ -145,7 +145,7 @@ pub fn min(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Returns the minimum of the given arguments.
|
||||
/// Computes the minimum of the given arguments.
|
||||
#[stdlib {
|
||||
name = "min",
|
||||
}]
|
||||
@ -160,7 +160,7 @@ fn inner_min(args: Vec<f64>) -> f64 {
|
||||
min
|
||||
}
|
||||
|
||||
/// Returns the maximum of the given arguments.
|
||||
/// Computes the maximum of the given arguments.
|
||||
pub fn max(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let nums = args.get_number_array()?;
|
||||
let result = inner_max(nums);
|
||||
@ -168,7 +168,7 @@ pub fn max(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Returns the maximum of the given arguments.
|
||||
/// Computes the maximum of the given arguments.
|
||||
#[stdlib {
|
||||
name = "max",
|
||||
}]
|
||||
@ -260,3 +260,118 @@ pub fn atan(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
fn inner_atan(num: f64) -> Result<f64, KclError> {
|
||||
Ok(num.atan())
|
||||
}
|
||||
|
||||
/// Computes the logarithm of the number with respect to an arbitrary base.
|
||||
///
|
||||
/// The result might not be correctly rounded owing to implementation
|
||||
/// details; `log2()` can produce more accurate results for base 2,
|
||||
/// and `log10()` can produce more accurate results for base 10.
|
||||
pub fn log(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let nums = args.get_number_array()?;
|
||||
if nums.len() > 2 {
|
||||
return Err(KclError::Type(KclErrorDetails {
|
||||
message: format!("expected 2 arguments, got {}", nums.len()),
|
||||
source_ranges: vec![args.source_range],
|
||||
}));
|
||||
}
|
||||
|
||||
if nums.len() <= 1 {
|
||||
return Err(KclError::Type(KclErrorDetails {
|
||||
message: format!("expected 2 arguments, got {}", nums.len()),
|
||||
source_ranges: vec![args.source_range],
|
||||
}));
|
||||
}
|
||||
let result = inner_log(nums[0], nums[1])?;
|
||||
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Computes the logarithm of the number with respect to an arbitrary base.
|
||||
///
|
||||
/// The result might not be correctly rounded owing to implementation
|
||||
/// details; `log2()` can produce more accurate results for base 2,
|
||||
/// and `log10()` can produce more accurate results for base 10.
|
||||
#[stdlib {
|
||||
name = "log",
|
||||
}]
|
||||
fn inner_log(num: f64, base: f64) -> Result<f64, KclError> {
|
||||
Ok(num.log(base))
|
||||
}
|
||||
|
||||
/// Computes the base 2 logarithm of the number.
|
||||
pub fn log2(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let num = args.get_number()?;
|
||||
let result = inner_log2(num)?;
|
||||
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Computes the base 2 logarithm of the number.
|
||||
#[stdlib {
|
||||
name = "log2",
|
||||
}]
|
||||
fn inner_log2(num: f64) -> Result<f64, KclError> {
|
||||
Ok(num.log2())
|
||||
}
|
||||
|
||||
/// Computes the base 10 logarithm of the number.
|
||||
pub fn log10(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let num = args.get_number()?;
|
||||
let result = inner_log10(num)?;
|
||||
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Computes the base 10 logarithm of the number.
|
||||
#[stdlib {
|
||||
name = "log10",
|
||||
}]
|
||||
fn inner_log10(num: f64) -> Result<f64, KclError> {
|
||||
Ok(num.log10())
|
||||
}
|
||||
|
||||
/// Computes the natural logarithm of the number.
|
||||
pub fn ln(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let num = args.get_number()?;
|
||||
let result = inner_ln(num)?;
|
||||
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Computes the natural logarithm of the number.
|
||||
#[stdlib {
|
||||
name = "ln",
|
||||
}]
|
||||
fn inner_ln(num: f64) -> Result<f64, KclError> {
|
||||
Ok(num.ln())
|
||||
}
|
||||
|
||||
/// Return the value of Euler’s number `e`.
|
||||
pub fn e(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let result = inner_e()?;
|
||||
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Return the value of Euler’s number `e`.
|
||||
#[stdlib {
|
||||
name = "e",
|
||||
}]
|
||||
fn inner_e() -> Result<f64, KclError> {
|
||||
Ok(std::f64::consts::E)
|
||||
}
|
||||
|
||||
/// Return the value of `tau`. The full circle constant (τ). Equal to 2π.
|
||||
pub fn tau(args: &mut Args) -> Result<MemoryItem, KclError> {
|
||||
let result = inner_tau()?;
|
||||
|
||||
args.make_user_val_from_f64(result)
|
||||
}
|
||||
|
||||
/// Return the value of `tau`. The full circle constant (τ). Equal to 2π.
|
||||
#[stdlib {
|
||||
name = "tau",
|
||||
}]
|
||||
fn inner_tau() -> Result<f64, KclError> {
|
||||
Ok(std::f64::consts::TAU)
|
||||
}
|
||||
|
Reference in New Issue
Block a user