Skip to content

Commit

Permalink
Correct Quantity::new to be zero-cost for float storage types.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
iliekturtles committed Jun 2, 2019
1 parent c02d306 commit 65f8985
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

0 comments on commit 65f8985

Please sign in to comment.