home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / c / 3212 < prev    next >
Encoding:
Text File  |  1992-12-15  |  1.5 KB  |  54 lines

  1. Path: sparky!uunet!mcsun!news.funet.fi!news.cs.tut.fi!pjh
  2. From: pjh@cs.tut.fi (Haavisto Petri)
  3. Newsgroups: comp.std.c
  4. Subject: Re: (long) x and (long) floor(x)
  5. Date: 15 Dec 1992 07:14:35 GMT
  6. Organization: Tampere University of Technology
  7. Lines: 41
  8. Distribution: world
  9. Message-ID: <1gk0krINNg59@cs.tut.fi>
  10. References: <1ghtanINNcf9@cs.tut.fi>
  11. NNTP-Posting-Host: cs.tut.fi
  12.  
  13. In article <1ghtanINNcf9@cs.tut.fi> pjh@cs.tut.fi (Haavisto Petri) writes:
  14.  
  15. >I don't have the standard, but it seems clear to me that given an
  16. >expression of type double with a positive value, the following
  17. >should always be true:
  18. >
  19. >        (long) (expression) == (long) floor(expression)
  20.  
  21. Thank you for all those who answered (both privately and in the net).
  22. As I thought, there's no excuse for the compiler. The problem was not
  23. merely academic since the bug makes the Julian Day Number functions
  24. given in Numerical Recipes in C (and in many other places) subtly fail.
  25.  
  26. Petri Haavisto
  27.  
  28. PS. Since there was considerable interest I will post a short program
  29. reproducing the bug:
  30. ---
  31. /* Demonstrate a feature of Microsoft C v6.00 (MS-DOS) involving
  32.    conversion from double to long.
  33.    (Compiler switches: /W3 /AL /G2 /FPi87, no other switches tried). */
  34.  
  35. #include <stdio.h>
  36. #include <math.h>
  37.  
  38. #define N1 2415080L
  39. #define N2 1867216.25
  40. #define N3 36524.25
  41.  
  42. long v1(long l) { return (long) ((l - N2) / N3);}
  43. long v2(long l) { return (long) floor((l - N2) / N3);}
  44.  
  45. int main(void)
  46. {
  47.   printf(" Version 1: %ld\n Version 2: %ld\n", v1(N1), v2(N1));
  48.   return 0;
  49. }
  50. ---
  51. This prints:
  52.  
  53.  Version 1: 14
  54.