Skip to content

Commit

Permalink
fix: use 'ryu' instead of 'lexical' for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
BatmanAoD committed Aug 9, 2024
1 parent f717bc1 commit 14e0be8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 117 deletions.
91 changes: 6 additions & 85 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion quil-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ categories = ["parser-implementations", "science", "compilers", "emulators"]
approx = { version = "0.5.1", features = ["num-complex"] }
dot-writer = { version = "0.1.2", optional = true }
itertools = "0.12.1"
lexical = "6.1.1"
ndarray.workspace = true
nom = "7.1.1"
nom_locate = "4.0.0"
Expand All @@ -26,6 +25,7 @@ strum.workspace = true
thiserror = "1.0.56"
indexmap.workspace = true
statrs = "0.16.0"
ryu = "1.0.18"

[dev-dependencies]
clap = { version = "4.3.19", features = ["derive", "string"] }
Expand Down
49 changes: 18 additions & 31 deletions quil-rs/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use crate::{
quil::Quil,
real,
};
use lexical::{format, to_string_with_options, WriteFloatOptions};
use nom_locate::LocatedSpan;
use num_complex::Complex64;
use once_cell::sync::Lazy;

Check warning on line 26 in quil-rs/src/expression/mod.rs

View workflow job for this annotation

GitHub Actions / Check (stable)

unused import: `once_cell::sync::Lazy`

Check failure on line 26 in quil-rs/src/expression/mod.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

unused import: `once_cell::sync::Lazy`

Check warning on line 26 in quil-rs/src/expression/mod.rs

View workflow job for this annotation

GitHub Actions / publish-docs

unused import: `once_cell::sync::Lazy`

Check warning on line 26 in quil-rs/src/expression/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

unused import: `once_cell::sync::Lazy`

Check warning on line 26 in quil-rs/src/expression/mod.rs

View workflow job for this annotation

GitHub Actions / Run benchmarks (macOS-latest, stable)

unused import: `once_cell::sync::Lazy`

Check warning on line 26 in quil-rs/src/expression/mod.rs

View workflow job for this annotation

GitHub Actions / Run benchmarks (ubuntu-latest, stable)

unused import: `once_cell::sync::Lazy`

Check warning on line 26 in quil-rs/src/expression/mod.rs

View workflow job for this annotation

GitHub Actions / Run benchmarks (windows-latest, stable)

unused import: `once_cell::sync::Lazy`
use ryu;
use std::{
collections::HashMap,
f64::consts::PI,
Expand Down Expand Up @@ -437,49 +437,36 @@ impl FromStr for Expression {
}
}

static FORMAT_REAL_OPTIONS: Lazy<WriteFloatOptions> = Lazy::new(|| {
WriteFloatOptions::builder()
.negative_exponent_break(NonZeroI32::new(-5))
.positive_exponent_break(NonZeroI32::new(15))
.trim_floats(true)
.build()
.expect("options are valid")
});

static FORMAT_IMAGINARY_OPTIONS: Lazy<WriteFloatOptions> = Lazy::new(|| {
WriteFloatOptions::builder()
.negative_exponent_break(NonZeroI32::new(-5))
.positive_exponent_break(NonZeroI32::new(15))
.trim_floats(false) // Per the quil spec, the imaginary part of a complex number is always a floating point number
.build()
.expect("options are valid")
});

/// Format a num_complex::Complex64 value in a way that omits the real or imaginary part when
/// reasonable. That is:
///
/// - When imaginary is set but real is 0, show only imaginary
/// - When imaginary is 0, show real only
/// - When both are non-zero, show with the correct operator in between
///
/// `ryu` formats exponents from -5 to 15 in standard notation, and larger magnitudes in scientific
/// notation.
#[inline(always)]
fn format_complex(value: &Complex64) -> String {
const FORMAT: u128 = format::STANDARD;
if value.re == 0f64 && value.im == 0f64 {
"0".to_owned()
} else if value.im == 0f64 {
to_string_with_options::<_, FORMAT>(value.re, &FORMAT_REAL_OPTIONS)
} else if value.re == 0f64 {
to_string_with_options::<_, FORMAT>(value.im, &FORMAT_IMAGINARY_OPTIONS) + "i"
ryu::Buffer::new().format(value.im).to_owned() + "i"
} else {
let mut out = to_string_with_options::<_, FORMAT>(value.re, &FORMAT_REAL_OPTIONS);
if value.im > 0f64 {
out.push('+')
let mut out = ryu::Buffer::new().format(value.re).to_owned();
match value.im {
0f64 => {}
im if im > 0f64 => {
out.push('+');
out.push_str(ryu::Buffer::new().format(im));
out.push('i');
}
im => {
// The `-` is included in the formatted output
out.push_str(ryu::Buffer::new().format(im));
out.push('i');
}
}
out.push_str(&to_string_with_options::<_, FORMAT>(
value.im,
&FORMAT_IMAGINARY_OPTIONS,
));
out.push('i');
out
}
}
Expand Down

0 comments on commit 14e0be8

Please sign in to comment.