Shared Rust kernels
catalogue/engineering
All modules · quoted at commit 9e1884369a67 of DnaCalc/DnaCalc. Comments are the source's own.
radix_kind · 9 lines · calc-func/src/catalogue/engineering.rs on GitHub/// The radix functions and BESSELJ refuse logicals with #VALUE! and omitted arguments with #N/A.
/// A blank cell remains distinct from an omitted argument.
fn radix_kind(a: &[CalcValue], index: usize) -> Step<()> {
match a[index].core_ref() {
CoreRef::Logical(_) => fail(WorksheetErrorCode::Value),
CoreRef::Missing => fail(WorksheetErrorCode::NA),
_ => Ok(()),
}
}
Used by 16 functions.
places · 11 lines · calc-func/src/catalogue/engineering.rs on GitHub/// The optional places argument: checked (kind, number and range) before the number is read.
fn places(site: &Site<'_>, a: &[CalcValue]) -> Step<Option<f64>> {
if a.len() > 1 && !a[1].is_missing() {
radix_kind(a, 1)?;
let p = num(site, a, 1)?;
radix::normalize_places(Some(p)).map_err(Exit::Error)?;
Ok(Some(p))
} else {
Ok(None)
}
}
Used by 9 functions.
from_decimal · 10 lines · calc-func/src/catalogue/engineering.rs on GitHubfn from_decimal(
site: &Site<'_>,
a: &[CalcValue],
kernel: fn(f64, Option<f64>) -> Result<ExcelText, WorksheetErrorCode>,
) -> Step<CalcValue> {
let p = places(site, a)?;
radix_kind(a, 0)?;
let n = num(site, a, 0)?;
out_text(kernel(n, p))
}
Used by 3 functions.
to_decimal · 5 lines · calc-func/src/catalogue/engineering.rs on GitHubfn to_decimal(site: &Site<'_>, a: &[CalcValue], kernel: fn(&str) -> Result<f64, WorksheetErrorCode>) -> Step<CalcValue> {
radix_kind(a, 0)?;
let t = text(site, a, 0)?;
out(site, kernel(&t))
}
Used by 3 functions.
between · 10 lines · calc-func/src/catalogue/engineering.rs on GitHubfn between(
site: &Site<'_>,
a: &[CalcValue],
kernel: fn(&str, Option<f64>) -> Result<ExcelText, WorksheetErrorCode>,
) -> Step<CalcValue> {
let p = places(site, a)?;
radix_kind(a, 0)?;
let t = text(site, a, 0)?;
out_text(kernel(&t, p))
}
Used by 6 functions.
complex_of · 14 lines · calc-func/src/catalogue/engineering.rs on GitHub/// A value as a complex number.
fn complex_of(site: &Site<'_>, index: usize, value: &CalcValue) -> Step<ParsedComplex> {
match value.core_ref() {
CoreRef::Number(n) => Ok(ParsedComplex::new(n, 0.0, None)),
CoreRef::Text(t) => match String::from_utf16(t.utf16_code_units()) {
Ok(text) => parse_complex_text(&text).ok_or(Exit::Error(WorksheetErrorCode::Num)),
Err(_) => Err(site.not_modelled(Some(index))),
},
CoreRef::Logical(_) => fail(WorksheetErrorCode::Value),
CoreRef::Error(code) => fail(code),
CoreRef::Empty | CoreRef::Missing => Ok(ParsedComplex::new(0.0, 0.0, None)),
CoreRef::Array(_) | CoreRef::Reference(_) => Err(site.not_modelled(Some(index))),
}
}
Used by 25 functions.
complex_text · 7 lines · calc-func/src/catalogue/engineering.rs on GitHub/// A complex-number result as its text, with the suffix the operands use.
fn complex_text(value: Result<ParsedComplex, WorksheetErrorCode>, operands: &[ParsedComplex]) -> Step<CalcValue> {
let suffix = suffix_from_operands(operands).map_err(Exit::Error)?;
let value = value.map_err(Exit::Error)?;
let text = format_complex_text(value, suffix).map_err(Exit::Error)?;
Ok(text_value(&text))
}
Used by 21 functions.
unary_complex · 8 lines · calc-func/src/catalogue/engineering.rs on GitHubfn unary_complex(
site: &Site<'_>,
a: &[CalcValue],
kernel: fn(ParsedComplex) -> Result<ParsedComplex, WorksheetErrorCode>,
) -> Step<CalcValue> {
let z = complex_of(site, 0, &a[0])?;
complex_text(kernel(z), &[z])
}
Used by 16 functions.
complex_number · 8 lines · calc-func/src/catalogue/engineering.rs on GitHubfn complex_number(
site: &Site<'_>,
a: &[CalcValue],
kernel: fn(ParsedComplex) -> Result<f64, WorksheetErrorCode>,
) -> Step<CalcValue> {
let z = complex_of(site, 0, &a[0])?;
out(site, kernel(z).map(normalize_zero))
}
Used by 4 functions.
coefficient · 8 lines · calc-func/src/catalogue/engineering.rs on GitHub/// COMPLEX's real and imaginary coefficients: a number, numeric text, a blank as 0; a logical is
/// #VALUE!.
fn coefficient(site: &Site<'_>, a: &[CalcValue], index: usize) -> Step<f64> {
match a[index].core_ref() {
CoreRef::Logical(_) => fail(WorksheetErrorCode::Value),
_ => num(site, a, index),
}
}
Used by 2 functions.
complexes · 9 lines · calc-func/src/catalogue/engineering.rs on GitHub/// Every value of IMSUM's and IMPRODUCT's arguments as a complex number.
fn complexes(site: &Site<'_>, a: &[CalcValue], sv: &mut crate::call::EvalServices<'_>) -> Step<Vec<ParsedComplex>> {
let mut found = Vec::new();
values(site, a, 0, a.len(), sv, &mut |_, value| {
found.push(complex_of(site, 0, value)?);
Ok(())
})?;
Ok(found)
}
Used by 2 functions.
bessel · 7 lines · calc-func/src/catalogue/engineering.rs on GitHubfn bessel(site: &Site<'_>, a: &[CalcValue], kernel: fn(f64, f64) -> Result<f64, WorksheetErrorCode>, checked_kinds: bool) -> Step<CalcValue> {
if checked_kinds { radix_kind(a, 0)?; }
let x = num(site, a, 0)?;
if checked_kinds { radix_kind(a, 1)?; }
let n = num(site, a, 1)?;
out(site, kernel(x, n))
}
Used by 4 functions.
bits · 5 lines · calc-func/src/catalogue/engineering.rs on GitHubfn bits(site: &Site<'_>, a: &[CalcValue], kernel: fn(f64, f64) -> Result<f64, WorksheetErrorCode>) -> Step<CalcValue> {
let x = num(site, a, 0)?;
let y = num(site, a, 1)?;
out(site, kernel(x, y))
}
Used by 5 functions.