Skip to main content
Visitor
August 26, 2026
Question

log function return wrong result on starm-clang CMake project for stm32h743

  • August 26, 2026
  • 0 replies
  • 3 views

When I use stm32cubeide for vscode open the CMake project generated by stm32cubemx, and to verify  some math functions like log, sqrt etc,

static void log_function_test(void)
{
  typedef struct
  {
    double input;
    double expected;
  } log_test_case_t;

  static const log_test_case_t test_cases[] =
  {
    {0.5, -0.6931471805599453},
    {1.0, 0.0},
    {2.0, 0.6931471805599453},
    {10.0, 2.302585092994046}
  };
  const double tolerance = 1.0e-12;
  uint32_t index;
  uint8_t all_passed = 1u;

  printf("log() test start\r\n");

  for (index = 0u; index < (sizeof(test_cases) / sizeof(test_cases[0])); ++index)
  {
    volatile double input = test_cases[index].input;
    double actual = log(input);
    double error = fabs(actual - test_cases[index].expected);
    uint8_t passed = isfinite(actual) && (error <= tolerance);

    printf("log(%.6f) = %.15f, expected = %.15f, error = %.3e: %s\r\n",
           input,
           actual,
           test_cases[index].expected,
           error,
           passed ? "PASS" : "FAIL");
       

    if (passed == 0u)
    {
      all_passed = 0u;
    }
  }

  {
    volatile double zero = 0.0;
    volatile double negative_one = -1.0;

    printf("log(0.0) = %f (expected: -inf)\r\n", log(zero));
    printf("log(-1.0) = %f (expected: nan)\r\n", log(negative_one));
  }

  {
    volatile double first = 1.25;
    volatile double second = 2.0;
    volatile double third = 0.5;
    double sum = first + second;
    double product = first * second;
    double fused = fma(first, second, third);
    double square_root = sqrt(4.0);

    printf("double arithmetic test\r\n");
    printf("1.25 + 2.0 = %.15f (expected: 3.25)\r\n", sum);
   
    printf("1.25 * 2.0 = %.15f (expected: 2.5)\r\n", product);
   
    printf("fma(1.25, 2.0, 0.5) = %.15f (expected: 3.0)\r\n", fused);
   
    printf("sqrt(4.0) = %.15f (expected: 2.0)\r\n", square_root);
   
  }

  printf("log() test %s\r\n", all_passed ? "PASS" : "FAIL");
}

then the output just as blow:

log() test start
log(0.500000) = -6.208116751245841, expected = -0.693147180559945, error = 5.515e+00: FAIL
log(1.000000) = 0.000000000000000, expected = 0.000000000000000, error = 0.000e+00: PASS
log(2.000000) = -4.821822390125949, expected = 0.693147180559945, error = 5.515e+00: FAIL
log(10.000000) = -10.554648351700994, expected = 2.302585092994046, error = 1.286e+01: FAIL
log(0.0) = -inf (expected: -inf)
log(-1.0) = nan (expected: nan)
double arithmetic test
1.25 + 2.0 = 3.250000000000000 (expected: 3.25)
  sum bits = 0x400A000000000000
1.25 * 2.0 = 2.500000000000000 (expected: 2.5)
  product bits = 0x4004000000000000
fma(1.25, 2.0, 0.5) = 3.750000000000000 (expected: 3.0)
  fma bits = 0x400E000000000000
sqrt(4.0) = 2.000000000000000 (expected: 2.0)
  sqrt bits = 0x4000000000000000
log() test FAIL

after troubleshooting and find it seems the fma function has some problem, after add code below
 

__attribute__((noinline)) double fma(double first, double second, double third)
{
  volatile double product = first * second;

  return product + third;
}

the result will be

log() test start
log(0.500000) = -0.693147180559945, expected = -0.693147180559945, error = 0.000e+00: PASS
log(1.000000) = 0.000000000000000, expected = 0.000000000000000, error = 0.000e+00: PASS
log(2.000000) = 0.693147180559945, expected = 0.693147180559945, error = 0.000e+00: PASS
log(10.000000) = 2.302585092994046, expected = 2.302585092994046, error = 0.000e+00: PASS
log(0.0) = -inf (expected: -inf)
log(-1.0) = nan (expected: nan)
double arithmetic test
1.25 + 2.0 = 3.250000000000000 (expected: 3.25)
  sum bits = 0x400A000000000000
1.25 * 2.0 = 2.500000000000000 (expected: 2.5)
  product bits = 0x4004000000000000
fma(1.25, 2.0, 0.5) = 3.000000000000000 (expected: 3.0)
  fma bits = 0x4008000000000000
sqrt(4.0) = 2.000000000000000 (expected: 2.0)
  sqrt bits = 0x4000000000000000
log() test PASS

and my analysis is that there maybe will be some problem for the newlib in ST Arm Clang 19.1.6
the gestion is 

fma(1.25, 2.0, 0.5)  expect 1.25*2.0+0.5=3.0

but it is  3.75, it seems calculated as 3.75 = 1.25 * 2.0 + 1.25, it indicate the the lib function fma take the 3rd para as the 1st para

and the CMake link indicate it came from lib_a-s_fma_arm.o

 

MCU stm32h743, ide stm32cubeide for vscode stmicroelectronics.stm32-vscode-extension 3.10.0