|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-09-19 23:57 UTC] php at richardneill dot org
Description:
------------
The documentation for gmp_init implies that a number will only be parsed as hexadecimal if it begins with 0x, and decimal otherwise.
Thus: "10" means ten
"0x10" means sixteen
"010" means ten.
However, in practice, "010" is parsed as eight!.
Not sure whether this is a documentation bug, or an actual bug though. Personally, I'd prefer "010" to mean ten, but I am aware of some conventions in which it means octal.
Confusingly:
$x=010 //$x is eight
$x="010" //$x is ten
Please also correct the comment I added to the gmp_init page - I said that it 010 was parsed as hex, when I meant octal.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 22 13:00:01 2025 UTC |
Here's why I think it matters: 1) The documentation for gmp_init explicitly states: "String representation can be decimal or hexadecimal". which implies "...but not octal" 2) gmp_init uses strings, not numbers. However, PHP does different things with strings. See Code 1: I'd expect $c and $d to behave like $b. 3) It's "common sense" that 010 means ten. I've been using PHP daily for 5 years, and (mea culpa), I'd completely forgotten that PHP does octal. Of course, I'd never normally write integers that way, but see Code 2 for an example bug, which is specific to gmp, and where I initially omitted the ltrim(,0) I also can't see my comment - I'll re-add it. ========== Code 1 ========== <? $a=010; $b="010" + 0; $c=gmp_strval(gmp_init(010)); $d=gmp_strval(gmp_init("010")); echo "a: $a, b: $b, c: $c, d: $d\n"; //prints a: 8, b: 10, c: 8, d: 8 //Expected: all 8, or all 10. ?> ========================== ========= Code 2 ========== if (preg_match("/^[0-9]+\.[0-9]+$/",$input)){ //Input is a base-10 decimal. $pieces=explode(".", $input); //Multiply as necessary to remove the decimal point. //Convert that to a gmp_resource, then decrement the //exponent to compensate. $input="$pieces[0]$pieces[1]"; //Remove the decimal point. $input=ltrim($input,'0'); //Then remove any leading zeros. We must remove leading //zeros, since otherwise, (and contrary to the //documentation), gmp_init will parse the number as //octal! Eg 0.25 becomes 025e-1 which becomes 21 * 10-1 if ($input==''){ $input=0; //Fix the case of 0.0, or it would be '' } $input=gmp_init($input); $exponent=-strlen($pieces[1]); //Exponent is (-) the number of characters after //the decimal point. } ===========================