diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 53d31e8..43edfb9 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -85,9 +85,9 @@ impl PreToken { return Ok(match &s[..] { // Mathematical constants // 100 digits of each. - "π"|"pi" => { Token::Constant(Quantity::new_float_from_string("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"), String::from("π")) }, - "e" => { Token::Constant(Quantity::new_float_from_string("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427"), String::from("e")) }, - "phi"|"φ" => { Token::Constant(Quantity::new_float_from_string("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137"), String::from("φ")) }, + "π"|"pi" => { Token::Constant(Quantity::new_float_from_string("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067").unwrap(), String::from("π")) }, + "e" => { Token::Constant(Quantity::new_float_from_string("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427").unwrap(), String::from("e")) }, + "phi"|"φ" => { Token::Constant(Quantity::new_float_from_string("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137").unwrap(), String::from("φ")) }, _ => { return Err((l, ParserError::Undefined(s))); } }); diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index 6a9f3b6..7fbb62f 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -102,11 +102,17 @@ impl Quantity { } } - pub fn new_float_from_string(s: &str) -> Quantity { + pub fn new_float_from_string(s: &str) -> Option { let v = Float::parse(s); - return Quantity::Float { - v: Float::with_val(FLOAT_PRECISION, v.unwrap()) - } + + let v = match v { + Ok(x) => x, + Err(_) => return None + }; + + return Some(Quantity::Float { + v: Float::with_val(FLOAT_PRECISION, v) + }) } @@ -189,6 +195,7 @@ impl Quantity { } quick_quant_fn!(fract); + quick_quant_fn!(exp); quick_quant_fn!(abs); quick_quant_fn!(floor); diff --git a/src/quantity/rationalq.rs b/src/quantity/rationalq.rs index 5916224..a4c47a0 100644 --- a/src/quantity/rationalq.rs +++ b/src/quantity/rationalq.rs @@ -86,6 +86,8 @@ impl RationalQ { } + pub fn exp(&self) -> Quantity {float!(self.to_float().exp())} + pub fn abs(&self) -> Quantity {rational!(self.val.clone().abs())} pub fn floor(&self) -> Quantity {rational!(self.val.clone().floor())} pub fn ceil(&self) -> Quantity {rational!(self.val.clone().ceil())}