Combined to_scientific_notation() methods for both types, and added tests

This commit is contained in:
bakk 2021-05-18 23:03:21 +02:00
parent cc776367fa
commit e04742695a
3 changed files with 48 additions and 22 deletions

View File

@ -10,6 +10,7 @@ pub use regular::*;
use lazy_static::lazy_static;
use std::collections::HashMap;
use wasm_bindgen::prelude::*;
lazy_static! {
static ref CONSTANTS: HashMap<&'static str, &'static str> = {
@ -42,12 +43,35 @@ lazy_static! {
};
}
#[wasm_bindgen]
impl KalkNum {
#[cfg(not(feature = "rug"))]
#[wasm_bindgen(js_name = estimate)]
pub fn estimate_js(&self) -> Option<String> {
self.estimate()
}
}
impl KalkNum {
pub fn to_scientific_notation(&self) -> ScientificNotation {
let value_string = self.value.to_string();
let trimmed = if value_string.contains(".") {
value_string.trim_end_matches("0")
} else {
&value_string
};
ScientificNotation {
negative: self.value < 0f64,
digits: trimmed
.to_string()
.replace(".", "")
.trim_start_matches("0")
.to_string(),
// I... am not sure what else to do...
exponent: KalkNum::new(self.value.clone().log10(), "").to_i32(),
}
}
// Get an estimate of what the number is, eg. 3.141592 => π
pub fn estimate(&self) -> Option<String> {
@ -231,4 +255,17 @@ mod tests {
assert_eq!(output, result);
}
}
#[test]
fn test_to_scientific_notation() {
let num = KalkNum::from(0.000001f64);
let sci_not = num.to_scientific_notation();
assert_eq!(sci_not.negative, false);
assert_eq!(sci_not.exponent, -6);
let num = KalkNum::from(123.456789f64);
let sci_not = num.to_scientific_notation();
assert_eq!(sci_not.negative, false);
assert_eq!(sci_not.exponent, 2);
}
}

View File

@ -49,6 +49,11 @@ impl KalkNum {
self.value
}
#[wasm_bindgen(js_name = getValue)]
pub fn to_i32(&self) -> i32 {
self.value as i32
}
#[wasm_bindgen(js_name = toString)]
pub fn to_string(&self) -> String {
let string_value = self.value.to_string();
@ -88,12 +93,8 @@ impl KalkNum {
}
#[wasm_bindgen(js_name = toScientificNotation)]
pub fn to_scientific_notation(&self) -> ScientificNotation {
ScientificNotation {
negative: self.value < 0f64,
digits: self.value.to_string().replace(".", ""),
exponent: self.value.log(10f64) as i32 + 1,
}
pub fn to_scientific_notation_js(&self) -> ScientificNotation {
self.to_scientific_notation()
}
pub(crate) fn convert_to_unit(

View File

@ -49,6 +49,10 @@ impl KalkNum {
self.value.to_f64_round(rug::float::Round::Nearest)
}
pub fn to_i32(&self) -> i32 {
self.value.to_i32_saturating().unwrap()
}
pub fn to_string(&self) -> String {
let as_str = self.to_f64().to_string();
@ -82,22 +86,6 @@ impl KalkNum {
self.unit.len() > 0
}
pub fn to_scientific_notation(&self) -> ScientificNotation {
let (neg, digits, exp_option) =
self.value
.to_sign_string_exp_round(10, None, rug::float::Round::Up);
ScientificNotation {
negative: neg,
digits: digits
.trim_start_matches('0')
.trim_end_matches('0')
.trim_end_matches('.')
.to_string(),
exponent: if let Some(exp) = exp_option { exp } else { 0 },
}
}
pub fn convert_to_unit(
&self,
context: &mut crate::interpreter::Context,