Article View: comp.std.c
Article #3027Re: (long) x and (long) floor(x)
From: Haavisto Petri
Date: Tue, 15 Dec 1992 07:14
Date: Tue, 15 Dec 1992 07:14
41 lines
1610 bytes
1610 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)
Thank you for all those who answered (both privately and in the net).
As I thought, there's no excuse for the compiler. The problem was not
merely academic since the bug makes the Julian Day Number functions
given in Numerical Recipes in C (and in many other places) subtly fail.
Petri Haavisto
PS. Since there was considerable interest I will post a short program
reproducing the bug:
---
/* Demonstrate a feature of Microsoft C v6.00 (MS-DOS) involving
conversion from double to long.
(Compiler switches: /W3 /AL /G2 /FPi87, no other switches tried). */
#include <stdio.h>
#include <math.h>
#define N1 2415080L
#define N2 1867216.25
#define N3 36524.25
long v1(long l) { return (long) ((l - N2) / N3);}
long v2(long l) { return (long) floor((l - N2) / N3);}
int main(void)
{
printf(" Version 1: %ld\n Version 2: %ld\n", v1(N1), v2(N1));
return 0;
}
---
This prints:
Version 1: 14
Technical Details
Message-ID:
<1gk0krINNg59@cs.tut.fi>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!news.funet.fi!news.cs.tut.fi!pjhReferences:
<1ghtanINNcf9@cs.tut.fi>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!news.funet.fi!news.cs.tut.fi!pjh From: pjh@cs.tut.fi (Haavisto Petri) Newsgroups: comp.std.c Subject: Re: (long) x and (long) floor(x) Date: 15 Dec 1992 07:14:35 GMT Organization: Tampere University of Technology Lines: 41 Distribution: world Message-ID: <1gk0krINNg59@cs.tut.fi> References: <1ghtanINNcf9@cs.tut.fi> NNTP-Posting-Host: cs.tut.fi