Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Refer to LICENCE for more information.
* */

use num::pow;
use once_cell::sync::Lazy;
use std::collections::HashMap;

Expand Down Expand Up @@ -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;
}
Comment on lines +247 to +250
Copy link
Collaborator

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.


num_vec.push(*next);
chars.next();
}

if num_vec.len() > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if num_vec.len() > 0 {
if !num_vec.is_empty() {

if let Some(exp) = num_vec.parse::<usize>().ok() {
result.push(Token::Num(x * pow(10 as f64, exp) as f64));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't using num::pow directly be better?

Suggested change
result.push(Token::Num(x * pow(10 as f64, exp) as f64));
result.push(Token::Num(x * num::pow(10 as f64, exp) as f64));

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;
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please try this too? 5e5e5.

#[test]
fn eval_e_times_n() {
let evaled = eval("e0", None);
assert_eq!(evaled, Ok(0.));
Expand Down