Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113096 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 89922 invoked from network); 6 Feb 2021 00:02:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 6 Feb 2021 00:02:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B20C81804F2 for ; Fri, 5 Feb 2021 15:46:10 -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-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (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 15:46:10 -0800 (PST) Received: by mail-oi1-f181.google.com with SMTP id m7so9280647oiw.12 for ; Fri, 05 Feb 2021 15:46:10 -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=Q6i64rcHZD5WYAp4iqy9DNFDYg5Q0VKNZx+ABjwSK+4=; b=jW0XOUt5qAIuhrBHDM9y/J56iG+0iLMBgCIbbu88uHd1CQrhhHUYkT+QJN87IaLgWB A99KEtdrlI6z2HhA7IYvL1PjQ+48vQn9cn3i8mE3kgWZ5HJ96fdn/zmbdD3blYQnKLa7 lD0sq0dyxq/cFL19cBUFeaR5KikDXPOUOT4jR4t6zaxvq7R+IzFSjlQU2/a01BhwSZ9M MCeFFFGiOrHCtFzZVHydWD/b5iYGAwEuJ/9Ab5XOjsmLzlhxk5Gry71SvEREvYgGCegA x5k/bFBbDzmk1XtNft4HpGD6gb8o1gq92gl+2HT//ZegtNqwUPYCLQHyLcdXYVH30EZj 9uTQ== 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=Q6i64rcHZD5WYAp4iqy9DNFDYg5Q0VKNZx+ABjwSK+4=; b=kPtLKxU5AbFL+s5qDGEeWgoSxfa4ViVO2+mF+J7RhSG28kUbtAEj0zpnDFSzeZErzg qaEZUqV57AjbAV1PYCXSQ8Fd7TwOsqBpT70IhAdtI+hff+ZeTBj20iVoPtOYPdkEF29X FV8X4GpTR6c6uvBVISuDxqDv8uelGWBWNtO2KBvVc8i1nHjHn8CEAOE2fonSCOSNN12Y ylZWoR6qaxRORFFI1MJNmLyrlBUUP5MXCm9GagEMFd4Q1jyDU7Z5M58yBhPA4QDn0MPk /F77hgOx/tDfCapy/69woKUW8g3n4fRxn2QOTQIWB/9bgdroVPr+ZH11IDg/u0J2MCJY k3gg== X-Gm-Message-State: AOAM531audE4adBamqyQ3+drX0DavWjR7IMbL484zefqQF5geMzFMPIn SsFnQ4hOm+EN956VxucHTyYIDzfeKRCqJBn19Wk= X-Google-Smtp-Source: ABdhPJyV3dNF/fJa2UiMaeNH23JQnRLUZ7tj5RYu2p2/Uo9vseasga4PPsIU6o/Bt8r2FfbR9B1azjKngm/0jojI6Xg= X-Received: by 2002:aca:c7c8:: with SMTP id x191mr2850876oif.152.1612568769788; Fri, 05 Feb 2021 15:46:09 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 5 Feb 2021 23:45:59 +0000 Message-ID: To: Benjamin Morel Cc: AllenJB , "G. P. B." , PHP internals Content-Type: multipart/alternative; boundary="000000000000b2ff3705ba9f6cd5" Subject: Re: [PHP-DEV] [RFC] Warning for implicit float to int conversions From: davidgebler@gmail.com (David Gebler) --000000000000b2ff3705ba9f6cd5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > 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() could > therefore be safely passed to (int) even if the cast operator checked for a > 0 fractional part, which is what I'm advocating for. Generating a warning on explicit casts of (non-integer) floats to int would 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 favour a notice or deprecation, were it to be done at all). 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 only > > OK to explictly cast a float that's the return value of a function? Or > > only explictly cast a float if the fractional part is .0? Is that viabl= e > > given the "inaccuracy" of floats? Or would it be better for PHP to have > > 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() could > 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 certain > > value and want the result explicitly as an int. > > > Perfect, so (int) floor() would work wonders for you, even with the stric= t > casting I'm talking about. > And if the result does overflow an integer one day, I'm sure you'd be hap= py > to know it by getting an exception, rather than getting silently ZERO: > > echo (int) 1e60; // 0 > > =E2=80=94 Benjamin > --000000000000b2ff3705ba9f6cd5--