-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_power.c
41 lines (38 loc) · 1.29 KB
/
ft_power.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_power.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jphilip- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/11 20:36:01 by jphilip- #+# #+# */
/* Updated: 2018/12/11 20:36:03 by jphilip- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
long long ft_power(int nb, int power)
{
long long res;
int x;
res = 1;
if (power < 0)
return (0);
if (power == 0)
return (1);
while (power >= 1)
{
if (res < 0)
x = -1;
else
x = 1;
res = res * nb;
if (res < 0 && x == -1 && nb < 0)
return (0);
else if (res > 0 && x == 1 && nb < 0)
return (0);
else if (res < 0 && x == 1 && nb > 0)
return (0);
power--;
}
return (res);
}