Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121330 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 93720 invoked from network); 15 Oct 2023 22:17:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Oct 2023 22:17:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AA7C31804BC for ; Sun, 15 Oct 2023 15:17:27 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 15 Oct 2023 15:17:27 -0700 (PDT) Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-533c5d10dc7so6736382a12.3 for ; Sun, 15 Oct 2023 15:17:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697408245; x=1698013045; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+OFj2LICyQTEq6SNB7HDr93v/yE0PVQJ4cx8ECFec3c=; b=Dnt2IcOd1/6V02+RGxK4+EDeoIgYwMZvc0al753LxFfOLJk3cCloWvSgT/78k8tD1j lqWaw/jXM6Jysp0a5mKAOPbLtqd1bZ8yjRB40OReg4XgGevTeA5oriMLRGCUX0WZ4hRt OjgN1HBPtpZh5kGRXjSZXLVL8gscDYqSgpLZkRSBIM0PgBF7PJCbsvvEIeACJnGZabn5 AosJBoo1n92ne/bg7qeftSSD1VhX//kezoJ2PBGizaHvzH5EyJl2tUSMAfaPMzAYvoe6 bjLjxE/WP9zzDuBZgULNIRrUOC0+T9wuENLGnVE1OL0OZcG1ONM4pAi9cgzQwZwsR3yL Mfrw== X-Gm-Message-State: AOJu0YzI6SEt8MZXMPCMUQv1h+hSGRMqWIuN3RZPK7laJl6WORm2LqlD N5eEMYByPTbg3h9BZ8PTU8bx1PXH3i6AplpE1DVlYtr7 X-Google-Smtp-Source: AGHT+IEgXWbeY/ZCjszrhvh46KUXWvBHAxXHGann68kSvbSb5NIDs8hQtFnPIPHsBzrsA0rgTFw/m0iNnVEvorAu1EM= X-Received: by 2002:a05:6402:430b:b0:53e:f4:ef85 with SMTP id m11-20020a056402430b00b0053e00f4ef85mr9891369edc.10.1697408245342; Sun, 15 Oct 2023 15:17:25 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 15 Oct 2023 23:17:14 +0100 Message-ID: To: Marc Bennewitz Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000080a5ae0607c8a742" Subject: Re: [PHP-DEV] [RFC] [Discussion] Rounding Integers as int From: bukka@php.net (Jakub Zelenka) --00000000000080a5ae0607c8a742 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Marc, On Sat, Oct 14, 2023 at 1:21=E2=80=AFAM Marc Bennewitz w= rote: > Hi Jakub, > > On 13.10.23 13:25, Jakub Zelenka wrote: > > On Tue, Sep 26, 2023 at 11:39=E2=80=AFAM Marc Bennewitz > wrote: > > > >> Hi internals > >> > >> I'd like to put a new RFC under discussion: > >> https://wiki.php.net/rfc/integer-rounding > >> > >> > > I would personally prefer a new function for rounding integers if anyon= e > > wants to round large integers. > > > > The things is that the current round behaviour is consistent with a way > how > > floats to int conversion is done for strict types. > > > > > declare(strict_types=3D1); > > > > function test(float $a) { > > echo $a; > > } > > test(987654321098765432); > > > > > > So it won't really help that much if this function returns long for lon= g > in > > cases where the result is passed to another function expecting float. > > > > The main problem that I see with the current approach is that it change= s > > return type for an edge case that most people are not impacted with. Fo= r > > example it is quite usual that people had a float value with 0 fraction > > which gets json encode to int. When they decode it and use round, the > > return type is suddenly int. Even though it's usually not a problem, > there > > might be some code that expects float and maybe even assert that with > > is_float. Such code will break. > > > > On the other hand I see use some case for rounding large integers with > > negative precision. But for that to work I think it would be better to > have > > a special function. > > Your JSON example is a bit unrelated because if you care about your > types your should have used JSON_RESERVE_ZERO_FRACTION in the first > place else you should not care about int vs float at all. > > The thing is that JSON might be encoded by another application that doesn't care about types that much. In fact in JSON there is no difference between 2.0 and 2. So this is quite possible situation IMHO. > It's true that passing/returning int to/from a function expecting float > will cast but currently with these rounding functions it's a different > deal as they expect an `int|float` instead of just `float`. So it's not > cast on passing the argument but the functions itself are casting. > > Well internally yeah but effectively it is a cast on the return type. What I meant is that this wont help everywhere when it's passed to function expecting just float unless it is changed by user to accept int|float. Where another set of functions would avoid the BC break it also would be > against having PHP as a loosely typed languageputting the burden to > everyone caring. I already see hundreds of `is_int($v) ? round_int($v) : > round($v)` everywhere around. > > But for me it's sort of an edge as I would think it is not that usual rounding values that are greater than 2^53. I think it would be good to note in the RFC what other language do about this issue. I quickly checked Go and it seems to only have float64 rounding (Math.Round). Rust seems to also round just f64. I'm starting to feel that the problem is that the input type is defined as int|float. I think we should just correct it to float. Regards Jakub --00000000000080a5ae0607c8a742--