Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: 4ndre4 <626401fd-4bc7-4f8f-b641-563ae0f72920@626401fd-4bc7-4f8f-b641-563ae0f72920.invalid> Newsgroups: comp.lang.java.programmer Subject: Re: double == double Date: Wed, 12 Oct 2016 18:14:02 +0100 Organization: A noiseless patient Spider Lines: 23 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 12 Oct 2016 17:13:46 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e9ed59421007092246098aff1ef89544"; logging-data="2650"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zqeMd8V+32VDrldmbS5Zq" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Cancel-Lock: sha1:vyaSmX3ZWhmoXhxTfMH9SQmvWRE= Xref: csiph.com comp.lang.java.programmer:35751 On 12/10/2016 13:15, Stefan Ram wrote: > Sometimes it is taught that it makes no sense to compare > two double values with »==«. This is not necessarily true. It is a good rule to stick to, given that most of the times double values are results of computations, therefore subject to round-off errors, but it is not always true that == would fail. Consider this: System.out.println(1.0 == 1.0); double x = 0; for (int i=0; i<50; i++) { x += 1.0/50.0; } System.out.println(x == 1.0); On my machine, the first printing true, the second printing false. If it is granted that the double values are internally represented the same way (see the literals above), they are equal. -- 4ndre4