From 085cfede8aa7b8c66e5bf263c14d3e710851db04 Mon Sep 17 00:00:00 2001 From: Lamdonn Date: Thu, 6 Mar 2025 01:33:43 +0800 Subject: [PATCH] Fix floatl print issue --- doc/floatl.en.md | 22 ++++++------- doc/floatl.md | 22 ++++++------- run.sh | 7 ++++- source/07_math/floatl.c | 6 ++-- source/07_math/floatl.h | 2 +- source/07_math/floatl_cfg.h | 37 ++++++++++++++-------- test/test_floatl.c | 61 ++++++++++++++++++++++++++++--------- 7 files changed, 102 insertions(+), 55 deletions(-) diff --git a/doc/floatl.en.md b/doc/floatl.en.md index 3b7a6b2..fb72e53 100644 --- a/doc/floatl.en.md +++ b/doc/floatl.en.md @@ -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 diff --git a/doc/floatl.md b/doc/floatl.md index 21f5e23..e603d0b 100644 --- a/doc/floatl.md +++ b/doc/floatl.md @@ -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 diff --git a/run.sh b/run.sh index f7ab983..b8449e2 100644 --- a/run.sh +++ b/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..." diff --git a/source/07_math/floatl.c b/source/07_math/floatl.c index a1678e6..a3a4d16 100644 --- a/source/07_math/floatl.c +++ b/source/07_math/floatl.c @@ -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__); diff --git a/source/07_math/floatl.h b/source/07_math/floatl.h index a21440c..71b1459 100644 --- a/source/07_math/floatl.h +++ b/source/07_math/floatl.h @@ -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. ********************************************************************************************************/ diff --git a/source/07_math/floatl_cfg.h b/source/07_math/floatl_cfg.h index 6d3f26c..325df22 100644 --- a/source/07_math/floatl_cfg.h +++ b/source/07_math/floatl_cfg.h @@ -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 ); diff --git a/test/test_floatl.c b/test/test_floatl.c index 4c567b5..410f7e4 100644 --- a/test/test_floatl.c +++ b/test/test_floatl.c @@ -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));