Skip to content

Commit 5331142

Browse files
committed
fix removing extra zeros and value types
default compiler and includes have been fixed too
1 parent 7c071e3 commit 5331142

6 files changed

Lines changed: 34 additions & 24 deletions

File tree

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ CP := cp -ruv
1414

1515

1616
# Compiler
17-
CXX = g++
17+
#CXX = g++
1818
CXXFLAGS = -Wall -Wextra -Wpedantic
19-
LIBS =
20-
LDFLAGS =
19+
#LIBS =
20+
#LDFLAGS =
21+
2122

2223
# Main target
2324
TARGET := $(BIN)/$(PROJECT)

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ overloaded. Also, the `std::string` conversion operator is overloaded.
1212

1313
## Build
1414

15-
Use the [`Makefile`] file to build the binary at `bin/bigfib`.
15+
Use the [`Makefile`] file to build the binary. There are some targets available:
1616

17-
`g++` is configured as the compiler by default, you can change it modifying the
18-
[`CXX`] variable if you want.
17+
- `release`: The default target generates de optimized binary at `bin/bigfib`.
18+
- `debug`: Generates the binary for debugging with `gdb`.
19+
- `clean`: Removes all the compilation files.
20+
21+
Feel free to update the [`CXX`], [`CXXFLAGS`], [`LIBS`] and [`LDFLAGS`]
22+
variables if you need.
1923

2024

2125
## CLI usage
@@ -45,4 +49,7 @@ Share and enjoy!
4549
[`bigint`]: src/bigint.hpp
4650
[`Makefile`]: Makefile
4751
[`CXX`]: Makefile#L17
52+
[`CXXFLAGS`]: Makefile#L18
53+
[`LIBS`]: Makefile#L19
54+
[`LDFLAGS`]: Makefile#L20
4855
[LICENSE]: LICENSE

src/bigint.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Naive multiplication
1010
std::vector<ull> bigint::_NAIVE_MULT(const std::vector<ull> &a, const std::vector<ull> &b) {
1111
std::size_t len = a.size();
12-
std::vector<ull> r(len << 1);
12+
std::vector<ull> r(len << 1ULL);
1313

1414
std::vector<ull>::const_iterator aend = a.cend();
1515
std::vector<ull>::const_iterator bend = b.cend();
@@ -39,7 +39,7 @@ std::vector<ull> bigint::_NAIVE_MULT(const std::vector<ull> &a, const std::vecto
3939
std::vector<ull> bigint::_KARATSUBA_MULT(const std::vector<ull> &a, const std::vector<ull> &b) {
4040
const std::size_t len = a.size();
4141

42-
if (len <= 32) {
42+
if (len <= static_cast<std::size_t>(32)) {
4343
return bigint::_NAIVE_MULT(a, b);
4444
}
4545

@@ -64,7 +64,7 @@ std::vector<ull> bigint::_KARATSUBA_MULT(const std::vector<ull> &a, const std::v
6464
const std::vector<ull>::const_iterator bli = bl.cbegin();
6565
const std::vector<ull>::const_iterator bri = br.cbegin();
6666

67-
for (std::size_t i = 0; i < mid; ++i) {
67+
for (std::size_t i = static_cast<std::size_t>(0); i < mid; ++i) {
6868
*(alri + i) = *(ali + i) + *(ari + i);
6969
*(blri + i) = *(bli + i) + *(bri + i);
7070
}
@@ -77,7 +77,7 @@ std::vector<ull> bigint::_KARATSUBA_MULT(const std::vector<ull> &a, const std::v
7777
const std::vector<ull>::iterator p3i = p3.begin();
7878
const std::vector<ull>::iterator ri = r.begin();
7979

80-
for (std::size_t i = 0; i < len; ++i) {
80+
for (std::size_t i = static_cast<std::size_t>(0); i < len; ++i) {
8181
const ull p2ii = *(p2i + i);
8282
*(p3i + i) -= p2ii + *(p1i + i);
8383
*(ri + i) = p2ii;
@@ -225,7 +225,7 @@ const bigint operator - (const bigint &a, const bigint &b) {
225225
for (std::vector<ull>::const_reverse_iterator i = r.crbegin(); *i == 0ULL; ++i, ++c);
226226

227227
if (c != 0ULL) {
228-
r.resize(std::max(r.size() - static_cast<std::size_t>(c), static_cast<std::size_t>(1)));
228+
r.resize(r.size() > static_cast<std::size_t>(c) ? r.size() - static_cast<std::size_t>(c) : static_cast<std::size_t>(1));
229229
}
230230

231231
return r;
@@ -264,7 +264,7 @@ const bigint operator * (const bigint &a, const bigint &b) {
264264
for (std::vector<ull>::const_reverse_iterator i = r.crbegin(); *i == 0ULL; ++i, ++extra);
265265

266266
if (extra != static_cast<std::size_t>(0)) {
267-
r.resize(std::max(r.size() - extra, static_cast<std::size_t>(1)));
267+
r.resize(r.size() > extra ? r.size() - extra : static_cast<std::size_t>(1));
268268
}
269269

270270
return r;

src/bigint.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class bigint {
5353
/**
5454
* Default constructor
5555
*/
56-
inline bigint() : _data(1, 0) {};
56+
inline bigint() : _data(static_cast<std::size_t>(1), 0ULL) {};
5757

5858
/**
5959
* Big integer copy constructor
@@ -67,7 +67,7 @@ class bigint {
6767
*
6868
* @param n Initializator
6969
*/
70-
inline bigint(const ull &n) : _data(1, n) {};
70+
inline bigint(const ull &n) : _data(static_cast<std::size_t>(1), n) {};
7171

7272

7373
/**

src/fib.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <sstream>
66
#include <chrono>
77

8+
89
/** Get current clock time */
910
#define NOW std::chrono::high_resolution_clock::now
1011

@@ -23,20 +24,20 @@ typedef std::chrono::high_resolution_clock::time_point time_point;
2324

2425
// Calculate Fibonacci number
2526
bigint Fibonacci::calc() {
26-
if (n == 0) {
27-
return 0;
27+
if (n == 0ULL) {
28+
return 0ULL;
2829
}
29-
else if (n < 3) {
30-
return 1;
30+
else if (n < 3ULL) {
31+
return 1ULL;
3132
}
3233

33-
ull h = 0;
34-
bigint a = 0;
35-
bigint b = 1;
34+
ull h = 0ULL;
35+
bigint a = 0ULL;
36+
bigint b = 1ULL;
3637

3738
for (ull i = n; i; ++h, i >>= 1ULL);
3839

39-
for (ull m = 1 << (h - 1); m; m >>= 1) {
40+
for (ull m = 1ULL << (h - 1ULL); m; m >>= 1ULL) {
4041
bigint c = a * (b + b - a);
4142
bigint d = a * a + b * b;
4243

@@ -109,4 +110,4 @@ void Fibonacci::print(const bool &summary, const bool &number) const {
109110
<< "Total time: " << FORMAT << this->_calc_ms + this->_cast_ms + cout_ms << " ms"
110111
<< std::endl;
111112
}
112-
};
113+
}

src/ui.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
#include <iostream>
55
#include <cstring>
6+
#include <stdexcept>
67

78

89
/** Version */
9-
#define VERSION "2.0"
10+
#define VERSION "2.1"
1011

1112

1213
// Private methods

0 commit comments

Comments
 (0)