mirror of
https://gitee.com/Lamdonn/varch.git
synced 2025-12-06 16:56:42 +08:00
Fix floatl print issue
This commit is contained in:
parent
fa4bd85f2e
commit
085cfede8a
@ -140,8 +140,8 @@ Usage example:
|
||||
```c
|
||||
static void test_calculate(void)
|
||||
{
|
||||
floatl a = floatl_from("123456789.123456789");
|
||||
floatl b = floatl_from("987654321.987654321");
|
||||
floatl a = floatl_from("12345678901234567890.123456789");
|
||||
floatl b = floatl_from("98765432109876543210.987654321");
|
||||
|
||||
printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f"));
|
||||
printf("b %s\r\n", floatl_show(b, buffer, sizeof(buffer), "%f"));
|
||||
@ -149,7 +149,7 @@ static void test_calculate(void)
|
||||
printf("a + b: %s\r\n", floatl_show(floatl_add(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a - b: %s\r\n", floatl_show(floatl_sub(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a * b: %s\r\n", floatl_show(floatl_mul(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("b / a: %s\r\n", floatl_show(floatl_div(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a / b: %s\r\n", floatl_show(floatl_div(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("-a: %s\r\n", floatl_show(floatl_neg(a), buffer, sizeof(buffer), "%f"));
|
||||
printf("|a|: %s\r\n", floatl_show(floatl_abs(a), buffer, sizeof(buffer), "%f"));
|
||||
printf("a > b: %d\r\n", floatl_gt(a, b));
|
||||
@ -163,14 +163,14 @@ static void test_calculate(void)
|
||||
|
||||
Results:
|
||||
```
|
||||
a 123456789.123456
|
||||
b 987654321.987650
|
||||
a + b: 1111111111.111118
|
||||
a - b: -864197532.864200
|
||||
a * b: 121932631356500755.185485
|
||||
b / a: 0.125000
|
||||
-a: -123456789.123456
|
||||
|a|: 123456789.123456
|
||||
a 12345678901234567890.123457
|
||||
b 98765432109876543210.987654
|
||||
a + b: 111111111011111111101.111111
|
||||
a - b: -86419753208641975320.864198
|
||||
a * b: 1219326311370217952261850327336229233322.374638
|
||||
a / b: 0.125000
|
||||
-a: -12345678901234567890.123457
|
||||
|a|: 12345678901234567890.123457
|
||||
a > b: 0
|
||||
a >= b: 0
|
||||
a < b: 1
|
||||
|
||||
@ -140,8 +140,8 @@ int floatl_ge(floatl a, floatl b); // a>=b
|
||||
```c
|
||||
static void test_calculate(void)
|
||||
{
|
||||
floatl a = floatl_from("123456789.123456789");
|
||||
floatl b = floatl_from("987654321.987654321");
|
||||
floatl a = floatl_from("12345678901234567890.123456789");
|
||||
floatl b = floatl_from("98765432109876543210.987654321");
|
||||
|
||||
printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f"));
|
||||
printf("b %s\r\n", floatl_show(b, buffer, sizeof(buffer), "%f"));
|
||||
@ -149,7 +149,7 @@ static void test_calculate(void)
|
||||
printf("a + b: %s\r\n", floatl_show(floatl_add(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a - b: %s\r\n", floatl_show(floatl_sub(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a * b: %s\r\n", floatl_show(floatl_mul(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("b / a: %s\r\n", floatl_show(floatl_div(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a / b: %s\r\n", floatl_show(floatl_div(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("-a: %s\r\n", floatl_show(floatl_neg(a), buffer, sizeof(buffer), "%f"));
|
||||
printf("|a|: %s\r\n", floatl_show(floatl_abs(a), buffer, sizeof(buffer), "%f"));
|
||||
printf("a > b: %d\r\n", floatl_gt(a, b));
|
||||
@ -163,14 +163,14 @@ static void test_calculate(void)
|
||||
|
||||
结果:
|
||||
```
|
||||
a 123456789.123456
|
||||
b 987654321.987650
|
||||
a + b: 1111111111.111118
|
||||
a - b: -864197532.864200
|
||||
a * b: 121932631356500755.185485
|
||||
b / a: 0.125000
|
||||
-a: -123456789.123456
|
||||
|a|: 123456789.123456
|
||||
a 12345678901234567890.123457
|
||||
b 98765432109876543210.987654
|
||||
a + b: 111111111011111111101.111111
|
||||
a - b: -86419753208641975320.864198
|
||||
a * b: 1219326311370217952261850327336229233322.374638
|
||||
a / b: 0.125000
|
||||
-a: -12345678901234567890.123457
|
||||
|a|: 12345678901234567890.123457
|
||||
a > b: 0
|
||||
a >= b: 0
|
||||
a < b: 1
|
||||
|
||||
7
run.sh
7
run.sh
@ -8,7 +8,12 @@ if [ -f $exe_file ]; then
|
||||
fi
|
||||
|
||||
echo "compilling..."
|
||||
make
|
||||
|
||||
if [ "$1" == "debug" ]; then
|
||||
make CFLAG=-g
|
||||
else
|
||||
make
|
||||
fi
|
||||
|
||||
if [ -f $exe_file ]; then
|
||||
echo "executing..."
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
* \unit floatl
|
||||
* \brief This is a simple large float number calculate module for C language
|
||||
* \author Lamdonn
|
||||
* \version v1.0.0
|
||||
* \version v1.0.1
|
||||
* \license GPL-2.0
|
||||
* \copyright Copyright (C) 2023 Lamdonn.
|
||||
********************************************************************************************************/
|
||||
@ -2852,7 +2852,7 @@ static char gChar(floatl num)
|
||||
// If the exponent is negative, the integer part is 0
|
||||
else if (exp < 0) return '0';
|
||||
|
||||
#if 0 // This block is commented out. It calculates the result by considering each bit
|
||||
#if 1 // This block is commented out. It calculates the result by considering each bit
|
||||
// Iterate over each bit of the mantissa
|
||||
for (int i = 0; i < __FLOATL_MANT_BITS__; i++)
|
||||
{
|
||||
@ -2876,7 +2876,7 @@ static char gChar(floatl num)
|
||||
// Add the value corresponding to the exponent
|
||||
c += ((exp > 0) ? bitEnd[(exp - 1) % 4] : 1);
|
||||
// Get the last digit and convert it to a character
|
||||
c = ((unsigned char)c) % 10 + '0';
|
||||
c = c % 10 + '0';
|
||||
#else // This block is the current method, calculating the result by considering u32 parts
|
||||
// Add the hidden bit to the high - order part of the mantissa
|
||||
uint32_t hi = (uint32_t)num.mantissa | (1 << __FLOATL_MANT_HIGH_BITS__);
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
* \unit floatl
|
||||
* \brief This is a simple large float number calculate module for C language
|
||||
* \author Lamdonn
|
||||
* \version v1.0.0
|
||||
* \version v1.0.1
|
||||
* \license GPL-2.0
|
||||
* \copyright Copyright (C) 2023 Lamdonn.
|
||||
********************************************************************************************************/
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
* \unit floatl
|
||||
* \brief This is a simple large int number calculate module config header file for C language
|
||||
* \author Lamdonn
|
||||
* \version v1.0.0
|
||||
* \version v1.0.1
|
||||
* \license GPL-2.0
|
||||
* \copyright Copyright (C) 2023 Lamdonn.
|
||||
********************************************************************************************************/
|
||||
@ -459,6 +459,28 @@ void floatl_cfg_const_out(FILE* file, FLINFO *info)
|
||||
fprintf(file, "}}\n");
|
||||
}
|
||||
|
||||
int floatl_cfg_info_init(FLINFO *info, unsigned int bits)
|
||||
{
|
||||
if ((bits & (bits - 1)) != 0) return 0;
|
||||
|
||||
info->FLOATL_BIT_PARTS = bits;
|
||||
info->FLOATL_U32_PARTS = info->FLOATL_BIT_PARTS >> (5);
|
||||
info->FLOATL_U16_PARTS = info->FLOATL_BIT_PARTS >> (4);
|
||||
info->FLOATL_EXP_BITS = floatl_cfg_gen_exp(info->FLOATL_BIT_PARTS);
|
||||
info->FLOATL_MANT_BITS = info->FLOATL_BIT_PARTS - info->FLOATL_EXP_BITS - 1;
|
||||
info->FLOATL_MANT_PARTS = info->FLOATL_MANT_BITS >> 5;
|
||||
info->FLOATL_MANT_HIGH_BITS = info->FLOATL_MANT_BITS & 0x1F;
|
||||
info->FLOATL_EXP_MID_VALUE = (uint32_t)pow(2, info->FLOATL_EXP_BITS - 1) - 1;
|
||||
info->FLOATL_EXP_WHL_VALUE = (uint32_t)pow(2, info->FLOATL_EXP_BITS) - 1;
|
||||
info->FLOATL2_BIT_PARTS = info->FLOATL_BIT_PARTS * 2;
|
||||
info->FLOATL2_U32_PARTS = info->FLOATL2_BIT_PARTS >> (5);
|
||||
info->FLOATL2_U16_PARTS = info->FLOATL2_BIT_PARTS >> (4);
|
||||
|
||||
memset(info->buffer, 0, info->FLOATL_U32_PARTS * sizeof(uint32_t));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
FLINFO *floatl_cfg_set_sign(FLINFO *info, int sign)
|
||||
{
|
||||
if (sign) info->buffer[info->FLOATL_U32_PARTS - 1] |= (1 << 31);
|
||||
@ -693,18 +715,7 @@ void floatl_cfg_generate(uint32_t bits, const char *filename)
|
||||
}
|
||||
|
||||
FLINFO info;
|
||||
info.FLOATL_BIT_PARTS = tb;
|
||||
info.FLOATL_U32_PARTS = info.FLOATL_BIT_PARTS >> (5);
|
||||
info.FLOATL_U16_PARTS = info.FLOATL_BIT_PARTS >> (4);
|
||||
info.FLOATL_EXP_BITS = floatl_cfg_gen_exp(info.FLOATL_BIT_PARTS);
|
||||
info.FLOATL_MANT_BITS = info.FLOATL_BIT_PARTS - info.FLOATL_EXP_BITS - 1;
|
||||
info.FLOATL_MANT_PARTS = info.FLOATL_MANT_BITS >> 5;
|
||||
info.FLOATL_MANT_HIGH_BITS = info.FLOATL_MANT_BITS & 0x1F;
|
||||
info.FLOATL_EXP_MID_VALUE = (uint32_t)pow(2, info.FLOATL_EXP_BITS - 1) - 1;
|
||||
info.FLOATL_EXP_WHL_VALUE = (uint32_t)pow(2, info.FLOATL_EXP_BITS) - 1;
|
||||
info.FLOATL2_BIT_PARTS = info.FLOATL_BIT_PARTS * 2;
|
||||
info.FLOATL2_U32_PARTS = info.FLOATL2_BIT_PARTS >> (5);
|
||||
info.FLOATL2_U16_PARTS = info.FLOATL2_BIT_PARTS >> (4);
|
||||
floatl_cfg_info_init(&info, tb);
|
||||
|
||||
fprintf(output, "#define __FLOATL_BIT_PARTS__ %u"NEWLINE, info.FLOATL_BIT_PARTS );
|
||||
fprintf(output, "#define __FLOATL_U32_PARTS__ %u"NEWLINE, info.FLOATL_U32_PARTS );
|
||||
|
||||
@ -840,6 +840,28 @@ void floatl_cfg_const_out(FILE* file, FLINFO *info)
|
||||
fprintf(file, "}}\n");
|
||||
}
|
||||
|
||||
int floatl_cfg_info_init(FLINFO *info, unsigned int bits)
|
||||
{
|
||||
if ((bits & (bits - 1)) != 0) return 0;
|
||||
|
||||
info->FLOATL_BIT_PARTS = bits;
|
||||
info->FLOATL_U32_PARTS = info->FLOATL_BIT_PARTS >> (5);
|
||||
info->FLOATL_U16_PARTS = info->FLOATL_BIT_PARTS >> (4);
|
||||
info->FLOATL_EXP_BITS = floatl_cfg_gen_exp(info->FLOATL_BIT_PARTS);
|
||||
info->FLOATL_MANT_BITS = info->FLOATL_BIT_PARTS - info->FLOATL_EXP_BITS - 1;
|
||||
info->FLOATL_MANT_PARTS = info->FLOATL_MANT_BITS >> 5;
|
||||
info->FLOATL_MANT_HIGH_BITS = info->FLOATL_MANT_BITS & 0x1F;
|
||||
info->FLOATL_EXP_MID_VALUE = (uint32_t)pow(2, info->FLOATL_EXP_BITS - 1) - 1;
|
||||
info->FLOATL_EXP_WHL_VALUE = (uint32_t)pow(2, info->FLOATL_EXP_BITS) - 1;
|
||||
info->FLOATL2_BIT_PARTS = info->FLOATL_BIT_PARTS * 2;
|
||||
info->FLOATL2_U32_PARTS = info->FLOATL2_BIT_PARTS >> (5);
|
||||
info->FLOATL2_U16_PARTS = info->FLOATL2_BIT_PARTS >> (4);
|
||||
|
||||
memset(info->buffer, 0, info->FLOATL_U32_PARTS * sizeof(uint32_t));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
FLINFO *floatl_cfg_set_sign(FLINFO *info, int sign)
|
||||
{
|
||||
if (sign) info->buffer[info->FLOATL_U32_PARTS - 1] |= (1 << 31);
|
||||
@ -1074,18 +1096,7 @@ void floatl_cfg_generate(uint32_t bits, const char *filename)
|
||||
}
|
||||
|
||||
FLINFO info;
|
||||
info.FLOATL_BIT_PARTS = tb;
|
||||
info.FLOATL_U32_PARTS = info.FLOATL_BIT_PARTS >> (5);
|
||||
info.FLOATL_U16_PARTS = info.FLOATL_BIT_PARTS >> (4);
|
||||
info.FLOATL_EXP_BITS = floatl_cfg_gen_exp(info.FLOATL_BIT_PARTS);
|
||||
info.FLOATL_MANT_BITS = info.FLOATL_BIT_PARTS - info.FLOATL_EXP_BITS - 1;
|
||||
info.FLOATL_MANT_PARTS = info.FLOATL_MANT_BITS >> 5;
|
||||
info.FLOATL_MANT_HIGH_BITS = info.FLOATL_MANT_BITS & 0x1F;
|
||||
info.FLOATL_EXP_MID_VALUE = (uint32_t)pow(2, info.FLOATL_EXP_BITS - 1) - 1;
|
||||
info.FLOATL_EXP_WHL_VALUE = (uint32_t)pow(2, info.FLOATL_EXP_BITS) - 1;
|
||||
info.FLOATL2_BIT_PARTS = info.FLOATL_BIT_PARTS * 2;
|
||||
info.FLOATL2_U32_PARTS = info.FLOATL2_BIT_PARTS >> (5);
|
||||
info.FLOATL2_U16_PARTS = info.FLOATL2_BIT_PARTS >> (4);
|
||||
floatl_cfg_info_init(&info, tb);
|
||||
|
||||
fprintf(output, "#define __FLOATL_BIT_PARTS__ %u"NEWLINE, info.FLOATL_BIT_PARTS );
|
||||
fprintf(output, "#define __FLOATL_U32_PARTS__ %u"NEWLINE, info.FLOATL_U32_PARTS );
|
||||
@ -1172,6 +1183,26 @@ static void test_error()
|
||||
printf("b: %s\r\n", buffer); // print 0x8.1538p-134
|
||||
// why?
|
||||
|
||||
// // 1234567890
|
||||
// floatl a = floatl_from("0x1.26580B48p+30"); // 1 001001100101100000001011010010 00
|
||||
|
||||
// floatl a = FLOATL_CONST_0;
|
||||
|
||||
// FLINFO info;
|
||||
// floatl_cfg_info_init(&info, sizeof(floatl) * 8);
|
||||
|
||||
// 1234567890
|
||||
// 1 001001100101100000001011010010 00
|
||||
// floatl_cfg_set_exp(&info, 30);
|
||||
// floatl_cfg_set_mant(&info, "001001100101100000001011010010");
|
||||
|
||||
// 123456789012345678901234567890
|
||||
// 1 100011101110100100001111111101101100001101110011111000001110111001001110001111110000101011010010
|
||||
// floatl_cfg_set_exp(&info, 96);
|
||||
// floatl_cfg_set_mant(&info, "100011101110100100001111111101101100001101110011111000001110111001001110001111110000101011010010");
|
||||
|
||||
// a = *((floatl *)(info.buffer));
|
||||
|
||||
// double_print(stdout, d1);
|
||||
// printf("double_from_string %f\r\n", double_from_string("123.456"));
|
||||
// printf("double_from_string %e\r\n", double_from_string("1.23456e+2"));
|
||||
@ -1233,8 +1264,8 @@ static void test_print(void)
|
||||
|
||||
static void test_calculate(void)
|
||||
{
|
||||
floatl a = floatl_from("123456789.123456789");
|
||||
floatl b = floatl_from("987654321.987654321");
|
||||
floatl a = floatl_from("12345678901234567890.123456789");
|
||||
floatl b = floatl_from("98765432109876543210.987654321");
|
||||
|
||||
printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f"));
|
||||
printf("b %s\r\n", floatl_show(b, buffer, sizeof(buffer), "%f"));
|
||||
@ -1242,7 +1273,7 @@ static void test_calculate(void)
|
||||
printf("a + b: %s\r\n", floatl_show(floatl_add(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a - b: %s\r\n", floatl_show(floatl_sub(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a * b: %s\r\n", floatl_show(floatl_mul(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("b / a: %s\r\n", floatl_show(floatl_div(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("a / b: %s\r\n", floatl_show(floatl_div(a, b), buffer, sizeof(buffer), "%f"));
|
||||
printf("-a: %s\r\n", floatl_show(floatl_neg(a), buffer, sizeof(buffer), "%f"));
|
||||
printf("|a|: %s\r\n", floatl_show(floatl_abs(a), buffer, sizeof(buffer), "%f"));
|
||||
printf("a > b: %d\r\n", floatl_gt(a, b));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user