Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103405 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 50501 invoked from network); 27 Oct 2018 13:58:34 -0000 Received: from unknown (HELO mail-yw1-f46.google.com) (209.85.161.46) by pb1.pair.com with SMTP; 27 Oct 2018 13:58:34 -0000 Received: by mail-yw1-f46.google.com with SMTP id c126-v6so1485090ywd.8 for ; Sat, 27 Oct 2018 03:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=cMuFjvqz3DsIs/ysi2Ul58CbXtHzekBLyXgr0DV72nE=; b=E2hTeoo5JoPM2I52gzL7elFCGmzujFCI1G/gWgVncaN0UzZpTgQKnBX91BaWVAI3Tm 9R8RRUkwTp47i6C44UlAK2iO8Qzu5RLCg9m3tsrRoRAsEe99DxCk6sxLApBrB7akDy2o Yu2QeJDK2Ui+7GEo3HMgH1yhrs201ZMB18b0L544D6sBHMA36ZZWMK7VY/D2MjtpBosx C+MqSd5qMB2XuKIOwEiACB4+yKqdIj0qIb3R39DtLNcXHPHacPV7Phatmzm4eExLIGYy 3rECW+iLP1P2ZBtYvXKTiLNN4LqZv+7aMsXb7giB0JJ+Ha1AZlUiZ8yiXlThtk1eUMn/ /pdQ== 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=cMuFjvqz3DsIs/ysi2Ul58CbXtHzekBLyXgr0DV72nE=; b=Qauz6CSD2y516wr7fwal4Fi+SGx37EUzNUn0y/3s/bnqSnfAsMYSV1CZggCT21t8tp qhBm12Dm4Ul0guz9fNQn8Rw4vnpjXnJoJTjdifMRSEMkFRjUuuad63eUOMVpvQU0eY/w 2bQV2oQPxV9ymsb2T4l4lwDV8vCNUZpdUWZE7/C5O4INPiRoYZyWkiGA9Z/L4Brna1um oP6CUBNIR4ohNiPJmKhKBT619QfZ+F4+3ZN87MzJHAGyp8eqFgp2bULkCD0apYsg+I0L n/e+2EFw4NWjgf6xOXNWxE025qkfwsKWwhBwjCeyvwuNlIUcpUR7/brMdvrwo6MMRs6b tGSA== X-Gm-Message-State: AGRZ1gJ0ehxlBGEoYqJUzj74iAeMVE7K+uQCyX0gCazg0NLF0IknrDJG wlUPJ6MZeauplJHftXYxGOfrBYcidSlQRinZuVA= X-Google-Smtp-Source: AJdET5ctlMFqNLbfuKHOv6IS0xkYj0wnvYv2+BPhkI0UeIfVUiz9JfpIgtlNICy+iUuHb+4AatFjcfWS66rJu5Akl4s= X-Received: by 2002:a81:b710:: with SMTP id v16-v6mr6744526ywh.177.1540635265065; Sat, 27 Oct 2018 03:14:25 -0700 (PDT) MIME-Version: 1.0 References: <9BA56FEF-D2DE-4696-B765-04BE3D70C244@gmail.com> In-Reply-To: Date: Sat, 27 Oct 2018 12:14:13 +0200 Message-ID: To: Rudi Theunissen Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000b3edbf0579331943" Subject: Re: Extension: arbitrary precision decimal arithmetic in PHP 7 From: benjamin.morel@gmail.com (Benjamin Morel) --000000000000b3edbf0579331943 Content-Type: text/plain; charset="UTF-8" > > 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 --000000000000b3edbf0579331943--