Skip to content

Commit 521ba68

Browse files
committed
More accurate factors for meter to inch, feet, yard, furlong, mile.
Previously the expressions included 1000.0 / 25.4, but 25.4 is not exactly representable in floating-point numbers, and thus the results were not always the nearest representable numbers. Switching to 10000.0 / 254.0 fixes this as whole numbers are exactly representable.
1 parent bc261d1 commit 521ba68

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/length.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ pub const METER_KILOMETER_FACTOR: f64 = 0.001;
2222
// Constants, imperial
2323

2424
/// Number of inches in a meter
25-
pub const METER_INCH_FACTOR: f64 = 1000.0 / 25.4;
25+
pub const METER_INCH_FACTOR: f64 = 10000.0 / 254.0;
2626
/// Number of feet in a meter
27-
pub const METER_FEET_FACTOR: f64 = 1000.0 / (25.4 * 12.0);
27+
pub const METER_FEET_FACTOR: f64 = 10000.0 / (254.0 * 12.0);
2828
/// Number of yards in a meter
29-
pub const METER_YARD_FACTOR: f64 = 1000.0 / (25.4 * 12.0 * 3.0);
29+
pub const METER_YARD_FACTOR: f64 = 10000.0 / (254.0 * 12.0 * 3.0);
3030
/// Number of furlongs in a meter
31-
pub const METER_FURLONG_FACTOR: f64 = 1000.0 / (25.4 * 12.0 * 3.0 * 220.0);
31+
pub const METER_FURLONG_FACTOR: f64 = 10000.0 / (254.0 * 12.0 * 3.0 * 220.0);
3232
/// Number of miles in a meter
33-
pub const METER_MILE_FACTOR: f64 = 1000.0 / (25.4 * 12.0 * 3.0 * 1760.0);
33+
pub const METER_MILE_FACTOR: f64 = 10000.0 / (254.0 * 12.0 * 3.0 * 1760.0);
3434

3535
/// The Length struct can be used to deal with lengths in a common way.
3636
/// Common metric and imperial units are supported.

0 commit comments

Comments
 (0)