Compare commits

...

3 Commits

Author SHA1 Message Date
rhinemann bc54f7ecab Added Pow operation 2024-10-10 19:13:51 +03:00
rhinemann 99af5da377 add a multiplication operation
Add a member-function which performs multiplication of two float point
digits.

Signed-off-by: Sergii Piatakov <sergii.piatakov@globallogic.com>
2024-10-10 18:43:08 +03:00
Sergii Piatakov bffd2aad57 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 <sergii.piatakov@globallogic.com>
2024-10-10 18:36:33 +03:00
4 changed files with 95 additions and 1 deletions

View File

@ -2,10 +2,24 @@
int Calculator::Add (double a, double b) int Calculator::Add (double a, double b)
{ {
return a + b; return a + b + 0.5;
} }
int Calculator::Sub (double a, double b) int Calculator::Sub (double a, double b)
{ {
return Add (a, -b); return Add (a, -b);
} }
int Calculator::Mul (double a, double b)
{
return a * b + 0.5;
}
double Calculator::Pow(double a, int n)
{
double result = 1;
for (int i = 0; i < n; i++) {
result *= a;
}
return result;
}

View File

@ -6,6 +6,8 @@ class Calculator
public: public:
int Add (double, double); int Add (double, double);
int Sub (double, double); int Sub (double, double);
int Mul (double, double);
double Pow (double, int);
}; };
#endif//CALCULATOR_H #endif//CALCULATOR_H

View File

@ -0,0 +1,30 @@
From 49150debe027e79690fd713db43e17e4f0b7341f Mon Sep 17 00:00:00 2001
From: Sergii Piatakov <sergii.piatakov@globallogic.com>
Date: Thu, 15 Nov 2018 15:28:04 +0200
Subject: [PATCH] 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 <sergii.piatakov@globallogic.com>
---
calculator.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/calculator.cpp b/calculator.cpp
index f323e14..d10f529 100644
--- a/calculator.cpp
+++ b/calculator.cpp
@@ -2,7 +2,7 @@
int Calculator::Add (double a, double b)
{
- return a + b;
+ return a + b + 0.5;
}
int Calculator::Sub (double a, double b)
--
2.43.0

View File

@ -0,0 +1,48 @@
From b239ff832843c03fafeef255c21fc586645fa4bd Mon Sep 17 00:00:00 2001
From: Sergii Piatakov <sergii.piatakov@globallogic.com>
Date: Thu, 15 Nov 2018 15:26:35 +0200
Subject: [PATCH] formatting: use tabs instead of spaces
Signed-off-by: Sergii Piatakov <sergii.piatakov@globallogic.com>
---
calculator.cpp | 4 ++--
calculator.h | 6 +++---
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/calculator.cpp b/calculator.cpp
index b91afea..f323e14 100644
--- a/calculator.cpp
+++ b/calculator.cpp
@@ -2,10 +2,10 @@
int Calculator::Add (double a, double b)
{
- return a + b;
+ return a + b;
}
int Calculator::Sub (double a, double b)
{
- return Add (a, -b);
+ return Add (a, -b);
}
diff --git a/calculator.h b/calculator.h
index 3740907..d59d596 100644
--- a/calculator.h
+++ b/calculator.h
@@ -3,9 +3,9 @@
class Calculator
{
- public:
- int Add (double, double);
- int Sub (double, double);
+ public:
+ int Add (double, double);
+ int Sub (double, double);
};
#endif//CALCULATOR_H
--
2.43.0