-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexponential.c
160 lines (146 loc) · 3.27 KB
/
exponential.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
** EPITECH PROJECT, 2023
** expoential function
** File description:
** exponetial fct
*/
#include <stdio.h>
#include "includes/my_printf.h"
static void divided(unsigned int *c, double *nb, int preci)
{
double temp = 0;
if (*nb < 0) {
*nb = -(*nb);
my_putchar('-');
}
for (*c = 0; *nb >= 10.0; *c = *c + 1) {
temp = (int) *nb % 10;
*nb /= 10.0 ;
}
if (temp >= 5 && preci == 0) {
*nb = *nb + 1;
}
}
static void multiply(unsigned int *d, double *nb)
{
while (*nb < 1.0) {
*nb *= 10.0;
*d = *d + 1;
}
}
static int positivexpo(int c, double nb, int preci, int caps)
{
int lenght = 0;
if (preci == 0)
lenght = my_put_nbr((int) nb);
else
lenght = my_put_double(nb, preci);
lenght = lenght + my_int_len(c);
if (caps == 0) {
if (c < 10){
lenght = lenght + 3;
my_printf("e+0%i", c);
}else{
lenght = lenght + 2;
my_printf("e+%i", c);
}
}
return lenght;
}
static int negexpo(int d, double nb, int preci, int caps)
{
int lenght = 0;
if (preci == 0)
lenght = my_put_nbr((int) nb);
else
lenght = my_put_double(nb, preci);
lenght = lenght + my_int_len(d);
if (caps == 0){
if (d < 10){
lenght = lenght + 3;
my_printf("e-0%i", d);
}else{
lenght = lenght + 2;
my_printf("e-%i", d);
}
}
return lenght;
}
int upnegexpo(int d, double nb, int preci)
{
int lenght = 0;
if (preci == 0)
lenght = my_put_nbr((int) nb);
else
lenght = my_put_double(nb, preci);
lenght = lenght + my_int_len(d);
if (d < 10){
lenght = lenght + 3;
my_printf("E-0%i", d);
}else{
lenght = lenght + 2;
my_printf("E-%i", d);
}
return lenght;
}
int uppositivexpo(int c, double nb, int preci)
{
int lenght = 0;
if (preci == 0)
lenght = my_put_nbr((int) nb);
else
lenght = my_put_double(nb, preci);
lenght = lenght + my_int_len(c);
if (c < 10){
lenght = lenght + 3;
my_printf("E+0%i", c);
} else {
lenght = lenght + 2;
my_printf("E+%i", c);
}
return lenght;
}
int exponentiel(double nb, int preci)
{
unsigned int c = 0;
unsigned int d = 0;
int lenght = 0;
divided(&c, &nb, preci);
multiply(&d, &nb);
if (c != 0){
lenght = positivexpo(c, nb, preci, 0);
return lenght;
}
if (d != 0){
lenght = negexpo(d, nb, preci, 0);
return lenght;
}
if (preci == 0)
lenght = my_put_nbr((int) nb) + 4;
else
lenght = my_put_double(nb, preci) + 4;
my_printf("e+0%i", nb, 0);
return lenght;
}
int upper_exponentiel(double nb, int preci)
{
unsigned int c = 0;
unsigned int d = 0;
int lenght = 0;
divided(&c, &nb, preci);
multiply(&d, &nb);
if (c != 0){
lenght = uppositivexpo(c, nb, preci);
return lenght;
}
if (d != 0){
lenght = upnegexpo(d, nb, preci);
return lenght;
}
if (preci == 0)
lenght = my_put_nbr((int) nb) + 4;
else
lenght = my_put_double(nb, preci) + 4;
my_printf("E+0%i", 0);
return lenght;
}