0

Tried on gcc and MSVC, both on Linux and Windows, remarkably the same result:

modf(+INFINITY) returns exact zero (0x0000000000000000 in binary representation of resulting double, 0x00000000 for float)

but modf(-INFINITY) returns 6.3e-322 (1.793662034335766e-43 for float version) (0x0000000000000080 in binary representation of resulting double, 0x00000080 for float)

Questions:

  1. Why is there a tiny non-zero value in second case?

  2. Is there any standard expected behavior for computing fractional part of infinities?

Code example:

#include <stdio.h>
#include <math.h>

int main(int argc, char* argv[])
{
  float f1;
  float f1a;
  double f2;
  double f2a;

  f1 = modff(-INFINITY,&f1a); 
  f2 = modf(-INFINITY,&f2a);

  //Dump into file to easily see the bits 
  FILE * f = fopen("a","wb");
  fwrite(&f1,4,1,f);
  fwrite(&f2,8,1,f);
  fclose(f);

  return 0;
}
  • Could you post the code that reproduces this? (Can't repro here) – Mat Jan 05 '13 at 13:22
  • Please use the edit link right under the tags to add that to your question (and use the code formatting options). Code is unreadable in comments. Also you'll need to explain what exactly are those FL types and constants. – Mat Jan 05 '13 at 13:45
  • `modf` takes two arguments. Please provide the exact code that does not give the results you expect. – Pascal Cuoq Jan 05 '13 at 13:54
  • see updated question for the standalone code sample. Sorry for prior formatting errors. – user1928550 Jan 05 '13 at 13:56

1 Answers1

1

0x00..80 on a little-endian platform is the representation for negative zero, not whatever you have up there.

modf must return negative zero for C implementations that conform to IEC 60559 floating-point arithmetic. From C99 §F.9.3.12 The modf functions:

modf(±∞, iptr) returns ±0 and stores ±∞ in the object pointed to by iptr.

Mat
  • 202,337
  • 40
  • 393
  • 406