Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Joshua Cranmer Newsgroups: comp.lang.java.programmer Subject: Re: higher precision doubles Date: Mon, 08 Aug 2011 21:03:24 -0500 Organization: A noiseless patient Spider Lines: 27 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 9 Aug 2011 02:03:31 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="B3q1fNdvNsCxx/IZ4idKGA"; logging-data="27350"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tDQ+WUqkwbqqNlxYCr4/gA2ViN7ZdWmE=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: Cancel-Lock: sha1:GPoDSveX2Rn0tYvi5F/zm9ag0y8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6890 On 8/6/2011 4:20 PM, Jan Burse wrote: > And BTW the negative sign in front of the zero anyway indicates > that some rounding was going on, and not a true zero was returned > by the computation. And you could try x == 0 || x == -0, if this > is possible, and you would still get false. Several notes: 1. 0 is an integer, so -0 == 0. 2. 0.0 == -0.0 is still true, since both positive and negative zero compare as equals. Figuring out how to distinguish between the two can get really aggravating if you have one of the few cases where it actually, truly matters. 3. Java still prints -0.0 (floating point numbers are signed magnitude and therefore have both +0.0 and -0.0). And there are cases where -0.0 can come up without rounding: -1/Infinity is a classic one, as is the "other" boundary in trigonometric cases. You can get a -0.0 without it being the result of an underflow in negative numbers. 4. Don't compare equal to a floating point number, with the possible exception of 0/Infinity/NaN (that's a fun one [1]!) to test for possible overflow/underflow/invalid input cases. [1] NaN is defined in IEEE 754 such that it fails all comparison tests. Which means NaN < 0, NaN > 0, and, most bizarrely, NaN != NaN. So x == NaN will never return true. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth