#38888 [Fbk->Opn]: gmp_init treats leading zero as octal

From: Date: Wed, 20 Sep 2006 10:06:54 +0000
Subject: #38888 [Fbk->Opn]: gmp_init treats leading zero as octal
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969374056@lists.php.net to get a copy of this message
ID: 38888 User updated by: php at richardneill dot org Reported By: php at richardneill dot org -Status: Feedback +Status: Open Bug Type: Documentation problem PHP Version: Irrelevant New Comment: 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. } =========================== Previous Comments: ------------------------------------------------------------------------ [2006-09-20 06:44:14] bjori@php.net What exactly gave you the impression it $i = 010; should be 10? IMO it is very well documented at php.net/integer and has nothing to do GMP what so ever. (I don't see your note anywhere..) ------------------------------------------------------------------------ [2006-09-19 23:57:22] 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. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=38888&edit=1

« previous php.doc (#969374056) next »