Skip to content

Commit

Permalink
Update: use lexical for writing floating point values
Browse files Browse the repository at this point in the history
  • Loading branch information
Graham Enos committed Jun 2, 2022
1 parent 3df866d commit f8673bb
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 24 deletions.
74 changes: 68 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ categories = ["parser-implementations", "science", "compilers", "emulators"]
[dependencies]
dot-writer = { version = "0.1.2", optional = true }
indexmap = "1.6.1"
lexical = "5.2.0"
lexical = "6.1.1"
nom = "6.1.0"
num-complex = "0.4.0"
petgraph = "0.5.1"
ryu = "1.0.10"
serde = { version = "1.0.125", features = ["derive"] }
thiserror = "1.0.30"

Expand Down
24 changes: 19 additions & 5 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::collections::{hash_map::DefaultHasher, HashMap};
use std::f64::consts::PI;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::num::NonZeroI32;
use std::str::FromStr;

#[cfg(test)]
Expand Down Expand Up @@ -435,16 +436,29 @@ impl<'a> FromStr for Expression {
/// - When both are non-zero, show with the correct operator in between
#[inline(always)]
fn format_complex(value: &Complex64) -> String {
const FORMAT: u128 = lexical::format::STANDARD;
let options = lexical::WriteFloatOptions::builder()
.negative_exponent_break(NonZeroI32::new(-5))
.positive_exponent_break(NonZeroI32::new(15))
.trim_floats(true)
.build()
.unwrap();
if value.re == 0f64 && value.im == 0f64 {
"0".to_owned()
} else if value.im == 0f64 {
ryu::Buffer::new().format(value.re).to_owned()
lexical::to_string_with_options::<_, FORMAT>(value.re, &options)
} else if value.re == 0f64 {
ryu::Buffer::new().format(value.im).to_owned() + "i"
lexical::to_string_with_options::<_, FORMAT>(value.im, &options) + "i"
} else {
let mut buf = ryu::Buffer::new();
let op = if value.im > 0f64 { "+" } else { "" };
buf.format(value.re).to_owned() + op + buf.format(value.im) + "i"
let mut out = lexical::to_string_with_options::<_, FORMAT>(value.re, &options);
if value.im > 0f64 {
out.push_str("+")
}
out.push_str(&lexical::to_string_with_options::<_, FORMAT>(
value.im, &options,
));
out.push_str("i");
out
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ impl Instruction {
/// let mut instructions = program.to_instructions(true);
/// instructions.iter_mut().for_each(|inst| inst.apply_to_expressions(Expression::simplify));
///
/// assert_eq!(instructions[0].to_string(), String::from("SHIFT-PHASE 0 \"rf\" 4.0"))
/// assert_eq!(instructions[0].to_string(), String::from("SHIFT-PHASE 0 \"rf\" 4"))
///
/// ```
pub fn apply_to_expressions(&mut self, mut closure: impl FnMut(&mut Expression)) {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ mod tests {
let cases = vec![
"pi",
"sin(pi)",
"(1.0+(2.0*3.0))",
"((1.0+2.0)*3.0)",
"(1+(2*3))",
"((1+2)*3)",
"%theta",
"cis(%theta)",
"(%a+%b)",
Expand Down
10 changes: 5 additions & 5 deletions src/program/calibration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ mod tests {
"RX(pi/2) 0\n"
),
expected: concat!(
"PULSE 1 \"xy\" gaussian(duration: 1.0, fwhm: 2.0, t0: 3.0)\n",
"PULSE 2 \"xy\" gaussian(duration: 1.0, fwhm: 2.0, t0: 3.0)\n",
"PULSE 3 \"xy\" gaussian(duration: 1.0, fwhm: 2.0, t0: 3.0)\n"
"PULSE 1 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"PULSE 2 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"PULSE 3 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n"
),
},
TestCase {
Expand All @@ -364,7 +364,7 @@ mod tests {
" PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"X 0\n"
),
expected: "PULSE 0 \"xy\" gaussian(duration: 1.0, fwhm: 2.0, t0: 3.0)\n",
expected: "PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
},
TestCase {
input: concat!(
Expand All @@ -374,7 +374,7 @@ mod tests {
" PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"X 0\n"
),
expected: "PULSE 0 \"xy\" gaussian(duration: 1.0, fwhm: 2.0, t0: 3.0)\n",
expected: "PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
},
TestCase {
input: concat!(
Expand Down
6 changes: 3 additions & 3 deletions src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ DEFCAL I 0:
DEFFRAME 0 \"rx\":
HARDWARE-OBJECT: \"hardware\"
DEFWAVEFORM custom:
1.0, 2.0
1, 2
I 0
";
let program = Program::from_str(input).unwrap();
Expand All @@ -290,9 +290,9 @@ I 0
DEFFRAME 0 \"rx\":
\tHARDWARE-OBJECT: \"hardware\"
DEFWAVEFORM custom:
\t1.0, 2.0
\t1, 2
DEFCAL I 0:
\tDELAY 0 1.0
\tDELAY 0 1
I 0
"
);
Expand Down

0 comments on commit f8673bb

Please sign in to comment.