This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_fat_operator.c
executable file
·112 lines (103 loc) · 2.32 KB
/
helper_fat_operator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
** helper_fat_operator.c for Bistromatique in /home/barrau_h/Desktop/Bistromatique FINAL/Bistromatique
**
** Made by Hippolyte Barraud
** Login <barrau_h@epitech.net>
**
** Started on Sun Nov 9 04:13:52 2014 Hippolyte Barraud
** Last update Sat Nov 15 02:37:09 2014 Hippolyte Barraud
*/
#include"include/bistromatique.h"
/*
** Schoolbook multiplication. The most time-demanding operations. Could be
** better. There are larges rooms for improvements.
*/
t_fatnum *multiply(t_fatnum *a, t_fatnum *b, t_fatnum *c)
{
static t_fatnum row;
static t_fatnum tmp;
register int i;
register int j;
init_fatnum(c);
fatnum_dup(a, &row);
i = 0;
while (i <= b->len)
{
j = 1;
while (j <= b->digits[i])
{
add(c, &row, &tmp);
fatnum_dup(&tmp, c);
j++;
}
digit_shift(&row, 1);
i++;
}
c->signbit = a->signbit * b->signbit;
format_fatnum(c);
return (c);
}
/*
** Helper function. Helps comply with the norm.
*/
inline t_fatnum *divide_helper(t_fatnum *a, t_fatnum *b, t_fatnum *c,
t_fatnum row, t_fatnum tmp, int asign, int bsign)
{
register int i;
i = a->len;
while (i >= 0)
{
digit_shift(&row, 1);
row.digits[0] = a->digits[i];
c->digits[i] = 0;
while (fatnum_cmp(&row, b) != PLUS)
{
c->digits[i] ++;
subtract(&row, b, &tmp);
row = tmp;
}
i--;
}
format_fatnum(c);
a->signbit = asign;
b->signbit = bsign;
return (c);
}
/*
** Schoolbook division, this function only handle signs. See division_helper
** for computation. Proected againts division by zero.
*/
t_fatnum *divide(t_fatnum *a, t_fatnum *b, t_fatnum *c)
{
static t_fatnum row;
static t_fatnum tmp;
int asign;
int bsign;
if (is_fatnum_zero(b) == 1)
print_fatal_error(7);
init_fatnum(c);
c->signbit = a->signbit * b->signbit;
asign = a->signbit;
bsign = b->signbit;
a->signbit = PLUS;
b->signbit = PLUS;
init_fatnum(&row);
init_fatnum(&tmp);
c->len = a->len;
return (divide_helper(a, b, c, row, tmp, asign, bsign));
}
/*
** Finds the modulo with simple maths.
** Protected againts mod by zero.
*/
t_fatnum *mod(t_fatnum *a, t_fatnum *b, t_fatnum *c)
{
static t_fatnum div;
static t_fatnum mult;
if (is_fatnum_zero(b) == 1)
print_fatal_error(8);
divide(a, b, &div);
multiply(&div, b, &mult);
subtract(a, &mult, c);
return (c);
}