From 65f8985fe03c26fe33e2ba2138d4fb04403f96c1 Mon Sep 17 00:00:00 2001 From: Mike Boutin Date: Sun, 2 Jun 2019 14:14:27 -0400 Subject: [PATCH] Correct `Quantity::new` to be zero-cost for float storage types. Use a default constant of `-0.0` to allow for floating point optimizations. For a value, `v: Float`, adding `-0.0` is a no-op while adding `0.0` will change the sign if `v` is `-0.0`. Resolves #143. v 0.0 + -0.0 = 0.0 -0.0 + 0.0 = 0.0 # v + 0.0 != v -0.0 + -0.0 = -0.0 --- src/quantity.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/quantity.rs b/src/quantity.rs index b0ca6b27..1b124f07 100644 --- a/src/quantity.rs +++ b/src/quantity.rs @@ -493,5 +493,14 @@ macro_rules! quantity { (@coefficient $factor:expr, $const:expr) => { $factor }; (@coefficient $factor:expr) => { $factor }; (@constant $factor:expr, $const:expr) => { $const }; - (@constant $factor:expr) => { 0.0 }; + // Use a default constant of `-0.0` to allow for floating point optimizations. For a value, + // `v: Float`, adding `-0.0` is a no-op while adding `0.0` will change the sign if `v` is + // `-0.0`. + // + // v + // 0.0 + 0.0 = 0.0 + // 0.0 + -0.0 = 0.0 + // -0.0 + 0.0 = 0.0 # v + 0.0 != v + // -0.0 + -0.0 = -0.0 + (@constant $factor:expr) => { -0.0 }; }