develooper Front page | perl.perl5.porters | Postings from April 2001

[DOC PATCH bleadperl] Document underscores in numeric literals

Thread Next
From:
Philip Newton
Date:
April 27, 2001 07:40
Subject:
[DOC PATCH bleadperl] Document underscores in numeric literals
Message ID:
3AE9A0ED.5248.CEA127@localhost
I knew you were allowed to put underscores in numeric literals, and that 
you could only group digits by threes for decimal numerals. I've 
subsequently seen that underscores are also possible in binary numbers, 
but there I saw digits grouped by fours (which makes sense, since then 
you can separate nibbles). 

There's also the precedent of version numbers such as 5.005_03, which 
have an underscore in the fractional part. Such usage appears even less 
documented, but the version number examples all appear to have three 
digits after the decimal point.

However, I didn't find anything in the Camel book or perldata which says 
where underscores may be placed. Reading the source suggests the 
following:

- in decimal numeric literals, the integral part has to have digits 
grouped by threes, or a warning is issued if WARN_SYNTAX is on
- the fractional part can have underscores anywhere; they are ignored
- the exponent may not have underscores
- binary, octal, and hexadecimal numeric literals allow underscores 
anywhere; they are always ignored and no warning is generated even for 
something like 0b1_01_010_0101 (or even 0xabc___).

Apparently, the only mention in perldata is the following line:

    4_294_967_296       # underline for legibility

, which isn't terribly explicit.

On the other hand, perhaps the gory details were deliberately not 
mentioned, perhaps to prevent abuse? (3.1_4_1_5_9)

Is the "underscore in numeric literals" an official feature? If so, IMO 
it should be adequately documented. I prepared a doc patch which you may 
wish to use for this. (Oh, and I changed the "underline" to "underscore" 
in the existing example, since "underscore" is used throughout the rest 
of the document.)

Comments on the language are welcome; email copies are appreciated since 
I read the digest.

Cheers,
Philip

--- pod/perldata.pod.orig       Fri Mar  9 01:17:10 2001
+++ pod/perldata.pod    Fri Apr 27 14:30:48 2001
@@ -271,10 +271,22 @@
     12345
     12345.67
     .23E-10             # a very small number
-    4_294_967_296       # underline for legibility
+    4_294_967_296       # underscore for legibility
     0xff                # hex
     0377                # octal
     0b011011            # binary
+
+You are allowed to use an underscore in numeric literals for legibility,
+but in decimal numeric literals (those written in base 10, not
+necessarily with a fractional part), digits may only be grouped in
+threes. For decimal numeric literals containing a fractional part,
+this applies only to the part before the decimal point; the fractional
+part (but not the exponent, if given!) may contain underscores
+anywhere you feel it enhances legibility. Binary, octal, and
+hexadecimal numeric literals may contain underscores in any place --
+so you could, for example, group binary digits by threes (as for a
+Unix-style mode argument such as 0b110_100_100) or by fours (to
+represent nibbles, as in 0b1010_0110) or in other groups.

 String literals are usually delimited by either single or double
 quotes.  They work much like quotes in the standard Unix shells:
End of patch.
-- 
Philip Newton <pnewton@gmx.de>
I appreciate copies of replies to my messages to Perl5 lists.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About