Added constants to rounding::CONSTANTS

This commit is contained in:
bakk 2022-01-16 22:55:20 +01:00 committed by PaddiM8
parent 89df281366
commit 4903707ce0
2 changed files with 44 additions and 34 deletions

View File

@ -14,41 +14,10 @@ mod rounding;
use crate::ast::Expr;
use crate::radix;
use lazy_static::lazy_static;
use std::collections::HashMap;
use wasm_bindgen::prelude::*;
const ACCEPTABLE_COMPARISON_MARGIN: f64 = 0.00000001;
lazy_static! {
static ref CONSTANTS: HashMap<&'static str, &'static str> = {
let mut m = HashMap::new();
m.insert("3.141592", "π");
m.insert("2.718281", "e");
m.insert("6.283185", "τ");
m.insert("1.618033", "ϕ");
m.insert("1.414213", "√2");
// Radian values for common angles
m.insert("0.523598", "π/6");
m.insert("0.785398", "π/4");
m.insert("1.047197", "π/3");
m.insert("1.570796", "π/2");
m.insert("2.094395", "2π/3");
m.insert("2.356194", "3π/4");
m.insert("2.617993", "5π/6");
m.insert("3.665191", "7π/6");
m.insert("3.926990", "5π/4");
m.insert("4.188790", "4π/3");
m.insert("4.712388", "3π/2");
m.insert("5.23598", "5π/3");
m.insert("5.497787", "7π/4");
m.insert("5.759586", "11π/6");
m.insert("6.283185", "");
m.insert("0.866025", "√3/2");
m
};
}
#[macro_export]
#[cfg(not(feature = "rug"))]
macro_rules! float {

View File

@ -1,6 +1,47 @@
use crate::float;
use std::{collections::HashMap};
use crate::{float, primitive};
use lazy_static::lazy_static;
use super::{ComplexNumberType, KalkValue};
lazy_static! {
static ref CONSTANTS: HashMap<&'static str, &'static str> = {
let mut m = HashMap::new();
m.insert("3.141592", "π");
m.insert("9.869604", "π²");
m.insert("0.318309", "1/π");
m.insert("0.636619", "2/π");
m.insert("2.718281", "e");
m.insert("7.389056", "");
m.insert("6.283185", "τ");
m.insert("1.618033", "ϕ");
m.insert("1.414213", "√2");
m.insert("0.707106", "1/√2");
m.insert("0.693147", "ln(2)");
m.insert("2.302585", "ln(10)");
// Radian values for common angles
m.insert("0.392699", "π/8");
m.insert("0.523598", "π/6");
m.insert("0.785398", "π/4");
m.insert("1.047197", "π/3");
m.insert("1.570796", "π/2");
m.insert("2.094395", "2π/3");
m.insert("2.356194", "3π/4");
m.insert("2.617993", "5π/6");
m.insert("3.665191", "7π/6");
m.insert("3.926990", "5π/4");
m.insert("4.188790", "4π/3");
m.insert("4.712388", "3π/2");
m.insert("5.23598", "5π/3");
m.insert("5.497787", "7π/4");
m.insert("5.759586", "11π/6");
m.insert("6.283185", "");
m.insert("0.866025", "√3/2");
m
};
}
use super::{ComplexNumberType, KalkValue, CONSTANTS};
pub(super) fn estimate(
input: &KalkValue,
@ -80,7 +121,7 @@ pub(super) fn estimate(
.values()
.0;
if squared.clone().sqrt().fract() != 0f64 && squared.clone().fract() == 0f64 {
return Some(format!("{}", squared));
return Some(format!("{}", primitive!(squared) as i32));
}
}