-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_types_1.c
106 lines (100 loc) · 2.81 KB
/
utils_types_1.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
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
/**
* set_float_parts - Sets the float_info fields to the appropriate
* \ parts of a given float
* @num: The given float
* @exponent_size: The number of bits in the exponent part
* @mantissa_size: The number of bits in the mantissa part
* @float_info: The float_info struct
*/
void set_float_parts(double num, uchar_t exponent_size,
uchar_t mantissa_size, float_info_t *float_info)
{
int i;
char *str;
uchar_t size = exponent_size + mantissa_size + 1;
size_t tmp = *((size_t *)&num);
if (float_info == NULL)
return;
str = malloc(sizeof(char) * (size + 1));
if (str != NULL)
{
for (i = 0; i < size; i++)
*(str + i) = ((tmp >> i) & 1) + '0';
*(str + size) = '\0';
rev_string(str);
float_info->sign = *str;
for (i = 0; i < exponent_size; i++)
*(float_info->exponent + i) = *(str + i + 1);
*(float_info->exponent + i) = '\0';
for (i = 0; i < mantissa_size; i++)
*(float_info->mantissa + i) = *(str + i + exponent_size + 1);
*(float_info->mantissa + i) = '\0';
free(str);
}
}
/**
* mantissa_to_dec_fraction - Converts the mantissa of a float to a fraction
* @mantissa: The mantissa to convert
* @frac_len: The maximum length of the decimal fraction
*
* Return: The converted fraction
*/
char *mantissa_to_dec_fraction(char *mantissa, unsigned short frac_len)
{
char *str, i, *pow2;
int len = str_len(mantissa);
str = malloc(sizeof(char) * (frac_len + 3));
if (str != NULL)
{
mem_set(str, frac_len + 2, '0');
*(str + 1) = '.';
*(str + frac_len + 2) = '\0';
for (i = 0; i < len; i++)
{
if (*(mantissa + i) == '1')
{
pow2 = two_exp(-(i + 1));
str = add_float(pow2, str, TRUE);
}
}
}
return (str);
}
/**
* float_to_str - Converts an IEEE 754 float to its string representation
* @flt_info: The information about the float
* @can_free: Specifies whether the given numbers can be freed
*
* Return: The string representation of the float, otherwise NULL
*/
char *float_to_str(float_info_t *flt_info, char can_free)
{
uchar_t exponent_size = str_len(flt_info->exponent);
short bias = two_pexp(exponent_size) / 2 - 1, exponent;
char *power, *fraction, *product, *float_num, *pow_frac;
unsigned short frac_len = 22;/* Only doubles are supported */
exponent = bin_to_int(flt_info->exponent) - bias;
power = two_exp(exponent);
fraction = mantissa_to_dec_fraction(flt_info->mantissa, frac_len);
fraction[0] = '1';
if (exponent >= 0)
{
pow_frac = malloc(sizeof(char) * 3);
if (pow_frac)
{
*(pow_frac + 0) = '.';
*(pow_frac + 1) = '0';
*(pow_frac + 2) = '\0';
power = str_cat(power, pow_frac, TRUE);
}
}
product = mul_float(fraction, power, TRUE);
float_num = str_cat(flt_info->sign == '1' ? "-" : "", product, FALSE);
free(product);
if (can_free)
free_float_info(flt_info);
return (float_num);
}