Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103406 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 9828 invoked from network); 27 Oct 2018 19:44:28 -0000 Received: from unknown (HELO mail-ua1-f53.google.com) (209.85.222.53) by pb1.pair.com with SMTP; 27 Oct 2018 19:44:28 -0000 Received: by mail-ua1-f53.google.com with SMTP id z8so1469859uap.13 for ; Sat, 27 Oct 2018 09:00:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=RBCQL1xJIrhSddrp51ywabc5E7qFx1AflsE3cqayZqE=; b=gWB0O0VS2MHgRYqmnFLqXITIhGtqh8fUU05hSOrSIcDtCGFWEDFtvBY9dOIvGauMFd KIr4e65gEga9O7F0TLeyA2NA0xFig6VpiOwG4jfi8E3mjQ5TNg/MOQRsM9IcNp6RmTBv Mp0VMU2gQ7lQr1pVBi5B1HRR0qgp0FQxiaF5C95UfruLOTAEKp4bD+7UWl7gp3CaHrrb F3h3k8FdJ6uXB7iSKLgw2xkCGx9px/vLT94RCJmC7l56fv/oKLaL/tsPnMErkmjCMtYA Er/g3/09FxUelgqoaVi9VDPno0W8DGUQFXoRD7zwfQ1pEhwYHGSfeNZ9kZkGaxmHtXCh UpGQ== X-Gm-Message-State: AGRZ1gKhTHcHaZt0LA00+uOjsrzw+/JLuvuMXVVFnJx3nS/D9lc1+fYY 5UhtFji2tel4bxHPNJ+t22UzY5h3 X-Google-Smtp-Source: AJdET5faR2JqygkdkiB6NKHhG0tuhRUPdScovnnapq4tROWW1TExSZbmGuN8Z4aegp3YOJLX9toTEQ== X-Received: by 2002:ab0:594e:: with SMTP id o14mr1862410uad.78.1540656023218; Sat, 27 Oct 2018 09:00:23 -0700 (PDT) Received: from mail-vs1-f54.google.com (mail-vs1-f54.google.com. [209.85.217.54]) by smtp.gmail.com with ESMTPSA id h207-v6sm3784091vke.50.2018.10.27.09.00.23 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 27 Oct 2018 09:00:23 -0700 (PDT) Received: by mail-vs1-f54.google.com with SMTP id w194so2687250vsc.11 for ; Sat, 27 Oct 2018 09:00:23 -0700 (PDT) X-Received: by 2002:a67:e15e:: with SMTP id o30mr2202467vsl.66.1540656022619; Sat, 27 Oct 2018 09:00:22 -0700 (PDT) MIME-Version: 1.0 References: <9BA56FEF-D2DE-4696-B765-04BE3D70C244@gmail.com> In-Reply-To: Date: Sat, 27 Oct 2018 09:00:11 -0700 X-Gmail-Original-Message-ID: Message-ID: To: Benjamin Morel Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000f312f8057937ee6e" Subject: Re: Extension: arbitrary precision decimal arithmetic in PHP 7 From: rtheunissen@php.net (Rudi Theunissen) --000000000000f312f8057937ee6e Content-Type: text/plain; charset="UTF-8" > Because the result is an infinite repeating decimal, in my opinion, your Decimal class should not allow such a division without explicitly specifying a scale and a rounding mode. In other words, I would expect an exception here. There is already an internal flag for inexact division, but is currently ignored. If exact division is a requirement, I would rather a dedicated type for that like Decimal\Exact or Decimal\Fraction. > Brick\Math does not have a concept of precision or "significant digits". It only cares about scale, and has unlimited precision. That's the main difference here. Arbitrary scale might be more intuitive and practical than arbitrary precision - I honestly don't have an opinion here. It would be interesting to compare some use cases and interoperability with SQL DECIMAL, which I assume would be a common analog for any PHP type. On Sat, Oct 27, 2018 at 3:14 AM Benjamin Morel wrote: > I recognize some of the patterns from OpenJDK's BigDecimal source. :) > > > Indeed, Brick\Math was largely inspired by Java's implementation! > > The major difference to me is scale vs precision, ie. number of >> significant digits vs number of digits behind the decimal point. Not sure >> which is better, just noticing the difference. > > > Brick\Math does not have a concept of precision or "significant digits". > It only cares about scale, and has unlimited precision. > > Its aim is to always return an exact result, unless rounding is explicitly > requested. > The scale is automatically adjusted for common operations such as plus(), > minus() and multipliedBy(). > For dividedBy(), you have to explicitly specify the requested scale of the > result and an optional rounding mode; if no rounding mode is provided, and > the result does not fit in this scale, you get an exception. > If you don't know the scale but do know that the division yields a number > with a finite scale, you can use the exactlyDividedBy(), which will either > return an exact result with the correct scale, or throw an exception. > > This is the first difference that strikes me with your current > implementation: > 0.1 / 7 == 0.01428571428571428571428571429 > > Because the result is an infinite repeating decimal, in my opinion, your > Decimal class should not allow such a division without explicitly > specifying a scale and a rounding mode. > In other words, I would expect an exception here. > > To exactly represent the result of this division, another concept such as > Brick\Math's BigRational can be used instead. > > Ben > --000000000000f312f8057937ee6e--