From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mengyan1223.wang (mengyan1223.wang [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 7EBDD3858C3A for ; Thu, 14 Oct 2021 15:31:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7EBDD3858C3A Received: from localhost.localdomain (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@mengyan1223.wang) by mengyan1223.wang (Postfix) with ESMTPSA id AE6E265916; Thu, 14 Oct 2021 11:31:45 -0400 (EDT) Message-ID: Subject: Re: C programing problem where <= is interpreted as < when using GCC 11.2.0 From: Xi Ruoyao To: frijolithedog 1 , "gcc-help@gcc.gnu.org" Date: Thu, 14 Oct 2021 23:31:41 +0800 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3030.0 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, JMQ_SPF_NEUTRAL, KAM_NUMSUBJECT, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Oct 2021 15:31:50 -0000 On Thu, 2021-10-14 at 15:20 +0000, frijolithedog 1 via Gcc-help wrote: > I am having a C programing problem where <= is interpreted as < when > using GCC 11.2.0 > > I was debugging a larger program which I broke down into smaller > sections of code and I noticed > the following code was not working correctly: > > #include > #include > #include ​ > > int main(void) >  { >        float n, step; > >        step = 0.1; > >            for (n = 2; n <= 10; n = n + step ) >            printf("%3.4f\n", n );                     /*  This stops > at 9.9000 and not at 10.0000  */ > >  } > > I tried the above code only using the following include statement > #include > but the result was the same. It's because 0.1 is not something can be represented precisely using float: it's binary representation is infinite. The float-type value closet to 0.1 is 0.100000001490116119384765625, which is slightly larger than 0.1. Generally you shouldn't use a floating-point value as a loop iterator unless you really know what you are doing. -- Xi Ruoyao School of Aerospace Science and Technology, Xidian University