-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_pow.c
45 lines (39 loc) · 1.32 KB
/
ft_pow.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pow.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngouy <ngouy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/02/24 11:05:35 by ngouy #+# #+# */
/* Updated: 2015/11/04 16:40:17 by ngouy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Pow of a number handeling integer overflow -> return 0 if its the case.
*/
#include "libft.h"
static int ft_wil_overflow(int i, int j)
{
int k;
k = i * j;
return (k / j == i ? k : 0);
}
int ft_pow(int x, int pow)
{
int ret;
ret = x;
if (pow < 0 || x == 0)
return (0);
if (pow == 0)
return (1);
if (pow == 1)
return (x);
while (pow != 1)
{
if (!(ret = ft_wil_overflow(ret, x)))
return (0);
pow--;
}
return (ret);
}