-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support for exponential notation like 1e10 => 10000000000 #69
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||
* Refer to LICENCE for more information. | ||||||
* */ | ||||||
|
||||||
use num::pow; | ||||||
use once_cell::sync::Lazy; | ||||||
use std::collections::HashMap; | ||||||
|
||||||
|
@@ -237,10 +238,32 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, CalcError | |||||
} | ||||||
'a'..='z' | 'A'..='Z' => { | ||||||
let parse_num = num_vec.parse::<f64>().ok(); | ||||||
|
||||||
if let Some(x) = parse_num { | ||||||
num_vec.clear(); | ||||||
|
||||||
// Check for exponential notation | ||||||
if letter == 'e' { | ||||||
while let Some(next) = chars.peek() { | ||||||
if !matches!(*next, '0'..='9') { | ||||||
break; | ||||||
} | ||||||
|
||||||
num_vec.push(*next); | ||||||
chars.next(); | ||||||
} | ||||||
|
||||||
if num_vec.len() > 0 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if let Some(exp) = num_vec.parse::<usize>().ok() { | ||||||
result.push(Token::Num(x * pow(10 as f64, exp) as f64)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't using
Suggested change
|
||||||
num_vec.clear(); | ||||||
continue; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
result.push(Token::Num(x)); | ||||||
result.push(OPERATORS.get(&'*').unwrap().clone()); | ||||||
num_vec.clear(); | ||||||
} | ||||||
char_vec.push(letter); | ||||||
last_char_is_op = false; | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,6 +283,16 @@ mod tests { | |
assert_eq!(evaled, Ok(20.0855369232)); | ||
} | ||
#[test] | ||
fn eval_exponential_notation() { | ||
let evaled = eval("1e5", None); | ||
assert_eq!(evaled, Ok(100000.0)); | ||
} | ||
#[test] | ||
fn eval_exponential_notation_decimal() { | ||
let evaled = eval("2.5e5", None); | ||
assert_eq!(evaled, Ok(250000.0)); | ||
} | ||
Comment on lines
+290
to
+294
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please try this too? |
||
#[test] | ||
fn eval_e_times_n() { | ||
let evaled = eval("e0", None); | ||
assert_eq!(evaled, Ok(0.)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can use
.next_if
instead.