lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] (no subject)


From: Greg Chicares
Subject: [lmi-commits] (no subject)
Date: Sat, 18 Jun 2016 22:44:43 +0000 (UTC)

branch: master
commit 24159ca328a05e9229fd029318298c778fa39c5c
Author: Gregory W. Chicares <address@hidden>
Date:   Sat Jun 18 22:43:11 2016 +0000

    Write inequality comparisons in number-line order
    
    This is the style consistently followed elsewhere in lmi. Integer
    division by 100 has this invariant in customary mathematical notation:
      0 <= remainder < 100
    Because C and C++ have no ternary "operator < <", the closest
    possible code is:
      0 <= remainder && remainder < 100
    and a violation occurs if that condition is not true, i.e.:
      !(0 <= remainder && remainder < 100)
---
 currency.hpp |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/currency.hpp b/currency.hpp
index eb61756..6ff4ceb 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -79,12 +79,12 @@ class currency
 
     currency(amount_type dollars, int cents)
         {
-        if(dollars < 0 || dollars >= max_dollars())
+        if(!(0 <= dollars && dollars < max_dollars()))
             {
             throw std::overflow_error("Currency amount out of range.");
             }
 
-        if(cents < 0 || cents >= cents_per_dollar)
+        if(!(0 <= cents && cents < cents_per_dollar))
             {
             throw std::runtime_error("Invalid number of cents.");
             }
@@ -99,7 +99,7 @@ class currency
 
     static currency from_value(double d)
         {
-        if(std::trunc(d) >= static_cast<double>(max_dollars()))
+        if(static_cast<double>(max_dollars()) <= std::trunc(d))
             {
             throw std::overflow_error("Currency amount out of range.");
             }
@@ -277,7 +277,7 @@ inline std::istream& operator>>(std::istream& is, currency& 
c)
         return is;
         }
 
-    if(cents < 0 || cents >= currency::cents_per_dollar)
+    if(!(0 <= cents && cents < currency::cents_per_dollar))
         {
         is.setstate(std::ios_base::failbit);
         return is;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]