Article View: comp.std.c
Article #3033Re: (long) x and (long) floor(x)
From: Gordon Hua
Date: Wed, 16 Dec 1992 21:34
Date: Wed, 16 Dec 1992 21:34
40 lines
1718 bytes
1718 bytes
In article <1ghtanINNcf9@cs.tut.fi> pjh@cs.tut.fi (Haavisto Petri) writes:
>I don't have the standard, but it seems clear to me that given an
>expression of type double with a positive value, the following
>should always be true:
>
> (long) (expression) == (long) floor(expression)
I also don't have the standard, but I have observed that the above will
fail for cases where the expression is negative. Both (long) and (int)
round toward zero, not negative infinity. I wrote the following test
program and tried it on both a SUN SparkStation and an IBM RS/6000 and
got the same results:
#include <math.h>
#include <stdio.h>
main(argc, argv)
int argc;
char **argv;
{
double x;
if (argc > 1) {
x = atof(argv[1]);
printf ("the number is %lf, (long) of it is %d, floor of it is %d\n", x, (long)x, (long) floor(x));
if ((long) x == (long) floor (x))
printf ("they are equal\n");
else
printf ("they are not equal\n");
}
else {
printf ("usage: %s n\nwhere n is a (double) number\n", argv[0]);
}
exit (0);
}
Gordon
Technical Details
Message-ID:
<1992Dec16.213428.2832@delfin.com>Path:
arc1-ca.pugleaf.net!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!gordonReferences:
<1ghtanINNcf9@cs.tut.fi> <1gk0krINNg59@cs.tut.fi>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!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 From: gordon@delfin.com (Gordon Hua) Subject: Re: (long) x and (long) floor(x) Message-ID: <1992Dec16.213428.2832@delfin.com> Sender: gordon@yang (Gordon Hua) Nntp-Posting-Host: merlin Reply-To: gordon@delfin.com Organization: Delfin Systems, Sunnyvale CA References: <1ghtanINNcf9@cs.tut.fi> <1gk0krINNg59@cs.tut.fi> Date: Wed, 16 Dec 1992 21:34:28 GMT Lines: 40