Beispiel #1
0
char				*ft_itoa(int i)
{
	char	*s;
	int		j;
	int		temp;
	int		len;

	if (i == -2147483648)
		return ("-2147483648");
	j = 0;
	len = ft_intlen(i);
	s = (char*)malloc(sizeof(char) * len);
	if (i < 0)
	{
		s[j++] = '-';
		i = i * -1;
		len--;
	}
	while (len - 1)
	{
		temp = i / ft_pow(10, len - 2);
		s[j++] = temp + 48;
		i = i - temp * ft_pow(10, len - 2);
		len--;
	}
	s[j] = '\0';
	return (s);
}
Beispiel #2
0
static int		ft_baseconvert(char *str)
{
	int		i;
	int		j;
	int		color;

	i = 0;
	j = 5;
	color = 0;
	while (str[i++])
		str[i - 1] = ft_tolower(str[i - 1]);
	i = 0;
	if (str[0] == '0' && str[1] == 'x')
		i = 2;
	while (j >= 0)
	{
		if (str[i] && str[i] >= 'a' && str[i] <= 'f')
			color += (int)((str[i] - 87) * ft_pow(16, j));
		else if (str[i] && str[i] >= 'A' && str[i] <= 'F')
			color += (int)((str[i] - 55) * ft_pow(16, j));
		else if (str[i] && str[i] >= '0' && str[i] <= '9')
			color += (int)((str[i] - 48) * ft_pow(16, j));
		if (str[i])
			i++;
		j--;
	}
	return (color);
}
Beispiel #3
0
static float	get_result(int size_int, int size_float, char *equ)
{
	int		i;
	int		j;
	float	result;
	float	result_float;
	float	tmp;

	result = 0;
	result_float = 0;
	i = -1;
	j = 1;
	while (++i < size_int)
		result = ((equ[i] - '0') * ft_pow(10, size_int - 1 - i)) + result;
	if (size_float > 0)
	{
		while (++i <= size_int + size_float)
		{
			tmp = ((1 / (float)ft_pow(10, j)) * (equ[i] - '0'));
			result_float = tmp + result_float;
			j++;
		}
	}
	result = result + result_float;
	return (result);
}
Beispiel #4
0
void	ft_deltamin2(double d, double r, double **nums)
{
	r = (-nums[1][0]) / (2 * nums[2][0]);
	if (r == 0)
		r = 0;
	printf("delta < 0\n");
	printf("(-b - root(b^2 - 4ac)) / 2a = (%g - ", -nums[1][0]);
	printf("root(%g - 4 *", ft_pow(nums[1][0], 2));
	printf(" (%g * %g))) / (2 * %g)\n", nums[0][0], nums[2][0], nums[2][0]);
	printf("(-b - root(b^2 - 4ac)) / 2a = (%g - ", -nums[1][0]);
	printf("root(%g -", ft_pow(nums[1][0], 2));
	printf(" %g)) / %g\n", 4 * nums[0][0] * nums[2][0], 2 * nums[2][0]);
	printf("(-b - root(b^2 - 4ac)) / 2a = (%g - ", -nums[1][0]);
	printf("%gi)", ft_root(-d));
	printf(" / %g\n", 2 * nums[2][0]);
	printf("Result 1 : %g - %gi\n", r, (ft_root(-d)) / (2 * nums[2][0]));
	printf("(-b + root(b^2 - 4ac)) / 2a = (%g + ", -nums[1][0]);
	printf("root(%g - 4 *", ft_pow(nums[1][0], 2));
	printf(" (%g * %g))) / (2 * %g)\n", nums[0][0], nums[2][0], nums[2][0]);
	printf("(-b + root(b^2 - 4ac)) / 2a = (%g + ", -nums[1][0]);
	printf("root(%g -", ft_pow(nums[1][0], 2));
	printf(" %g)) / %g\n", 4 * nums[0][0] * nums[2][0], 2 * nums[2][0]);
	printf("(-b + root(b^2 - 4ac)) / 2a = (%g + ", -nums[1][0]);
	printf("%gi)", ft_root(-d));
	printf(" / %g\n", 2 * nums[2][0]);
	printf("Result 2 : %g + %gi\n", r, (ft_root(-d)) / (2 * nums[2][0]));
}
t_real			collision_test_sphere_c(t_ray *ray, t_sphere *sphere)
{
	return (ft_pow(ray->origin.x - sphere->position.x, 2)
			+ ft_pow(ray->origin.y - sphere->position.y, 2)
			+ ft_pow(ray->origin.z - sphere->position.z, 2)
			- ft_pow(sphere->radius, 2));
}
Beispiel #6
0
void		ft_putnbr(int n)
{
    int		save;
    size_t	len;

    save = n;
    len = 0;
    if (!n)
        ft_putchar('0');
    if (n < 0)
    {
        ft_putchar('-');
        n = -n;
    }
    while (save)
    {
        save /= 10;
        len++;
    }
    while (len)
    {
        ft_putchar(n / ft_pow(10, (int)len - 1) + '0');
        n -= n / ft_pow(10, (int)len - 1) * ft_pow(10, (int)len - 1);
        len--;
    }
}
Beispiel #7
0
static	void	ft_print_fd(int taille, int c, int fd)
{
	while (taille >= 0)
	{
		ft_putchar_fd((char)(c / (ft_pow(10, taille)) + (int)'0'), fd);
		c %= ft_pow(10, taille);
		taille--;
	}
}
Beispiel #8
0
int		ft_strisint(char *str)
{
	int		i;
	int		sign;
	int		len;
	int		nb;

	sign = 1;
	if (*str == '+' || *str == '-')
		str += (*str == '+') ? sign : -(sign *= -1);
	len = ft_strlen(str);
	i = len;
	while (--i >= 0)
	{
		if (!ft_isdigit(str[i]))
			return (0);
	}
	if (len != 10)
		return (len < 10);
	while (++i < len)
	{
		nb = (i == len - 1 && sign == -1)
			? 8 : (2147483647 / ft_pow(10, len - i - 1)) % 10;
		if (str[i] != nb + '0')
			return (str[i] < nb + '0');
	}
	return (1);
}
Beispiel #9
0
int						collision_test_sphere(t_ray_result *r,
											  t_sphere *sphere)
{
	t_real				a;
	t_real				b;
	t_real				c;
	t_real				det;

	a = collision_test_sphere_a(&r->ray.direction);
	b = collision_test_sphere_b(&r->ray, &sphere->position);
	c = collision_test_sphere_c(&r->ray, sphere);

	det = ft_pow(b, 2) - (4 * a * c);
	if (!det)
		r->distance = -(b / 2 * a);
	else if (det > 0)
		r->distance = FT_MIN((-b - sqrt(det)) / (2 * a),
							 (-b + sqrt(det)) / (2 * a));
	else
		return (0);
	r->hit = OT_SPHERE;
	r->contact.point = vec3_add(r->ray.origin,
								vec3_scale(r->ray.direction, r->distance));
	return (1);
}
Beispiel #10
0
double		ft_disc(double a, double b, double c)
{
	double		delta;

	delta = ft_pow(b, 2) - (4 * a * c);
	return (delta);
}
Beispiel #11
0
char			*ft_itoa_base(int value, int base)
{
	char	*res;
	int		i;

	i = 1;
	if (base <= 1 || base > 16)
		return (NULL);
	if (base == 10 || value == 0)
		return (ft_itoa(value));
	value = value < 0 ? -value : value;
	while (ft_pow(base, i) - 1 < (long int)value)
		i++;
	if (!(res = malloc(sizeof(res) * (i + 1))))
		return (NULL);
	res[i] = '\0';
	i--;
	while (value)
	{
		if (value % base >= 10)
			res[i] = 'A' + (value % base - 10);
		else
			res[i] = '0' + value % base;
		value /= base;
		i--;
	}
	return (res);
}
Beispiel #12
0
int				ft_atoi(const char *str)
{
	long long	result;
	int			neg;
	size_t		size;
	char		*s;

	neg = 1;
	result = 0;
	s = skip_white_space(str);
	if (!s)
		return (0);
	if (*s == '+')
		s++;
	else if (*s == '-')
	{
		s++;
		neg = -1;
	}
	s = skip_nul(s);
	size = get_size(s);
	while (ft_isdigit((int)*s))
	{
		result += (*s - '0') * ft_pow(10, size-- - 1);
		s++;
	}
	return (result * neg);
}
Beispiel #13
0
static	int		ft_pow(int nb, int pow)
{
	if (pow == 0)
		return (1);
	else
		return (nb * ft_pow(nb, pow - 1));
}
Beispiel #14
0
char				*printf_ho(unsigned short int nb, t_args args)
{
	char		*res;
	int			i;
	const char	hexa[] = "0123456789abcdef";

	i = 1;
	if (!nb && args.precision == 0 && !(ft_strchr(args.option, '#')))
		return (ft_strdup("\0"));
	while (ft_pow(8, i) - 1 < nb)
		i++;
	i = (args.precision > i) ? args.precision : i;
	if (ft_strchr(args.option, '#') && nb)
		i++;
	if (!(res = ft_strnew(i--)))
		return (NULL);
	res[i] = (!nb) ? '0' : res[i];
	while (nb)
	{
		res[i--] = hexa[nb % 8];
		nb /= 8;
	}
	if (ft_strchr(args.option, '#') && i >= 0)
		res[i--] = '0';
	while (i >= 0)
		res[i--] = '0';
	return (res);
}
Beispiel #15
0
long int	ft_strtol(char *line)
{
	long int	rslt;
	long int	*hexa;
	int			cmp;
	int			power;

	cmp = 0;
	power = 0;
	rslt = 0;
	hexa = (long int*)malloc(sizeof(long int) * ft_strlen(line));
	while (line[cmp])
	{
		if (line[cmp] >= 'A' && line[cmp] <= 'F')
			hexa[cmp] = line[cmp] + 10 - 'A';
		else if (line[cmp] >= '0' && line[cmp] <= '9')
			hexa[cmp] = line[cmp] - '0';
		cmp++;
	}
	while (--cmp >= 0)
	{
		rslt += hexa[cmp] * ft_pow(16, power);
		power++;
	}
	free(hexa);
	return (rslt);
}
Beispiel #16
0
char		*ft_itoa(int n)
{
	char	*str;
	long	nb;
	int		i;
	int		j;
	int		k;

	nb = (long)n;
	j = 0;
	i = ft_splitting(nb, 0);
	str = (char *)malloc(sizeof(char) * i + 1);
	if (nb < 0)
	{
		str[j++] = '-';
		nb = -nb;
	}
	while (i >= 0)
	{
		k = ft_pow(i, 1);
		str[j] = nb / k + '0';
		j++;
		nb = nb % k;
		i--;
	}
	str[j] = '\0';
	return (str);
}
Beispiel #17
0
void		get_flag(char *str, t_data *data)
{
	int		i;
	char	*flag;

	flag = "#0-+ ";
	i = -1;
	data->len_for = ft_strlen(str) + 1;
	while (ft_strpos(flag, str[++i]) > -1 && str[i])
		data->flag = data->flag | ft_pow(2, ft_strpos(flag, str[i]));
	if (ft_isdigit(str[i]))
		data->field = ft_atoi(str + i);
	while (ft_isdigit(str[i]))
		i++;
	if (str[i] == '.')
	{
		i++;
		data->prec = ft_atoi(str + i);
	}
	while (ft_isdigit(str[i]))
		i++;
	data->len_mod = get_len_mod(str + i);
	if ((data->flag & 2) > 0 && (data->flag & 4) > 0)
		data->flag = data->flag - 2;
	if ((data->flag & 8) > 0 && (data->flag & 16) > 0)
		data->flag = data->flag - 16;
}
Beispiel #18
0
void	ft_puthex(long long int nb, int width)
{
	char			val[width + 1];
	int				i;
	long long int	tmp;

	i = width;
	while (--i >= 0)
	{
		tmp = nb / ft_pow(16, i);
		nb -= tmp * ft_pow(16, i);
		tmp = (tmp < 10 ? tmp + '0' : tmp - 10 + 'a');
		val[width - (i + 1)] = tmp;
	}
	val[width] = '\0';
	ft_putstr(val);
}
Beispiel #19
0
unsigned long	ft_pow
	(unsigned long a, unsigned long n)
{
	if (n == 0)
		return (1);
	else
		return (a * ft_pow(a, n - 1));
}
Beispiel #20
0
int	ft_pow(int nb, int pow)
{
	if (pow > 1)
		return (nb * ft_pow(nb, pow - 1));
	if (pow <= 0)
		return (1);
	return (nb);
}
t_counter	ft_counterpow(t_counter cnt, int val, char c)
{
	cnt.value = ft_pow(cnt.value, val);
	cnt.ope = c;
	cnt.nbo = cnt.nbo + 1;
	cnt.num = cnt.num;
	return (cnt);
}
Beispiel #22
0
int	ft_pow(int nb, int power)
{
	if (power == 0)
		return (1);
	if (power == 1)
		return (nb);
	return (nb * ft_pow(nb, power - 1));
}
Beispiel #23
0
int		ft_pow(int nb, int ex)
{
	if (ex == 0)
		return (1);
	else if (ex < 0)
		return (0);
	return (nb * ft_pow(nb, ex - 1));
}
Beispiel #24
0
static unsigned int	ft_pow(unsigned int nb)
{
	if (nb == 0)
		return (1);
	else if (nb == 1)
		return (10);
	else
		return (ft_pow(nb - 1) * 10);
}
Beispiel #25
0
static int	ft_pow(int i, int nb)
{
	while (i > 0)
	{
		nb = nb * 10;
		i--;
		ft_pow(i, nb);
	}
	return (nb);
}
Beispiel #26
0
static int			ft_addnumber(const char *nptr, int i, unsigned int len)
{
	int				j;
	unsigned int	cpt;

	j = 0;
	cpt = 0;
	while (cpt < len)
	{
		j = j + (nptr[i + len - cpt - 1] - 48) * ft_pow(cpt);
		cpt++;
	}
	return (j);
}
Beispiel #27
0
void	ft_deltasup2(double d, double **nums)
{
	printf("delta > 0\n");
	printf("(-b - root(b^2 - 4ac)) / 2a = (%g - ", -nums[1][0]);
	printf("root(%g - 4 *", ft_pow(nums[1][0], 2));
	printf(" (%g * %g))) / (2 * %g)\n", nums[0][0], nums[2][0], nums[2][0]);
	printf("(-b - root(b^2 - 4ac)) / 2a = (%g - ", -nums[1][0]);
	printf("root(%g -", ft_pow(nums[1][0], 2));
	printf(" %g)) / %g\n", 4 * nums[0][0] * nums[2][0], 2 * nums[2][0]);
	printf("(-b - root(b^2 - 4ac)) / 2a = (%g - ", -nums[1][0]);
	printf("%g)", ft_root(d));
	printf(" / %g\n", 2 * nums[2][0]);
	printf("Result 1 : %g\n", (-nums[1][0] - ft_root(d)) / (2 * nums[2][0]));
	printf("(-b + root(b^2 - 4ac)) / 2a = (%g + ", -nums[1][0]);
	printf("root(%g - 4 *", ft_pow(nums[1][0], 2));
	printf(" (%g * %g))) / (2 * %g)\n", nums[0][0], nums[2][0], nums[2][0]);
	printf("(-b + root(b^2 - 4ac)) / 2a = (%g + ", -nums[1][0]);
	printf("root(%g -", ft_pow(nums[1][0], 2));
	printf(" %g)) / %g\n", 4 * nums[0][0] * nums[2][0], 2 * nums[2][0]);
	printf("(-b + root(b^2 - 4ac)) / 2a = (%g + ", -nums[1][0]);
	printf("%g)", ft_root(d));
	printf(" / %g\n", 2 * nums[2][0]);
	printf("Result 2 : %g\n", (-nums[1][0] + ft_root(d)) / (2 * nums[2][0]));
}
Beispiel #28
0
void	ft_solve2(double **nums)
{
	double	d;
	double	r;

	r = 0;
	d = ft_pow(nums[1][0], 2) - (4 * nums[0][0] * nums[2][0]);
	if (d < 0)
		ft_deltamin2(d, r, nums);
	else if (d == 0)
		ft_deltaequal2(nums);
	else
		ft_deltasup2(d, nums);
	return ;
}
Beispiel #29
0
static void				ft_dtoa_dot(char *buffer, double nb,
	unsigned int precision, const char *map)
{
	long	x;

	if (!precision)
		return ;
	*(buffer++) = '.';
	nb -= (double)((__int128)nb);
	x = (long)(nb * ft_pow(10, (int)precision));
	while (precision--)
	{
		buffer[precision] = map[x % 10];
		x /= 10;
	}
}
Beispiel #30
0
static char		*ft_init_itoa(unsigned int nb, unsigned int base,
		unsigned int *pow, int signe)
{
	char	*ret;

	*pow = 1;
	while (nb >= base)
	{
		nb /= base;
		(*pow)++;
	}
	if (!(ret = ft_strnew(*pow + (signe == -1))))
		return (NULL);
	*pow = ft_pow(base, *pow - 1);
	return (ret);
}