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

Fix Windows FP discrepancy issue in test #400

Merged
merged 2 commits into from
Apr 15, 2024
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* Remove unnecessary logging synchronisation #381 - @cyqsimon
* Apply suggestions from new clippy lint clippy::assigning_clones #382 - @cyqsimon
* Fix IPv6 socket detect logic #383 - @cyqsimon
* Support build for `target_os`` `android` #384 - @flxo
* Support build for `target_os` `android` #384 - @flxo
* Fix Windows FP discrepancy issue in test #400 - @cyqsimon

## Added

Expand Down
12 changes: 11 additions & 1 deletion src/display/components/display_bandwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use derivative::Derivative;

use crate::cli::UnitFamily;

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Derivative)]
#[derivative(Debug)]
pub struct DisplayBandwidth {
#[derivative(Debug(format_with = "fmt_f64"))]
pub bandwidth: f64,
pub unit_family: BandwidthUnitFamily,
}
Expand All @@ -17,6 +19,14 @@ impl fmt::Display for DisplayBandwidth {
}
}

/// Custom formatter with reduced precision.
///
/// Workaround for FP calculation discrepancy between Unix and Windows.
/// See https://github.com/rust-lang/rust/issues/111405#issuecomment-2055964223.
fn fmt_f64(val: &f64, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{val:.10e}")
}

/// Type wrapper around [`UnitFamily`] to provide extra functionality.
#[derive(Copy, Clone, Derivative, Default, Eq, PartialEq)]
#[derivative(Debug = "transparent")]
Expand Down
Loading
Loading