Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113098 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 95790 invoked from network); 6 Feb 2021 00:40:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 6 Feb 2021 00:40:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A11BF180089 for ; Fri, 5 Feb 2021 16:24:16 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f172.google.com (mail-vk1-f172.google.com [209.85.221.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 5 Feb 2021 16:24:15 -0800 (PST) Received: by mail-vk1-f172.google.com with SMTP id u22so1872475vke.9 for ; Fri, 05 Feb 2021 16:24:15 -0800 (PST) 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=aecBg1bsQjrmWqzz/PENyd2EDzw5GS3a+Lt8BkhXC2I=; b=QVd2eThZ6UUCCxCQ2kquagupJWtDVgNHZZZPlBDhHHgD9WSTS3/63HcbtF1EWG2SIj kKAwdXOAhV9JAoDqyjb6ONvOnSROcf2QNoCC2rFAwggxf+ocmIX6QyPbsUmB9aaViVQM I7aatwQygOS4fwhziyO9kyvZ+IHIynTSpHKJLVvpC9TyeKooisLAr4ZW53eo+cfFuUC9 fi7vE8Dyxv4t5LPDZx6U4Vo5Rg5c1EnULEJHpcvU8GY1Vt9A5EcRcPnFyoJKbhHBTpD1 MKYIk2I2bLMT3SOtDt7xBtsTE0X5BnwlVUu+Muzp2eqCtxI9/YIiyQMXNdpKL5xV3I25 it/Q== 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=aecBg1bsQjrmWqzz/PENyd2EDzw5GS3a+Lt8BkhXC2I=; b=ulR+fxtYuMFimsQNSAy7iikl5RtdTJ+Jfy5JFz2hOEveDdM5VxJmMnI7weBSU6Qcxo YuXhAhzGfMctIPo+eAK1h4b/N2VyOAn+6ZbHUKgefivp6gDC1NFJJbIKBDvH66btZW/g oeCLpf/Kybvb3ChT0q/G6aHkEqLygycaeVnMg2RIsQiXTzjCyDnAnQw/qRQpE9AXqoi5 KrrUpqnCeMCCxt4nA1bX1d5ZuV6q+vgd6p+wpeWZgl6VmdX9V5FmF+8uvVK/IsvHWnlZ xJvPoPTEWXOgJUjYXyuM/IU8ZLpB1LVv/sPTCo5X/GIKynWrADPjBxWYWy7qrE9PXGtV FNLg== X-Gm-Message-State: AOAM53295SAZ/gohAwtFqza1fM7FZuz5xMX2xcVwAlw+h9p/3Tt+OUKc XnsUEVWFMrYe7LGyX9hWP+dHhEZve+vrRcrKwe4= X-Google-Smtp-Source: ABdhPJxpjIotlJ4dJhQrRopwXVOzVUxFowhi5dsXTgiyrzknf31FCV1IFoDwaoSzOHFJhmyL/QF/P658vloXG4F837Y= X-Received: by 2002:a1f:9d43:: with SMTP id g64mr5343988vke.16.1612571054565; Fri, 05 Feb 2021 16:24:14 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 5 Feb 2021 19:24:06 -0500 Message-ID: To: David Gebler Cc: Benjamin Morel , AllenJB , "G. P. B." , PHP internals Content-Type: multipart/alternative; boundary="000000000000e1eeb905ba9ff424" Subject: Re: [PHP-DEV] [RFC] Warning for implicit float to int conversions From: chasepeeler@gmail.com (Chase Peeler) --000000000000e1eeb905ba9ff424 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Feb 5, 2021 at 6:46 PM David Gebler wrote: > > Floats (doubles) can accurately represent all integers up to 2=E2=81=B5= =C2=B3, so > there > > is no inaccuracy in this range; the result from round() or floor() coul= d > > therefore be safely passed to (int) even if the cast operator checked f= or > a > > 0 fractional part, which is what I'm advocating for. > > Generating a warning on explicit casts of (non-integer) floats to int wou= ld > IMO make no sense at all, it would put PHP at odds with other major > languages such as C, Python and Java and go against normal, reasonable > expectations of how a programming language behaves. > > You said in an earlier comment "it's better to be explicit about your > intent", but doing something like (int)3.5 *is* being explicit about your > intent - and truncating casts on float to int is the widely established > norm. > > This was exactly my reservation about deprecating this behaviour even as = an > implicit cast - in my mind it isn't a bug or flaw, it's there by design. > > If developers want to round/ceil/floor/do whatever with a float prior to > using it as an int, they already have that option and the greatest > flexibility. > > At least with the implicit case, I understand the motivation and argument > for bringing coercion more in line with strict typing behaviour and > catching cases where such a cast may not have been intentional (though I > still think a warning is too high an error level for this and would favou= r > a notice or deprecation, were it to be done at all). > > A notice is fine, but PLEASE don't make it a warning. I'm in the process of upgrading to 8.0 right now and I have so much code that works perfectly fine but generates warnings (undefined array key for example - in 99.9% of the cases where an array key is not defined, the null value that used to result from that was perfectly fine). > > On Fri, Feb 5, 2021 at 12:52 PM Benjamin Morel > wrote: > > > On Fri, 5 Feb 2021 at 11:56, AllenJB wrote: > > > > > (And after checking the manual, I'd also note here that round() also > > > returns a float, so how exactly does your example here work? Is it on= ly > > > OK to explictly cast a float that's the return value of a function? O= r > > > only explictly cast a float if the fractional part is .0? Is that > viable > > > given the "inaccuracy" of floats? Or would it be better for PHP to ha= ve > > > some non-range/accuracy-sensitive representation for integers (and > > > decimals) here?) (and now we're getting into "why are we still using > > > floating point math by default in 2021" territory, so I'll stop right > > here) > > > > > > > Floats (doubles) can accurately represent all integers up to 2=E2=81=B5= =C2=B3, so > there > > is no inaccuracy in this range; the result from round() or floor() coul= d > > therefore be safely passed to (int) even if the cast operator checked > for a > > 0 fractional part, which is what I'm advocating for. > > > > There are legitimate cases for explicitly casting floats to int. For > > > example floor() outputs a float, but in the context of the domain I'm > > > working I might know that the result is never going to exceed a certa= in > > > value and want the result explicitly as an int. > > > > > > Perfect, so (int) floor() would work wonders for you, even with the > strict > > casting I'm talking about. > > And if the result does overflow an integer one day, I'm sure you'd be > happy > > to know it by getting an exception, rather than getting silently ZERO: > > > > echo (int) 1e60; // 0 > > > > =E2=80=94 Benjamin > > > --=20 Chase Peeler chasepeeler@gmail.com --000000000000e1eeb905ba9ff424--