From owner-freebsd-current@freebsd.org Mon Jan 15 17:38:16 2018 Return-Path: Delivered-To: freebsd-current@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id ADD61EBD809 for ; Mon, 15 Jan 2018 17:38:16 +0000 (UTC) (envelope-from m.e.sanliturk@gmail.com) Received: from mail-ot0-x236.google.com (mail-ot0-x236.google.com [IPv6:2607:f8b0:4003:c0f::236]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49448C3A for ; Mon, 15 Jan 2018 17:38:16 +0000 (UTC) (envelope-from m.e.sanliturk@gmail.com) Received: by mail-ot0-x236.google.com with SMTP id p36so3239890otd.10 for ; Mon, 15 Jan 2018 09:38:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=BGxLXkogBFNk40ia3Zsr3iWOjvITpFCi3PaSUoNjC8A=; b=fJWtv8qhyp+Dg1n2gb7SbNf3mdc44BBwU2M8pZlXAekV8XE8jvuNFXY1AEq9Mn6S8S wJodpyYWymJk8dMLOWgoKfCZjZozO6fjZJpdkzDCBxOERTzidgZ7Y0ShfkBl38+JpPR2 opZGby6gQcxw6FJdeFUM60UgCOdAFcJs4ZJT0q1hYo05C6MmWn3hX39VV4lobYHcxHdL 3Cy3Hkm5I880a2GMebSbG90Ahfn3T9b7+3T1gBsByEIKRXd7Fi31qSwWGH9oGRaARr3L NCnwyauxc1jh2+BKGmEacBw1WAXUAl/d6VyZndrLHAzKLGTlujUXB7898Gu7tay79jzs Eh4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=BGxLXkogBFNk40ia3Zsr3iWOjvITpFCi3PaSUoNjC8A=; b=jTq2AKcn+c1qFEkV2LOZtAMm92OQmlUlLdk3KmAJ6N+9mtO94bk/csq/0cP1SnG/Jg KKio97JeCZcE9BnWgEvsjpBoahV24qzWc8SR2nZZKb2DX99ue9+t13EUEcVfVMOaWXOW thAjtPZPpqf915iYoXx7TD0J3QFXXEesQFl6x8wzMAA+DZh8NPnGK9mLwf5XPPxCRuWg 3TlwhjLvYJchx4ctm/Y+iskx5spDRTdgNT3InUgbKmh94Abp6W2LzQyhdeivtqgEhiyE ydvI9xp8XYuFNNN1IymLa/zw0YDCcm3fdQ/MWRfwMhPAd7AANOOrho2Qc9u8M9lgu5+U ugtg== X-Gm-Message-State: AKwxytdSpWGo4Gu7E6TM0VszhdN3+LhOk54ge3OqvZxl2eWetQ19mn/G vDGoJAuJEL8TuFCMTy/KtDiTnIzxLgLppO2kzD4= X-Google-Smtp-Source: ACJfBouFHYNPmBvOXdsMtQcwU2W5PW39ObUZ/2tD/Ye8Mf+uXFIGkC7/vr7jkonAhft8NtO6YTSgw4e2tWnCSoTjIRY= X-Received: by 10.157.82.96 with SMTP id q32mr8818724otg.376.1516037895575; Mon, 15 Jan 2018 09:38:15 -0800 (PST) MIME-Version: 1.0 Received: by 10.157.0.2 with HTTP; Mon, 15 Jan 2018 09:38:15 -0800 (PST) In-Reply-To: <6c423dbf-cd85-3c93-41e4-3362c06dfbb7@icloud.com> References: <6c423dbf-cd85-3c93-41e4-3362c06dfbb7@icloud.com> From: Mehmet Erol Sanliturk Date: Mon, 15 Jan 2018 20:38:15 +0300 Message-ID: Subject: Re: inconsistent for() and while() behavior when using floating point To: Yuri Pankov Cc: freebsd-current Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.25 X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Jan 2018 17:38:16 -0000 On Mon, Jan 15, 2018 at 5:38 PM, Yuri Pankov wrote: > Hi, > > Looking at https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=217149, I > noticed that it isn't a seq(1) problem per se, rather for() and while() > loops behaving inconsistently while using floating point, i.e.: > > double i; > > for (i = 1; i <= 2.00; i += 0.1) > printf("%g\n", i); > > would produce: > > 1 > ... > 1.9 > > but: > > double i; > > for (i = 1; i <= 2; i += 0.2) > printf("%g\n", i); > > would correctly end with 2: > > 1 > ... > 2 > > $ cc -v > FreeBSD clang version 6.0.0 (branches/release_60 321788) (based on LLVM > 6.0.0) > Target: x86_64-unknown-freebsd12.0 > Thread model: posix > InstalledDir: /usr/bin > > though gcc 4.4.4 on illumos behaves the same. > > Is this a known problem with loops and floating point numbers? > _______________________________________________ > When you perform floating point computations , it may be useful to remember that , the last bits of floating point numbers may be considered to be "noise" . For that reason , the same "for" or "while" loops may behave differently in different times and places . To make floating point related loops more deterministic , the useful steps may be to compute "step size" and "number of steps" , and use integer variables for counting loop steps with multiplication of "loop counter" and "step size" during loop steps : For floating point loop counter T = "integer loop counter" * "step size" . A statement like T = T + "step size" will/may produce wrong results if number of steps is sufficiently large . Computer arithmetic and theoretical arithmetic are not the same . For example , addition is not associative in computer arithmetic : a + ( b + c ) is not always equal to ( a + b ) + c . Mehmet Erol Sanliturk