From: Ira W. Snyder Date: Fri, 5 Oct 2007 18:26:17 +0000 (-0700) Subject: Fix underflow in Position X-Git-Url: https://irasnyder.com/gitweb/?a=commitdiff_plain;h=83a0692b324ab753ba9678b0d136a85fcca13930;p=cs356-p1-elevator.git Fix underflow in Position The operator-=() function in Position could underflow the part right of the decimal point. Fix it. Signed-off-by: Ira W. Snyder --- diff --git a/position.cpp b/position.cpp index f1cfc90..e0da0f4 100644 --- a/position.cpp +++ b/position.cpp @@ -44,7 +44,18 @@ Position& Position::operator+= (const float rhs) Position& Position::operator-= (const float rhs) { - *this += -rhs; + int major = (int)rhs; + int minor = (int)((rhs - major) * 10); + + major_ -= major; + minor_ -= minor; + + /* Check for underflow */ + if (minor_ < 0) + { + major_ -= 1; + minor_ += 10; + } return *this; }