Account for floating point inaccuracy in histogram calculation

finchie
Apr 29, 2024, 11:30 AM
2H6RJC35PGFYV7XDCQYOA45MU4GPJAEYHEJKP4KFZ3YIQVJP6WBAC

Dependencies

  • [2] ZEN3WUPD Add support for histogram charts

Change contents

  • edit in src/visualize/histogram.rs at line 6
    [2.945]
    [2.945]
    const DECIMAL_PLACES: usize = 50;
  • replacement in src/visualize/histogram.rs at line 60
    [2.2895][2.2895:3004]()
    assert_eq!(next_multiple, next_multiple.floor());
    let bucket_index = next_multiple as usize;
    [2.2895]
    [2.3004]
    let float_error_margin = 2_f64.powf(1_f64 - (DECIMAL_PLACES as f64));
    assert!((next_multiple - next_multiple.round()).abs() <= float_error_margin);
    // assert_eq!(next_multiple, next_multiple.round());
    let bucket_index = (next_multiple.round()) as usize;
  • replacement in src/visualize/histogram.rs at line 66
    [2.3063][2.3063:3099]()
    buckets[bucket_index] += 1;
    [2.3063]
    [2.3099]
    if bucket_index == BUCKET_COUNT {
    // The max duration should be the only one in this final bucket
    assert_eq!(duration, max_duration);
    buckets[bucket_index - 1] += 1;
    } else {
    // Every other duration should hit this case instead
    buckets[bucket_index] += 1;
    }