Skip to content

Commit

Permalink
Fix: parse and print real values as double precision
Browse files Browse the repository at this point in the history
  • Loading branch information
notmgsk committed Jul 22, 2021
1 parent 78cff3d commit 4b8b923
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
13 changes: 4 additions & 9 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,17 @@ macro_rules! format_complex {

if $value.im > 0f64 {
operator = "+".to_owned();
imaginary_component = format!("{}i", pretty_float!($value.im))
imaginary_component = format!("{:.}i", $value.im)
} else if $value.im < 0f64 {
imaginary_component = format!("-{}i", pretty_float!($value.im))
imaginary_component = format!("-{:.}i", $value.im)
}

if imaginary_component == "" {
pretty_float!($value.re)
format!("{:.}", $value.re)
} else if $value.re == 0f64 {
format!("{}", imaginary_component)
} else {
format!(
"{}{}{}",
pretty_float!($value.re),
operator,
imaginary_component
)
format!("{:.}{}{}", $value.re, operator, imaginary_component)
}
}};
}
Expand Down
6 changes: 3 additions & 3 deletions src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use nom::{
character::complete::{digit1, one_of},
combinator::{all_consuming, map, recognize, value},
multi::many0,
number::complete::float,
number::complete::double,
sequence::{delimited, preceded, terminated, tuple},
IResult,
};
Expand Down Expand Up @@ -287,13 +287,13 @@ fn lex_non_blocking(input: &str) -> LexResult {
}

fn lex_number(input: &str) -> LexResult {
let (input, float_string): (&str, &str) = recognize(float)(input)?;
let (input, float_string): (&str, &str) = recognize(double)(input)?;
let integer_parse_result: IResult<&str, _> = all_consuming(digit1)(float_string);
Ok((
input,
match integer_parse_result {
Ok(_) => Token::Integer(float_string.parse::<u64>().unwrap()),
Err(_) => Token::Float(float(float_string)?.1 as f64),
Err(_) => Token::Float(double(float_string)?.1 as f64),
},
))
}
Expand Down

0 comments on commit 4b8b923

Please sign in to comment.