Skip to content

Commit e07d57d

Browse files
committed
Bug Fix: pow(a,1) is incorrect
1 parent 8b9ab0a commit e07d57d

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

bigintegermath.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ BigInteger BigIntegerMath::pow(const BigInteger &base, int exponent)
1111
{
1212
BigInteger result;
1313

14-
if (exponent <= 1) {
14+
if (exponent < 1) {
1515
// It don't support floating point
1616
result = 1;
1717
return result;
18+
} else if (exponent == 1) {
19+
return base;
1820
}
1921

2022
BigInteger x = base;
@@ -86,6 +88,10 @@ void BigIntegerMath::fraction(double input, BigInteger &numerator, BigInteger &d
8688
QString integerPart = str.mid(0, matcher.capturedStart());
8789
denominator = pow(BigInteger(10), decimal.size());
8890

91+
if (integerPart == "0") {
92+
integerPart = "";
93+
}
94+
8995
for (int i = 0 ; i < decimal.size() ; i++) {
9096
integerPart += decimal.at(i);
9197
}
@@ -100,3 +106,11 @@ void BigIntegerMath::fraction(double input, BigInteger &numerator, BigInteger &d
100106
}
101107

102108
}
109+
110+
111+
QDebug operator<<(QDebug d, const BigInteger &bigInteger)
112+
{
113+
d << QString::fromStdString(bigIntegerToString(bigInteger));
114+
115+
return d;
116+
}

bigintegermath.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef BIGINTEGERMATH_H
22
#define BIGINTEGERMATH_H
33

4+
#include <QtCore>
45
#include "BigInteger.hh"
56

67
/// Math utility
@@ -16,4 +17,6 @@ class BigIntegerMath
1617
static void fraction(double decimal, BigInteger& numerator, BigInteger& denominator);
1718
};
1819

20+
QDebug operator<< (QDebug d, const BigInteger &bigInteger);
21+
1922
#endif // BIGINTEGERMATH_H

bigintegerobject.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,3 @@ class BigIntegerWrapperRegistrationHelper {
214214
};
215215

216216
static BigIntegerWrapperRegistrationHelper registerHelper;
217-
218-
QDebug operator<<(QDebug d, const BigInteger &bigInteger)
219-
{
220-
d << QString::fromStdString(bigIntegerToString(bigInteger));
221-
222-
return d;
223-
}

bigintegerobject.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22
#include <QObject>
33
#include <QJSValue>
4-
#include "BigInteger.hh"
54

65
// QML Wrapper Object for BigInteger manipulation
76

@@ -50,5 +49,3 @@ public slots:
5049
QString pow(const QString& a, int b) const;
5150

5251
};
53-
54-
QDebug operator<< (QDebug d, const BigInteger &bigInteger);

tests/bigintegerunittests/bigintegerunittests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ void BigIntegerUnitTests::pow()
7878
result = QString::fromStdString(bigIntegerToString(c));
7979
QVERIFY(result == "647225717667958234512676373328684966608135637121798638546825574314018838197362232702277832316406382792759851833889013515631314023038087");
8080

81+
a = 10;
82+
b = 1;
83+
c = BigIntegerMath::pow(a, b);
84+
result = QString::fromStdString(bigIntegerToString(c));
85+
QVERIFY(result == "10");
86+
8187
}
8288

8389
void BigIntegerUnitTests::gcd()
@@ -148,4 +154,6 @@ void BigIntegerUnitTests::fraction_data()
148154
QTest::newRow("") << 1.07 << "107" << "100";
149155
QTest::newRow("") << -1.07 << "-107" << "100";
150156
QTest::newRow("") << 0.0 << "0" << "1";
157+
QTest::newRow("") << 0.8 << "4" << "5";
158+
151159
}

tests/bigintegerunittests/qml/tst_calculation.qml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ TestCase {
99
compare(BigInteger.multiply("3141592653589793238462643383279","3141592653589793238462643383279"), "9869604401089358618834490999872991420714831834895839696791841");
1010
compare(BigInteger.multiply("134",[ "5", "7"]), "95");
1111
compare(BigInteger.multiply("134", 0.714285 ), "95");
12+
compare(BigInteger.multiply("10", [8,10]), "8");
13+
compare(BigInteger.multiply("10", 0.8 ), "8");
1214
}
1315

1416
function test_divide() {
@@ -50,6 +52,8 @@ TestCase {
5052

5153
function test_fraction() {
5254
compare(BigInteger.fraction("0.125") , ["1","8"]);
55+
compare(BigInteger.fraction("0.8") , ["4","5"]);
56+
5357
}
5458

5559
}

0 commit comments

Comments
 (0)