Skip to content

Commit

Permalink
Final fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
anOsuPlayer committed Jun 2, 2024
1 parent dabde44 commit e1a5581
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
55 changes: 32 additions & 23 deletions bignum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <iostream>
#include <string>
#include <vector>
#include <bitset>

typedef signed char digit_t;
Expand Down Expand Up @@ -173,6 +174,10 @@
small.sign = !small.sign;
}

if (greater.nlen == 1 && greater[0] == 0) {
greater.sign = false;
}

return greater;
}

Expand Down Expand Up @@ -266,28 +271,34 @@
*this = *this * n;
}

friend big_num operator / (const big_num& n1, const big_num& n2) {
if (n1 == 0) {
return 0;
}
if (n2 == 0) {
std::cout << "Illegal division by 0\n";
return 0;
friend big_num operator / (big_num& n1, const big_num& n2) {
if(n2 == 0) {
std::cout << "impossible ivision By 0";
}

bool sign = n1.sign ^ n2.sign;

if (!n2 > !n1) { return 0; }
if (!n2 == !n1) { return 1; }

big_num result = 0, accumulator, factor = !n2;

for (accumulator = 0; (accumulator + factor) <= n1; accumulator += factor) {
result++;
if(n1 < n2){
return big_num(0);
}

result.sign = sign;
return result;
if(n1 == n2){
return big_num(1);
}
int i, lgcat = 0, cc;
int n = n1.nlen, m = n2.nlen;
std::vector<int> cat(n, 0);
big_num t;
for (i = n - 1; t * 10 + n1.digits[i] < n2; i--){
t *= 10;
t += n1.digits[i] ;
}
for (; i >= 0; i--){
t = t * 10 + n1.digits[i];
for (cc = 9; cc * n2 > t;cc--);
t -= cc * n2;
cat[lgcat++] = cc;
}
for (i = 0; i < lgcat;i++)
n1.digits[i] = cat[lgcat - i - 1];
n1.resize(lgcat-cat.size());
return n1;
}

void operator /= (const big_num& n) {
Expand Down Expand Up @@ -490,12 +501,10 @@
big_num copy(num);
for (int i = 0; i < size; i++) {
if ((copy % 2) == 1) {
copy--;
set.set(i);
}
copy /= 2;
if (copy == 0) {
break;
}
}

return set;
Expand Down
8 changes: 4 additions & 4 deletions nfloat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
exp -= _bias(size);

big_num integral = 0, decimal = 1;
big_num int_factor = 2 ^ !(exp), dec_factor = 5;
big_num int_factor = 2 ^ !(exp), dec_factor = 1;

if (exp < 0) {
for (big_num n = exp; n < -1; n++) {
Expand All @@ -182,9 +182,6 @@
decimal *= 10;
dec_factor *= 5;
}
else {
int_factor /= 2;
}
if (i == -1 || this->body()[mant_size-1-i] == 1) {
if (exp >= 0) {
integral += int_factor;
Expand All @@ -194,6 +191,9 @@

}
}
else {
int_factor /= 2;
}
exp--;
}

Expand Down
5 changes: 3 additions & 2 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

int main() {

big_num n = 125123412313123;
nfloat<32> f(1.51f);

std::cout << to_bitset<500>(n);
std::cout << f << "\n";
std::cout << f.to_string(500);
}

0 comments on commit e1a5581

Please sign in to comment.