Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Double/float accuracy in QNX 7.1: (3 Items)
   
Double/float accuracy in QNX 7.1  
Hi,

I am seeing an issue with accuracy of float/double in QNX 7.1.
For example, with this code:
#include <stdio.h>

int main(int argc, char** argv)
{
  double d;
  d = 1234567890123.0;
  printf("%lf\n", d);
  d = 12345678901234.0;
  printf("%lf\n", d);
  d = 123456789012345.0;
  printf("%lf\n", d);
  d = 1234567890123456.0;
  printf("%lf\n", d);
  d = 12345678901234567.0;
  printf("%lf\n", d);
  d = 123456789012345678.0;
  printf("%lf\n", d);
  d = 1234567890123456789.0;
  printf("%lf\n", d);
  d = 12345678901234567890.0;
  printf("%lf\n", d);

  d = 1e13;
  printf("%lf\n", d);
  d = 1e14;
  printf("%lf\n", d);
  d = 1e15;
  printf("%lf\n", d);
  d = 1e16;
  printf("%lf\n", d);
  d = 1e17;
  printf("%lf\n", d);
  d = 1e18;
  printf("%lf\n", d);
  d = 1e19;
  printf("%lf\n", d);
  d = 1e20;
  printf("%lf\n", d);

  return 0;
}

When I run it on a Ubuntu 18.04 PC, I get this output:
1234567890123.000000
12345678901234.000000
123456789012345.000000
1234567890123456.000000
12345678901234568.000000
123456789012345680.000000
1234567890123456768.000000
12345678901234567168.000000
10000000000000.000000
100000000000000.000000
1000000000000000.000000
10000000000000000.000000
100000000000000000.000000
1000000000000000000.000000
10000000000000000000.000000
100000000000000000000.000000

When I run it in QNX 7.1 x86 QEMU, I get this output:
1234567890123.000000
12345678901234.000000
123456789012345.000000
1234567890123456.000000
12345678901234567.999900
123456789012345679.999900
1234567890123456767.999900
12345678901234567167.999900
10000000000000.000000
100000000000000.000000
1000000000000000.000000
9999999999999999.999900
99999999999999999.999900
999999999999999999.999900
9999999999999999999.999900
99999999999999999999.000000

If my understanding is correct, the accuracy of double is 15 digits (same on QNX and Ubuntu PC).  However, as shown in 
above example, the first 15 digits are not right when float is 1e16 and above.

Is there a configuration or compiler option to allow the output be more like on the Ubuntu PC?  We ran into this issue 
when running a unit test for an open source package.  It used 1e21 in the test case which causes a test failure in QNX 7
.1.

Thanks.
Re: Double/float accuracy in QNX 7.1  
To do a strict comparison you should either be running your Linux on QEMU or be running QNX on a PC. QEMU is known to 
have flaws in its floating-point hardware emulation.

This is also very likely to be a problem with formatting floating point values for printing rather than internal 
representation and handling. If you include the hex value of the doubles in your output and they're correct it would 
point towards the problem being printf() and not some other area.
Re: Double/float accuracy in QNX 7.1  
This is to circle back and update on the outcome of this issue.  Thanks for the suggestions.

I did go back and compare the the output for Linux on QEMU and QNX on QEMU.  The output for Linux on QEMU was the same 
as on the Ubuntu 18.04 PC (i.e. the correct output).

I also compared the binary representation for Linux on QMEU and QNX on QEMU and they were the same.  So this pointed to 
a problem with the printf() library.

I reported the issue to QNX and they confirmed that it is an issue with printf() library.  They provided me a patch for 
the issue, I tested it and it resolved the issue.  It will be included in a future TBD release.

Interestingly, the issue only happens on x86 QNX.  There is a similar issue for ARM QNX, but for higher numbers.