home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!world!ksr!jfw
- From: jfw@ksr.com (John F. Woods)
- Newsgroups: comp.std.c
- Subject: Re: (long) x and (long) floor(x)
- Message-ID: <20095@ksr.com>
- Date: 14 Dec 92 17:48:10 EST
- References: <1ghtanINNcf9@cs.tut.fi>
- Sender: news@ksr.com
- Lines: 25
-
- pjh@cs.tut.fi (Haavisto Petri) writes:
- >I just spent several hours debugging a simple program and before
- >crying Compiler Bug I would like to make sure that there is one.
- >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)
- >Does the standard guarantee this or is there a loop hole that has to
- >do with finite precision arithmetic and which allows the compiler to
- >do what it chooses?
-
- The most obvious thought is: do you have a proper prototype for floor()
- in scope at that point? If not, you can expect random results.
-
- The second most obvious thought:
- "If the integral part of the double cannot be represented by the integral
- type, the behavior is undefined." So, if the expression's value is 5.0E+9
- on a system with 32-bit longs, then acceptable runtime behavior would include
- growing long red tentacles and slapping you silly, or returning the current
- time, or whatever (especially including calculating two different numbers,
- and there are plausible reasons why it could).
-
- If floor() is in scope and properly declared, and the expression is in the
- range of the representation of long, then you have a compiler bug (which
- will surprise no one :-).
-