Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103702 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 18418 invoked from network); 8 Jan 2019 21:52:51 -0000 Received: from unknown (HELO mail-yw1-f47.google.com) (209.85.161.47) by pb1.pair.com with SMTP; 8 Jan 2019 21:52:51 -0000 Received: by mail-yw1-f47.google.com with SMTP id h193so1928744ywc.4 for ; Tue, 08 Jan 2019 10:27:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:content-transfer-encoding:date:from:to:cc:subject :in-reply-to:references:message-id:user-agent; bh=XTOCKN7KAve5CLVY+kp8DSLNT2kjBw06bVhNfQAgWPU=; b=nmDAAq8J951gjd8U76fnOdpv5SyUarEqEgR2KjlVrbUboQfNqBVhI08pdPAYkm3Z1H +mhkuxDMNw/ZmUnYZ9yWF513dbL0hwl/dNNNbDUEWQbkHEqIBUSl7MzmDmJoALi+inre ptxnDsWF3tSRC4ezCHTQJjaNeuC3l3i484VvHxADlkhfOGA/tidorhhtxT2Fc1PQOrhT B+HO6761/HVaXnGNFVbLi69lSdVSkqmQrpXGtt6/gbkQZJhULU/x8rHwOPL3QlvKhJoT C1pXnriq6LK0zzMGisZFbpmlV211JivGvrhz3ndf8QccHv0YcSjuLHoKp/GuMHey6WNM nj+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:content-transfer-encoding:date:from :to:cc:subject:in-reply-to:references:message-id:user-agent; bh=XTOCKN7KAve5CLVY+kp8DSLNT2kjBw06bVhNfQAgWPU=; b=BePI7nsWvlKQXM0MZwHyQKEVr28Jvsl5M11DyNySQuNaI/a6IEkOG7eOJeayxZxf1K qWk01cs7EHjwNBkhX56IkRTta/lQVHnZYoPK/JOyt3dlseyv8LGtllHdPOOTyAFPb8bj ZqvRlku9Ot/FL06cG2SkOjqel3wohXX2zJe0aoyOGcIz7ETCY1yImpcJ3j3tIAdAClpd XTQn1VWEYSQ3YMJKKHCqMYzaUh/SY744GIQN+iiWIJoeZDuo6mvmyhevoOPTth3m2PQo L5QQlDOw3QOKMcG9k8jwehWs+ZPY7gHXV00q9pUhKOGbj6LgySFqLM8xpvXi+TWVAZ3+ tXBw== X-Gm-Message-State: AJcUukfQyGiPNCiX/GtTkJg6TSlA0FJQz25uJMhB8rZ3ZXiF/r6IDjXG o3vJXu3vbTeozTNUyZ1Lysm7nGT+ X-Google-Smtp-Source: ALg8bN5fJOpwS7VbZhrxmLd5IZ9VtZcZhzTGzsCMMVEniDJ032hMA+MRV5NxZSwnvbUi8Ov+2es8JQ== X-Received: by 2002:a81:b342:: with SMTP id r63mr2666901ywh.475.1546972022268; Tue, 08 Jan 2019 10:27:02 -0800 (PST) Received: from k-piste.fi (k-piste.fi. [178.62.210.197]) by smtp.gmail.com with ESMTPSA id a71sm35693784ywe.66.2019.01.08.10.27.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jan 2019 10:27:01 -0800 (PST) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Date: Tue, 08 Jan 2019 20:26:59 +0200 To: "Legale.legale" Cc: internals@lists.php.net In-Reply-To: <5c33a5db.1c69fb81.eed82.f2ab@mx.google.com> References: <5c33a5db.1c69fb81.eed82.f2ab@mx.google.com> Message-ID: <3fc7802760ad01d9f77ea6aa2f06b789@gmail.com> X-Sender: lauri.kentta@gmail.com User-Agent: Roundcube Webmail/1.3.8 Subject: Re: [PHP-DEV] Proposal: change precision for output functions From: lauri.kentta@gmail.com (=?UTF-8?Q?Lauri_Kentt=C3=A4?=) On 2019-01-07 21:17, Legale.legale wrote: > I think your solution by changing precision is not good enough because > float summation is still not working properly. > > var_dump(0.1 + 0.7); > > returns: > > 0.7999999999 > > expected: 0.8 As you may know, most computers use binary numbers. This applies to floating-point values as well. They have a limited number of binary digits. Many base-10 numbers cannot be expressed exactly in base-2 with limited number of digits. When you write 0.1 + 0.7, what you get (with double-precision floats) is closer to 0.1000000000000000055511 + 0.6999999999999999555911 = 0.7999999999999999333866. This is expected and proper and well known. You have to use formatting functions like number_format if you need neatly rounded base-10 output. There are also a lot of libraries for precise base-10 calculations. They will be a lot slower than native binary floating-point calculations, though. Regards, Lauri Kenttä