php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #40996 Floating point grammar omits signed numbers
Submitted: 2007-04-04 14:56 UTC Modified: 2007-08-17 10:42 UTC
From: ehanneken at pobox dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ehanneken at pobox dot com
New email:
PHP Version: OS:

 

 [2007-04-04 14:56 UTC] ehanneken at pobox dot com
Description:
------------
According to the manual (http://www.php.net/manual/en/language.types.float.php), the following grammar specifies floating point numbers:

LNUM          [0-9]+
DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})

This leaves out signed numbers.  The grammar should be written as something like

ULNUM          [0-9]+
SLNUM          [+-]{ULNUM}
DNUM           [+-]?(([0-9]*[\.]{ULNUM}) | ({ULNUM}[\.][0-9]*))
EXPONENT_DNUM  [+-]?( ({ULNUM} | {DNUM}) [eE][+-]? {ULNUM})


Reproduce code:
---------------
echo gettype(-3.14) . "\n";
echo gettype(+3.14);


Expected result:
----------------
double
double

Actual result:
--------------
double
double

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-04 15:03 UTC] ehanneken at pobox dot com
There was a bug in my revised grammer, too.  How about this?

ULNUM          [0-9]+
SLNUM          [+-]{ULNUM}
DNUM           [+-]?(([0-9]*[\.]{ULNUM}) | ({ULNUM}[\.][0-9]*))
EXPONENT_DNUM  ( ({SLNUM} | {DNUM}) [eE][+-]? {ULNUM})
 [2007-04-04 15:03 UTC] ehanneken at pobox dot com
There was a bug in my revised grammar, too.  How about this?

ULNUM          [0-9]+
SLNUM          [+-]{ULNUM}
DNUM           [+-]?(([0-9]*[\.]{ULNUM}) | ({ULNUM}[\.][0-9]*))
EXPONENT_DNUM  ( ({SLNUM} | {DNUM}) [eE][+-]? {ULNUM})
 [2007-04-08 19:40 UTC] ezyang@php.net
I wonder whether or not we should have the formal grammar at all. We're not trying to teach people how to parse floating point numbers: just tell them how to write them out. Plus, it's not even stated what the grammar is written in! (it's certainly not EBNF, not a Perl regexp due to extra spacing, what is it?)

The formal grammar was added by jeroen in v1.21 as a comment, which goba turned into an actual section later on.

The PHP parser doesn't "natively" recognize signs to the float syntax: so +-+-+-+3.4, while very strange, does work with the PHP parser. Instead, we're really using + and - operator to change the sign of the float. I.e.

-4.2 == -(4.2) to the PHP parser

For the average Joe, I don't think this distinction makes much difference though.

Finally, the revised grammar has a bug: it doesn't include "unsigned" exponential non-decimal numbers, eg. 4e4.
 [2007-08-17 10:42 UTC] vrana@php.net
Not a bug as explained by ezyang.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 02:01:35 2025 UTC