Skip to content

Commit

Permalink
Fix Windows FP discrepancy issue in test (#400)
Browse files Browse the repository at this point in the history
* Fix Windows FP discrepancy issue in test

* Write changelog
  • Loading branch information
cyqsimon authored Apr 15, 2024
1 parent 86a17cb commit 2ac352d
Show file tree
Hide file tree
Showing 3 changed files with 761 additions and 751 deletions.
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

0 comments on commit 2ac352d

Please sign in to comment.