Skip to content

Commit 9421e01

Browse files
committed
Added decimal place moving for negative numbers.
1 parent bc90a70 commit 9421e01

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ dist/
8383
downloads/
8484
eggs/
8585
.eggs/
86-
lib/
8786
lib64/
8887
parts/
8988
sdist/

lib/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ if (NOT DEFINED Python_EXECUTABLE)
2727
Python
2828
COMPONENTS Development Interpreter
2929
REQUIRED)
30-
else ()
31-
set(Python_EXECUTABLE ${PYTHON_EXECUTABLE})
30+
find_package(
31+
Python3
32+
COMPONENTS Development Interpreter
33+
REQUIRED)
3234
endif ()
35+
set(Python_EXECUTABLE ${Python3_EXECUTABLE})
3336

3437
# Detect the installed nanobind package and import it into CMake
3538
execute_process(

src/multiply/multiply.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,18 @@ namespace steppable::__internals::arithmetic
5252
auto b = static_cast<std::string>(_b);
5353
const auto& [splitNumberArray, aIsNegative, bIsNegative] = splitNumber(a, b, false, false);
5454
bool resultIsNegative = false;
55+
56+
// Determine the polarity of the result.
57+
if (aIsNegative and bIsNegative)
58+
resultIsNegative = false; // NOLINT(bugprone-branch-clone)
59+
else if (aIsNegative or bIsNegative)
60+
resultIsNegative = true;
61+
else
62+
resultIsNegative = false;
63+
5564
const auto& [aInteger, aDecimal, bInteger, bDecimal] = splitNumberArray;
5665
std::stringstream out;
66+
// Multiply by zero gives zero.
5767
if (isZeroString(a) or isZeroString(b))
5868
{
5969
if (steps == 2)
@@ -82,14 +92,20 @@ namespace steppable::__internals::arithmetic
8292
{
8393
if (steps == 2)
8494
out << "Since " << a << " is a power of 10, we can move the decimal places to obtain the result.\n";
85-
out << moveDecimalPlaces(b, determineScale(a));
95+
auto result = moveDecimalPlaces(b, determineScale(a));
96+
if (resultIsNegative)
97+
result = "-" + result; // NOLINT(performance-inefficient-string-concatenation)
98+
99+
out << result;
86100
return out.str();
87101
}
88102
if (isPowerOfTen(b))
89103
{
90104
if (steps == 2)
91105
out << "Since " << b << " is a power of 10, we can move the decimal places to obtain the result.\n";
92-
out << moveDecimalPlaces(a, determineScale(b));
106+
auto result = moveDecimalPlaces(a, determineScale(b));
107+
108+
out << result;
93109
return out.str();
94110
}
95111

@@ -98,13 +114,6 @@ namespace steppable::__internals::arithmetic
98114
std::vector<std::vector<int>> prodDigits;
99115
std::vector<std::vector<int>> carries;
100116

101-
if (aIsNegative and bIsNegative)
102-
resultIsNegative = false; // NOLINT(bugprone-branch-clone)
103-
else if (aIsNegative or bIsNegative)
104-
resultIsNegative = true;
105-
else
106-
resultIsNegative = false;
107-
108117
for (size_t indexB = 0; indexB < bStr.length(); indexB++)
109118
{
110119
const int bDigit = static_cast<int>(bStr[indexB]) - '0';

src/rounding.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ namespace steppable::__internals::numUtils
128128
auto splitNumberArray = splitNumberResult.splitNumberArray;
129129
auto integer = splitNumberArray[0];
130130
auto decimal = splitNumberArray[1];
131-
bool isNegative = splitNumberResult.aIsNegative;
132131
auto repetitions = std::abs(places);
133132

134133
// Move decimal places to the right
@@ -157,8 +156,6 @@ namespace steppable::__internals::numUtils
157156
}
158157

159158
auto result = integer + "." + decimal;
160-
if (isNegative)
161-
result = '-' + result;
162159
return standardizeNumber(removeLeadingZeros(result));
163160
}
164161
} // namespace steppable::__internals::numUtils

src/util.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "util.hpp"
2424

2525
#include <cstddef>
26-
#include <map>
2726

2827
#ifdef WINDOWS
2928
#undef min
@@ -200,7 +199,17 @@ namespace steppable::__internals::numUtils
200199
if (string == "0")
201200
return "0";
202201
auto out = string;
203-
out.erase(0, std::min(out.find_first_not_of('0'), out.size() - 1));
202+
bool negative = false;
203+
if (out.front() == '-')
204+
{
205+
negative = true;
206+
out.erase(0, 1);
207+
}
208+
while (out.front() == '0')
209+
out.erase(0, 1);
210+
211+
if (negative)
212+
out.insert(0, 1, '-');
204213
return out;
205214
}
206215

0 commit comments

Comments
 (0)