Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128258 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 677B61A00BC for ; Mon, 28 Jul 2025 13:01:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753707573; bh=1hQ5I9qls72QKcYNVOWI+qpGWR4dPkL7JLdczP1xWVA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=PkbRFETcrXbzuyPcmAI/n434tQ20S7OMHAMKIHKjT/FU8rjQfizZBiZwS1KKEhtdE A1RaS/CYXUylDoftndcY3cv4K5TmgNPRFzKOd7vz9Yiw7CQ3x53Vdm5nNa5vnCFipu yeNmAzE5EkBlG3OpILKpKBb1aW1MsIL3e25YeHOj3dN/wEZLyEn/MfyzK05Dosvan2 6OokWMQ4gIY2p579npec5y9tX2eBasaKv4OiEvQLJhcbyqGIvTR3n/GFg/vu+WieQh aOjtjjhleLh29xOhD2uZbo3fWKcfRPsfgUDg/MTEIq5l3tZthOY6BC/KHUbg7UI7LS QYhPUFNbKSmBQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3F09A18007E for ; Mon, 28 Jul 2025 12:59:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 28 Jul 2025 12:59:31 +0000 (UTC) Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-ae360b6249fso761483566b.1 for ; Mon, 28 Jul 2025 06:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753707674; x=1754312474; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=cattoSTrI8z7clNB76jpMTZWJ5Ypimp9IarYgfBVvB0=; b=NZSZKpjQmdgGGHdTUO8IzkaDBsmcGujI7a0zSxCXRk6EGWNwb8DLOkHDC7O/PyxueJ Bi1Gb/uyQT/4leUNLMFOU3tlh/dXBgMHY0xgVOJIPW0ue+n1A+2bJLbyW0JEsb0RCFer FcuKKNS9WD5ppn/pY5EEI9Uk8XsSQXS0z1WEtGN0f+Ta6ZhjyNgHnkoAlSgqFDfV84dT 1qSQl2qXYyo3Zne9WSspFX9G8Ghj7z9BVMGQuQBuOyGmgHXlgPvTJH77VQSyZnVINI3i azDloFtFZGPPKzx/v5IUvuGyWRB3ee22ypELjXpsnTxlkHZrrbWLMKLL6hSsKu8VPyEW 2zsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753707674; x=1754312474; h=content-transfer-encoding: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=cattoSTrI8z7clNB76jpMTZWJ5Ypimp9IarYgfBVvB0=; b=fm7mck3K3HJurHGBnEXXrazuPU/3y+JQfEAAlP9vYv3T97g6W5MRMdGNcF3FBQ1vQw 9l8KVh/Kzf9GcgQ4Hu9KleuIQzaSlIyG8sf/uedRDLyP2o4ZKpgO/UDuoUx5hMoYUEkF zQAYyRQixal/SjE0zA/OJrwXMQofdvBVj+Ph1Pf3keTqYMYwwf16pQUMLIOf5vs0yCEo 69YtNYAtqhXy4lfXRs2cVEmTiOMG3x9VsycGUz90FZJX9xaI0+DRRZ377edcjm29UGor +5/LdCn/uyK711NMeQT2USuvQFkAH9a+UqD9Y8DEofqsp000H9UFgCHA0xFsOUvS1HHN dpsg== X-Gm-Message-State: AOJu0Yz9UKd43gCAgXmVhd+V5UOgHeGtQA3EVfOBaG+TMeZGe+Yj0zwN cFnHaB1SDxTT5xxr4VgiWM8hFi40MwBihB8IAuyymILF43ohqfa9LevIBhUdqKCb5ts6BGxTmcG CefnvUoh6J26HrRPGAbUEgU40IWRfqEU= X-Gm-Gg: ASbGncujlXC3MLE3sOFLC4pzac3uYKr0OVXvU5CRTyVB1uiXtJOXWdCOtw5vX11Z6ha 59bVNF3pNQE+YpQGZnHdeRBeMjJl4Mzpoq3Ya58nYpUQHB2pTkeo2a+zMy8zRzhUIn2OQgia/34 OOnWm41W9MWtM7lIRyoMhpUvdNguz1uKPKePgy2sd8mam6jVBC5KSokJ2BKWT5fLrM3rwIjXlYb 9a667rswB4gW3yuKrdeUw0EyT2oJPb8sq5pN/uYvA== X-Google-Smtp-Source: AGHT+IGRit+EKqy5/2uWLp47+T5uyxN1vn874J1BzVfscXxoamjuLzKJVr8VdSwhGgibrWmYe8ZLfRZ0otNwNjw0x5s= X-Received: by 2002:a17:907:7f0e:b0:ae0:b46b:decd with SMTP id a640c23a62f3a-af61e050c38mr1392791866b.31.1753707665662; Mon, 28 Jul 2025 06:01:05 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 28 Jul 2025 15:00:52 +0200 X-Gm-Features: Ac12FXzMJSEDnP7nDPMnWm530w6YRAy6m5hY-tnReW9CsNDY-cfG--T7tXtaKOE Message-ID: Subject: Re: [PHP-DEV] [DISCUSSION] Adding the "is_integer_safe()" function To: "Gina P. Banyard" Cc: PHP internals list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: alex.daubois+php@gmail.com (Alexandre Daubois) Le lun. 28 juil. 2025 =C3=A0 12:14, Gina P. Banyard a = =C3=A9crit : > For naming, maybe the following pair of functions make sense? > - is_representable_as_int() Definitely, thank you for proposing. Having a pair of functions for bidirectional transformations makes much more sense than a single ambiguous function. Here are some quick specs for what these functions could do. `is_representable_as_int(mixed $value): bool` checks if a given value can be losslessly converted to an integer. - Floats: returns true if the float is within PHP_INT_MIN to PHP_INT_MAX range AND has no fractional part - Strings: returns true if the string represents a valid integer within the platform's integer range - Always returns true for integers - Special float values: returns false for NaN, INF, -INF This gives the following example: ```php is_representable_as_int(42.0); // true is_representable_as_int(42.5); // false is_representable_as_int(2**31); // false on 32-bit, true on 64-bit is_representable_as_int(2**63); // false on both platforms is_representable_as_int("123"); // true is_representable_as_int("123.0"); // true is_representable_as_int("123.5"); // false is_representable_as_int(NAN); // false ``` > - is_representable_as_float() Now, `is_representable_as_float(mixed $value): bool`. The function would check if a value can be represented as a float without precision loss. - Integers: returns true if within the IEEE 754 safe integer range (+/-(2^53-1)) regardless of the system's PHP_INT_MAX - Strings: returns true if parseable as a float within safe precision bound= s - Floats always returns true - The IEEE 754 safe integer limit applies universally This give the following examples: ```php is_representable_as_float(2**53 - 1); // true is_representable_as_float(2**53); // false, precision loss when casted to float is_representable_as_float(PHP_INT_MAX); // false on 64-bit, true on 32-bit is_representable_as_float(42); // true is_representable_as_float("123.456"); // true is_representable_as_float("1e308"); // true is_representable_as_float("1e400"); // false ``` What do you think of this new approach? Best, Alexandre Daubois