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

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!europa.asd.contel.com!emory!swrinde!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!delfin.com!yang!gordon
  3. From: gordon@delfin.com (Gordon Hua)
  4. Subject: Re: (long) x and (long) floor(x)
  5. Message-ID: <1992Dec16.213428.2832@delfin.com>
  6. Sender: gordon@yang (Gordon Hua)
  7. Nntp-Posting-Host: merlin
  8. Reply-To: gordon@delfin.com
  9. Organization: Delfin Systems, Sunnyvale CA
  10. References: <1ghtanINNcf9@cs.tut.fi> <1gk0krINNg59@cs.tut.fi>
  11. Date: Wed, 16 Dec 1992 21:34:28 GMT
  12. Lines: 40
  13.  
  14. In article <1ghtanINNcf9@cs.tut.fi> pjh@cs.tut.fi (Haavisto Petri) writes:
  15.  
  16. >I don't have the standard, but it seems clear to me that given an
  17. >expression of type double with a positive value, the following
  18. >should always be true:
  19. >
  20. >        (long) (expression) == (long) floor(expression)
  21.  
  22. I also don't have the standard, but I have observed that the above will
  23. fail for cases where the expression is negative.  Both (long) and (int) 
  24. round toward zero, not negative infinity.  I wrote the following test
  25. program and tried it on both a SUN SparkStation and an IBM RS/6000 and 
  26. got the same results:
  27.  
  28. #include <math.h>
  29. #include <stdio.h>
  30.  
  31. main(argc, argv)
  32. int argc;
  33. char **argv;
  34. {
  35.     double x;
  36.  
  37.     if (argc > 1) {
  38.         x = atof(argv[1]);
  39.  
  40.         printf ("the number is %lf, (long) of it is %d, floor of it is %d\n", x, (long)x, (long) floor(x));
  41.         if ((long) x == (long) floor (x))
  42.             printf ("they are equal\n");
  43.         else
  44.             printf ("they are not equal\n");
  45.     }
  46.     else {
  47.         printf ("usage:  %s n\nwhere n is a (double) number\n", argv[0]);
  48.     }
  49.  
  50.     exit (0);
  51. }
  52.  
  53. Gordon
  54.