From b7e7566c43a6e451586674d47defb6bd2b8abdef Mon Sep 17 00:00:00 2001 From: Sergii Piatakov Date: Thu, 15 Nov 2018 15:21:34 +0200 Subject: [PATCH] squashing two commits together ---- Commit 1 ---- improve calculation accuracy Allow using float point arguments to avoid truncation. Test: Add (4.9, 4.9) should return 10. Signed-off-by: Sergii Piatakov ---- Commit 2 ---- fix truncation error To convert float to integer the truncation is performed, but the rounding is expected. Test: Add (4.9, 4.9) should return 10. Signed-off-by: Sergii Piatakov Signed-off-by: IO-23 Oleh Shmuliar --- calculator.cpp | 6 +++--- calculator.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/calculator.cpp b/calculator.cpp index 3c63184..d93e35b 100644 --- a/calculator.cpp +++ b/calculator.cpp @@ -1,11 +1,11 @@ #include "calculator.h" -int Calculator::Add (int a, int b) +int Calculator::Add (double a, double b) { - return a + b; + return a + b + 0.5; } -int Calculator::Sub (int a, int b) +int Calculator::Sub (double a, double b) { return Add (a, -b); } diff --git a/calculator.h b/calculator.h index 84ff414..3740907 100644 --- a/calculator.h +++ b/calculator.h @@ -4,8 +4,8 @@ class Calculator { public: - int Add (int, int); - int Sub (int, int); + int Add (double, double); + int Sub (double, double); }; #endif//CALCULATOR_H