Adding two small floating-point values produces a wrong answer

Asked by Ming Zhang

When adding two floating-point numbers with small magnitudes, the sum is wrong.
I observed such an arithmetic error when running the following C program.

#include <stdio.h>

int main()
{
    // The volatile keyword prevents compiler optimizations from replacing the
    // sum of a and b with a constant computed at compile-time.
    volatile double a, b, c;
    a = 1.916e-314;
    b = 1.141e-308;
    // The sum is observed to be incorrectly calculated, resulting in a value
    // like 5.693e-310.
    c = a + b;
    printf("%lg + %lg = %lg\n", a, b, c);
    return 0;
}

This program adds two double variables containing small positive values 1.916e-314 and 1.141e-308, respectively.
Apparently, the correct sum should be no less than 1.141e-308, but the addition produced something like 5.693e-310, which is much less than the correct sum.

Is this a feature or a bug?

I compiled the source code with the following command line:

arm-none-eabi-gcc -c -mthumb -mcpu=cortex-m3 -g -fno-lto -Wall -Wextra -Wconversion main.c

where main.c is the file containing the C source code above.
I observed the problem on a virtual LM3S6965 Stellaris LM3S Microcontroller simulated by QEMU.

Question information

Language:
English Edit question
Status:
Expired
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
john (jkovach) said :
#1

Sounds like a bug.

Revision history for this message
Launchpad Janitor (janitor) said :
#2

This question was expired because it remained in the 'Open' state without activity for the last 15 days.