Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C round floating-point constants

A question about floating-point precision in Go made me wonder how C handles the problem.

With the following code in C:

float a = 0.1;

Will a have the closest IEEE 754 binary representation of:

00111101110011001100110011001101 (Decimal:  0.10000000149011612)

or will it just crop it to:

00111101110011001100110011001100 (Decimal:  0.09999999403953552)

Or will it differ depending on compiler/platform?

like image 424
ANisus Avatar asked Mar 12 '14 00:03

ANisus


1 Answers

An implementation is allowed to do either (or even be off by one more):

For decimal floating constants, and also for hexadecimal floating constants when FLT_RADIX is not a power of 2, the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner.

(C11, §6.4.4.2/3)

Since C99, we've had hexadecimal floating point constants so that you can specify precisely the bits you want (assuming that the implementation offers binary floating point :) ), so you could have said, for example:

float a = 0x1.99999Ap-4;

For IEEE 754 32-bit floats:

#include <stdio.h>
int main() {
  float a = 0.1;
  float low  = 0x0.1999999p0;
  float best = 0x0.199999ap0;
  float high = 0x0.199999bp0;
  printf("a is %.6a or %.16f, which is either %.16f, %.16f or %.16f\n",
          a, a, low, best, high);
  return 0;
}
like image 166
rici Avatar answered Sep 23 '22 19:09

rici