Fix floatl print issue

This commit is contained in:
Lamdonn 2025-03-06 01:33:43 +08:00
parent fa4bd85f2e
commit 085cfede8a
7 changed files with 102 additions and 55 deletions

View File

@ -140,8 +140,8 @@ Usage example:
```c ```c
static void test_calculate(void) static void test_calculate(void)
{ {
floatl a = floatl_from("123456789.123456789"); floatl a = floatl_from("12345678901234567890.123456789");
floatl b = floatl_from("987654321.987654321"); floatl b = floatl_from("98765432109876543210.987654321");
printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f")); printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f"));
printf("b %s\r\n", floatl_show(b, 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_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_sub(a, b), buffer, sizeof(buffer), "%f"));
printf("a * b: %s\r\n", floatl_show(floatl_mul(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_neg(a), buffer, sizeof(buffer), "%f"));
printf("|a|: %s\r\n", floatl_show(floatl_abs(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)); printf("a > b: %d\r\n", floatl_gt(a, b));
@ -163,14 +163,14 @@ static void test_calculate(void)
Results: Results:
``` ```
a 123456789.123456 a 12345678901234567890.123457
b 987654321.987650 b 98765432109876543210.987654
a + b: 1111111111.111118 a + b: 111111111011111111101.111111
a - b: -864197532.864200 a - b: -86419753208641975320.864198
a * b: 121932631356500755.185485 a * b: 1219326311370217952261850327336229233322.374638
b / a: 0.125000 a / b: 0.125000
-a: -123456789.123456 -a: -12345678901234567890.123457
|a|: 123456789.123456 |a|: 12345678901234567890.123457
a > b: 0 a > b: 0
a >= b: 0 a >= b: 0
a < b: 1 a < b: 1

View File

@ -140,8 +140,8 @@ int floatl_ge(floatl a, floatl b); // a>=b
```c ```c
static void test_calculate(void) static void test_calculate(void)
{ {
floatl a = floatl_from("123456789.123456789"); floatl a = floatl_from("12345678901234567890.123456789");
floatl b = floatl_from("987654321.987654321"); floatl b = floatl_from("98765432109876543210.987654321");
printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f")); printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f"));
printf("b %s\r\n", floatl_show(b, 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_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_sub(a, b), buffer, sizeof(buffer), "%f"));
printf("a * b: %s\r\n", floatl_show(floatl_mul(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_neg(a), buffer, sizeof(buffer), "%f"));
printf("|a|: %s\r\n", floatl_show(floatl_abs(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)); printf("a > b: %d\r\n", floatl_gt(a, b));
@ -163,14 +163,14 @@ static void test_calculate(void)
结果: 结果:
``` ```
a 123456789.123456 a 12345678901234567890.123457
b 987654321.987650 b 98765432109876543210.987654
a + b: 1111111111.111118 a + b: 111111111011111111101.111111
a - b: -864197532.864200 a - b: -86419753208641975320.864198
a * b: 121932631356500755.185485 a * b: 1219326311370217952261850327336229233322.374638
b / a: 0.125000 a / b: 0.125000
-a: -123456789.123456 -a: -12345678901234567890.123457
|a|: 123456789.123456 |a|: 12345678901234567890.123457
a > b: 0 a > b: 0
a >= b: 0 a >= b: 0
a < b: 1 a < b: 1

7
run.sh
View File

@ -8,7 +8,12 @@ if [ -f $exe_file ]; then
fi fi
echo "compilling..." echo "compilling..."
make
if [ "$1" == "debug" ]; then
make CFLAG=-g
else
make
fi
if [ -f $exe_file ]; then if [ -f $exe_file ]; then
echo "executing..." echo "executing..."

View File

@ -6,7 +6,7 @@
* \unit floatl * \unit floatl
* \brief This is a simple large float number calculate module for C language * \brief This is a simple large float number calculate module for C language
* \author Lamdonn * \author Lamdonn
* \version v1.0.0 * \version v1.0.1
* \license GPL-2.0 * \license GPL-2.0
* \copyright Copyright (C) 2023 Lamdonn. * \copyright Copyright (C) 2023 Lamdonn.
********************************************************************************************************/ ********************************************************************************************************/
@ -2852,7 +2852,7 @@ static char gChar(floatl num)
// If the exponent is negative, the integer part is 0 // If the exponent is negative, the integer part is 0
else if (exp < 0) return '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 // Iterate over each bit of the mantissa
for (int i = 0; i < __FLOATL_MANT_BITS__; i++) 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 // Add the value corresponding to the exponent
c += ((exp > 0) ? bitEnd[(exp - 1) % 4] : 1); c += ((exp > 0) ? bitEnd[(exp - 1) % 4] : 1);
// Get the last digit and convert it to a character // 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 #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 // Add the hidden bit to the high - order part of the mantissa
uint32_t hi = (uint32_t)num.mantissa | (1 << __FLOATL_MANT_HIGH_BITS__); uint32_t hi = (uint32_t)num.mantissa | (1 << __FLOATL_MANT_HIGH_BITS__);

View File

@ -7,7 +7,7 @@
* \unit floatl * \unit floatl
* \brief This is a simple large float number calculate module for C language * \brief This is a simple large float number calculate module for C language
* \author Lamdonn * \author Lamdonn
* \version v1.0.0 * \version v1.0.1
* \license GPL-2.0 * \license GPL-2.0
* \copyright Copyright (C) 2023 Lamdonn. * \copyright Copyright (C) 2023 Lamdonn.
********************************************************************************************************/ ********************************************************************************************************/

View File

@ -7,7 +7,7 @@
* \unit floatl * \unit floatl
* \brief This is a simple large int number calculate module config header file for C language * \brief This is a simple large int number calculate module config header file for C language
* \author Lamdonn * \author Lamdonn
* \version v1.0.0 * \version v1.0.1
* \license GPL-2.0 * \license GPL-2.0
* \copyright Copyright (C) 2023 Lamdonn. * \copyright Copyright (C) 2023 Lamdonn.
********************************************************************************************************/ ********************************************************************************************************/
@ -459,6 +459,28 @@ void floatl_cfg_const_out(FILE* file, FLINFO *info)
fprintf(file, "}}\n"); 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) FLINFO *floatl_cfg_set_sign(FLINFO *info, int sign)
{ {
if (sign) info->buffer[info->FLOATL_U32_PARTS - 1] |= (1 << 31); 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; FLINFO info;
info.FLOATL_BIT_PARTS = tb; floatl_cfg_info_init(&info, 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);
fprintf(output, "#define __FLOATL_BIT_PARTS__ %u"NEWLINE, info.FLOATL_BIT_PARTS ); fprintf(output, "#define __FLOATL_BIT_PARTS__ %u"NEWLINE, info.FLOATL_BIT_PARTS );
fprintf(output, "#define __FLOATL_U32_PARTS__ %u"NEWLINE, info.FLOATL_U32_PARTS ); fprintf(output, "#define __FLOATL_U32_PARTS__ %u"NEWLINE, info.FLOATL_U32_PARTS );

View File

@ -840,6 +840,28 @@ void floatl_cfg_const_out(FILE* file, FLINFO *info)
fprintf(file, "}}\n"); 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) FLINFO *floatl_cfg_set_sign(FLINFO *info, int sign)
{ {
if (sign) info->buffer[info->FLOATL_U32_PARTS - 1] |= (1 << 31); 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; FLINFO info;
info.FLOATL_BIT_PARTS = tb; floatl_cfg_info_init(&info, 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);
fprintf(output, "#define __FLOATL_BIT_PARTS__ %u"NEWLINE, info.FLOATL_BIT_PARTS ); fprintf(output, "#define __FLOATL_BIT_PARTS__ %u"NEWLINE, info.FLOATL_BIT_PARTS );
fprintf(output, "#define __FLOATL_U32_PARTS__ %u"NEWLINE, info.FLOATL_U32_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 printf("b: %s\r\n", buffer); // print 0x8.1538p-134
// why? // 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); // double_print(stdout, d1);
// printf("double_from_string %f\r\n", double_from_string("123.456")); // 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")); // 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) static void test_calculate(void)
{ {
floatl a = floatl_from("123456789.123456789"); floatl a = floatl_from("12345678901234567890.123456789");
floatl b = floatl_from("987654321.987654321"); floatl b = floatl_from("98765432109876543210.987654321");
printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f")); printf("a %s\r\n", floatl_show(a, buffer, sizeof(buffer), "%f"));
printf("b %s\r\n", floatl_show(b, 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_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_sub(a, b), buffer, sizeof(buffer), "%f"));
printf("a * b: %s\r\n", floatl_show(floatl_mul(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_neg(a), buffer, sizeof(buffer), "%f"));
printf("|a|: %s\r\n", floatl_show(floatl_abs(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)); printf("a > b: %d\r\n", floatl_gt(a, b));