CloudFlare suffered a massive security issue affecting all of its customers, including Rosetta Code. All passwords not changed since February 19th 2017 have been expired, and session cookie longevity will be reduced until late March.--Michael Mol (talk) 05:15, 25 February 2017 (UTC)

Literals/Floating point

Literals/Floating point
You are encouraged to solve this task according to the task description, using any language you may know.

Programming languages have different ways of expressing floating-point literals.

Show how floating-point literals can be expressed in your language: decimal or other bases, exponential notation, and any other special features.

You may want to include a regular expression or BNF/ABNF/EBNF defining allowable formats for your language.

Real literals contain decimal point. The exponent part is optional. Underline may be used to separate groups of digits. A literal does not have sign, + or - are unary operations. Examples of real literals:

 3.141_592_61.0E-120.13

Aime

3.145.08r      # without the "r"(eal) suffix, "8" would be an integer.125

ALGOL 68

# floating point literals are called REAL denotations in Algol 68      ## They have the following forms:                                       ##     1: a digit sequence followed by "." followed by a digit sequence ##     2: a "." followed by a digit sequence                            ##     3: forms 1 or 2 followed by "e" followed by an optional sign     ##                     followed by a digit sequence                     ##     4: a digit sequence follows by "e" followed by an optional sign  ##                     followed by a digit sequence                     ##                                                                      ## The "e" indicates the following optionally-signed digit sequence is  ## the exponent of the literal.                                         ## If the implementation allows, a "times ten to the power symbol"      ## can be used to replace "e" - e.g. a subscript "10" character         ##                                                                      ## spaces can appear anywhere in the denotation                         ## Examples:                                                            #REAL r;r := 1.234;r :=  .987;r := 4.2e-9;r := .4e+23;r := 1e10;r := 3.142e-23;r := 1 234 567 .      9           e -                  4;

ALGOL W

begin    real r; long real lr;    % floating point literals have the following forms:                      %    %    1 - a digit sequence followed by "." followed by a digit sequence   %    %    2 - a digit sequence followed by "."                                %    %    3 - "." followed by a digit sequence                                %    %    4 - one of the above, followed by "'" followed by an optional sign  %    %        folloed by a digit sequence                                     %    % the literal can be followed by "L", indicating it is long real         %    % the literal can be followed by "I", indicating it is imaginary         %    % the literal can be followed by "LI" or "IL" indicating it is a long    %    % imaginary number                                                       %    % an integer literal ( digit sequence ) can also be used where a         %    % floating point literal is required                                     %    % non-imaginary examples:                                                %    r  := 1.23;    r  := 1.;    r  := .9;    r  := 1.23'5;    r  := 1.'+4;    r  := .9'-12;    r  := 7;    lr := 5.4321L;end.

Applesoft BASIC

All numeric literals are treated as floating point. (In the Apple II world, Applesoft was sometimes called "floating-point BASIC" to contrast it with Integer BASIC.)

0
19
-3
29.59
-239.4
1E10
1.9E+09
-6.66E-32


AWK

With the One True Awk (nawk), all numbers are floating-point. A numeric literal consists of one or more digits '0-9', with an optional decimal point '.', followed by an optional exponent. The exponent is a letter 'E' or 'e', then an optional '+' or '-' sign, then one or more digits '0-9'.

22..345e645e+678e-91.2E34

Other implementations of Awk can differ. They might not use floating-point numbers for integers.

This Awk program will detect whether each line of input contains a valid integer.

Nemerle

3.14f                                   // float literal
3.14d, 3.14                             // double literal
3.14m                                   // decimal literal

Formally (from the Reference Manual):

<floating_point_literal> ::=
[ <digits_> ] '.' <digits_> [ <exponent> ] [ <suffix> ]
|       <digits_> <exponent> [ <suffix> ]
|       <digits_> <suffix>
<exponent> ::=
<exponential_marker> [ <sign> ] <digits>
<digits> ::=
{ <digit> }
<digits_> ::=
<digits> [ { '_' <digits> } ]
<exponential_marker> ::=
'e'
|       'E'
<sign> ::=
'+'
|       '-'
<digit> ::=
<decimal_digit>
<suffix> ::=
<floating_point_suffix>
<floating_point_suffix> ::=
'f'
|       'd'
|       'm'

NetRexx

NetRexx supports decimal and exponential notation for floating point constants. A number in exponential notation is a simple number followed immediately by the sequence "E" (or "e"), followed immediately by a sign ("+" or "-"), followed immediately by one or more digits.

NetRexx supports floating point number notation in the primitive float and double types, it's built in Rexx object and any other Java object that supports floating point numbers.

/* NetRexx */options replace format comments java crossref symbols nobinary numeric digits 40 -- make lots of space for big numbersnumeric form scientific -- set output form for exponential notation say 'Sample using objects of type "Rexx" (default):'fv =      1.5; say      '1.5'.right(20) '==' normalize(fv).right(20) --            1.5fv =     -1.5; say     '-1.5'.right(20) '==' normalize(fv).right(20) --           -1.5fv =    15e-1; say    '15e-1'.right(20) '==' normalize(fv).right(20) --            1.5fv =    3e-12; say    '3e-12'.right(20) '==' normalize(fv).right(20) --          3E-12fv =    3e+12; say    '3e+12'.right(20) '==' normalize(fv).right(20) --  3000000000000fv = 17.3E-12; say '17.3E-12'.right(20) '==' normalize(fv).right(20) --       1.73E-11fv = 17.3E+12; say '17.3E+12'.right(20) '==' normalize(fv).right(20) -- 17300000000000fv = 17.3E+40; say '17.3E+40'.right(20) '==' normalize(fv).right(20) --       1.73E+41fv = 0.033e+9; say '0.033e+9'.right(20) '==' normalize(fv).right(20) --       33000000fv = 0.033e-9; say '0.033e-9'.right(20) '==' normalize(fv).right(20) --        3.3E-11say say 'Sample using primitive type "float":'ff = floatff = float    15e-1; say    '15e-1'.right(20) '==' normalize(ff).right(20) --            1.5ff = float 17.3E-12; say '17.3E-12'.right(20) '==' normalize(ff).right(20) --       1.73E-11ff = float 17.3E+12; say '17.3E+12'.right(20) '==' normalize(ff).right(20) -- 17300000000000ff = float 0.033E+9; say '0.033E+9'.right(20) '==' normalize(ff).right(20) --       33000000ff = float 0.033E-9; say '0.033E-9'.right(20) '==' normalize(ff).right(20) --        3.3E-11say say 'Sample using primitive type "double":'fd = doublefd =    15e-1; say    '15e-1'.right(20) '==' normalize(fd).right(20) --            1.5fd = 17.3E-12; say '17.3E-12'.right(20) '==' normalize(fd).right(20) --       1.73E-11fd = 17.3E+12; say '17.3E+12'.right(20) '==' normalize(fd).right(20) -- 17300000000000fd = 17.3E+40; say '17.3E+40'.right(20) '==' normalize(fd).right(20) --       1.73E+41fd = 0.033E+9; say '0.033E+9'.right(20) '==' normalize(fd).right(20) --       33000000fd = 0.033E-9; say '0.033E-9'.right(20) '==' normalize(fd).right(20) --        3.3E-11say return /** * Convert input to a Rexx object and add zero to the value which forces NetRexx to change its internal representation * * @param fv a Rexx object containing the floating point value * @return a Rexx object which allows NetRexx string manipulation methods to act on it */method normalize(fv) private constant  return fv + 0

Output:

Sample using objects of type "Rexx" (default):
1.5 ==                  1.5
-1.5 ==                 -1.5
15e-1 ==                  1.5
3e-12 ==                3E-12
3e+12 ==        3000000000000
17.3E-12 ==             1.73E-11
17.3E+12 ==       17300000000000
17.3E+40 ==             1.73E+41
0.033e+9 ==             33000000
0.033e-9 ==              3.3E-11

Sample using primitive type "float":
15e-1 ==                  1.5
17.3E-12 ==             1.73E-11
17.3E+12 ==       17300000000000
0.033E+9 ==             33000000
0.033E-9 ==              3.3E-11

Sample using primitive type "double":
15e-1 ==                  1.5
17.3E-12 ==             1.73E-11
17.3E+12 ==       17300000000000
17.3E+40 ==             1.73E+41
0.033E+9 ==             33000000
0.033E-9 ==              3.3E-11


Nim

var x: floatx = 2.3x = 2.0x = 0.3x = 123_456_789.000_000_1x = 2e10x = 2.5e10x = 2.523_123E10x = 5.2e-10 var y = 2'f32 # Automatically a float32var z = 2'f64 # Automatically a float64

Objeck

 3 + .141593.14159314.159E-2

OCaml

In the OCaml manual, the chapter lexical conventions describes floating-point literals, which are:

float-literal  ::=   [-] (0…9) { 0…9∣ _ } [. { 0…9∣ _ }] [(e∣ E) [+∣ -] (0…9) { 0…9∣ _ }]


Here are some examples:

0.51.01.    (* it is not possible to write only "1" because OCaml is strongly typed,         and this would be interpreted as an integer *)1e-103.14159_26535_89793

Oforth

A literal floating point number is written with a . and with or without an exponential notation :

3.141.0e-120.131000.0.22

PARI/GP

Similar to C, but allowing only decimal. Also, GP allows a trailing decimal point:

[+-]?((\d*\.\d+\b)|(\d+(\.\d*)?[Ee][+-]?\d+\b)|-?(\.\d+[Ee][+-]?\d+\b)|(\d+\.))

PARI t_REAL numbers have a maximum value of

 32-bit 64-bit ${\displaystyle 2^{2^{29}}(1-\epsilon )}$ 161,614,249 decimal digits ${\displaystyle 2^{2^{61}}(1-\epsilon )}$ 694,127,911,065,419,642 decimal digits

where ${\displaystyle \epsilon }$ is the machine epsilon at the selected precision. The minimum value is the opposite of the maximum value (reverse the sign bit).

Pascal

1.345
-0.5345
5.34e-34


Perl

# Standard notations:.5;0.5;1.23345e10;1.23445e-10;# The numbers can be grouped:100_000_000;	# equals to 100000000

Perl 6

Floating point numbers (the Num type) are written in the standard 'e' scientific notation:

2e2      # same as 200e0, 2e2, 200.0e0  and 2.0e26.02e23-2e481e-91e0

A number like 3.1416 is specifically not floating point, but rational (the Rat type), equivalent to 3927/1250. On the other hand, Num(3.1416) would be considered a floating literal though by virtue of mandatory constant folding.

Phix

Phix does not require any distinction between integers and floats: 5 and 5.0 are exactly the same. A variable declared as atom can hold an integer or a floating point value.
Division and other operators do what a sensible language should, eg 1/3 is 0.333333, not 0. [for the latter use floor(1/3)]
Floats cannot be expressed in any base other than decimal. They may optinally include a sign for mantissa and/or exponent.
It is not necessary for a digit to precede a decimal point, but one must follow it. Upper or lower e/g may be used.
In the 32-bit version, integers outside -1,073,741,824 to +1,073,741,823 must be stored as atoms. In the 64-bit version the limits of integers are -4,611,686,018,427,387,904 to +4,611,686,018,427,387,903.
On a 32-bit architecture floats can range from approximately -1e308 to +1e308 with 15 decimal digits, and on a 64-bit architecture they can range from approximately -1e4932 to +1e4932 with 19 decimal digits.
The included bigatom library allows working with extremely large integers and floats with arbitrary precision. In the following, '?x' is the Phix shorthand for 'print(1,x)', plus \n

?1e+12  -- (same as 1e12)?1e-12?5      -- (same as 5.0)--?1.   -- (illegal, use 1 or 1.0)?.1     -- (same as 0.1)?1/3    -- 0.333333printf(1,"%g %G\n",1e-30)
Output:
1e+12
1e-12
5
0.1
0.3333333333
1e-30 1E-30


PHP

.120.12341.2e37E-10

Formal representation:

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


PicoLisp

PicoLisp does not support floating point literals in the base language, only fixed point (scaled) decimal integers of unlimited size and precision. See Numbers in the reference.

PL/I

 1.2345e-4      decimal floating-point7e5            decimal floating-point1.234_567_89e0 decimal floating-point.1.0s0          decimal floating-point (single precision)1.0d0          decimal floating-point (double precision)1.34q0         decimal floating-point (quadruple/extended precision) 111.0101e7b    binary floating-point equals 111.0101 * 2**7                                     or 7.3125 * 2**71e5b           binary floating-point equals 1 * 2**5

PureBasic

Floating point literals do not need a decimal point if an exponent is used. They may also include a sign for the number or exponent.

-1.0   1.0  1.0E2  1.0E+2  1.0E-2  -1E2

Python

Works with: Python version 2.3.3

This is an excerpt of an ANTLR grammar for python obtained from here.

FLOAT    :   '.' DIGITS (Exponent)?    |   DIGITS '.' Exponent    |   DIGITS ('.' (DIGITS (Exponent)?)? | Exponent)    ; DIGITS : ( '0' .. '9' )+ ; Exponent    :    ('e' | 'E') ( '+' | '-' )? DIGITS    ;

Examples

 2.3    # 2.2999999999999998.3     # 0.29999999999999999.3e4   # 3000.0.3e+34 # 2.9999999999999998e+33.3e-34 # 2.9999999999999999e-352.e34  # 1.9999999999999999e+34

Racket

 #lang racket.22.2.+0i  ; zero imaginary part2e0#x10.8 ; hex float#o1e2  ; oct float2.0f0  ; single float1.0t0  ; extended 80-bit float (when available on platform)

Output:

0.2
2.0
2.0
2.0
16.5
64.0
2.0f0
1.0t0


REXX

All values in REXX are character strings,   so a value could hold such things as these (decimal) numbers:

something = 127something = '127'     /*exactly the same as the above. */something = 1.27e2something = 1.27E2something = 1.27E+2something = '    +    0001.27e+00000000000000002     '

To forcibly express a value in exponential notation,   REXX has a built-in function   format   that can be used.

Note that a value of   0   (zero)   in any form is always converted to

  0


by the   format   BIF.

something = -.00478say somethingsay format(something,,,,0)

output

-0.00478
-4.78E-3


The last invocation of   format   (above,   with the 5th parameter equal to zero)   forces exponential notation,   unless the exponent is   0   (zero),   then exponential notation won't be used.

There are other options for the   format   BIF to force any number of digits before and/or after the decimal point,   and/or specifying the number of digits in the exponent.

Ruby

A Float literal is an optional sign followed by one or more digits and a dot, one or more digits and an optional exponent (e or E followed by an optional sign and one or more digits). Unlike many languages .1 is not a valid float.

Underscores can be used for clarity: 1_000_000_000.01

Rust

2.3         // Normal floating point literal3.          // Equivalent to 3.0 (3 would be interpreted as an integer)2f64        // The type (in this case f64, a 64-bit floating point number) may be appended to the value1_000.2_f32 // Underscores may appear anywhere in the number for clarity.<lang rust> =={{header|Scala}}=={{libheader|Scala}}As all values in Scala, values are boxed with wrapper classes. The compiler will unbox them to primitive types for run-time execution.<lang Scala>1. //Double equal to 1.01.0 //Double, a 64-bit IEEE-754 floating point number (equivalent to Java's double primitive type) 2432311.7567374 //Double1.234E-10 //Double1.234e-10 //Double758832d //Double728832f //32-bit IEEE-754 floating point number (equivalent to Java's float primitive type)1.0f //Float758832D //Double728832F //Float1.0F //Float1 / 2. //Double1 / 2 //Int equal to 0 // ConstantsFloat.MinPositiveValueFloat.NaNFloat.PositiveInfinityFloat.NegativeInfinity Double.MinPositiveValueDouble.NaNDouble.PositiveInfinityDouble.NegativeInfinity

Values that are outside the bounds of a type will give compiler-time errors when trying to force them to that type.

Scheme

 .2      ; 0.22.      ; 2.02e3     ; 20002.+3.i  ; complex floating-point number ; in Scheme, floating-point numbers are inexact numbers(inexact? 2.); #t(inexact? 2); #f

Seed7

The type float consists of single precision floating point numbers. Float literals are base 10 and contain a decimal point. There must be at least one digit before and after the decimal point. An exponent part, which is introduced with E or e, is optional. The exponent can be signed, but the mantissa is not. A literal does not have a sign, + or - are unary operations. Examples of float literals are:

 3.141592653589791.0E-120.1234

The functions str and the operators digits and parse create and accept float literals with sign.

Original source: [1]

Sidef

say 1.234;say .1234;say 1234e-5;say 12.34e5;
Output:
1.234
0.1234
0.01234
1234000

Smalltalk

2.0       45e6      45e+6     78e-9     1.2E34

base 2 mantissa:

2r1010.0  -> 10.0  2r0.01      -> 0.25    2r1010e5 -> 320.0

base 2 mantissa and base 2 exponent:

2r1010e2r0101 -> 320.0

Swift

let double = 1.0 as Double  // Double precisionlet float = 1.0 as Float // Single precisionlet scientific = 1.0E-12 // Swift does not feature type coercion for explicit type declarationlet sum = double + float // Error let div = 1.1 / 2 // Doublelet div1 = 1 / 2 // 0

Tcl

Floating point literals in Tcl always contain either “.” or “e” (of any case), if not both, or are references to either one of the IEEE infinities or NaN. Formally, they are values that (case-insensitively) match one these regular expressions:

Normal values
[-+]?[0-9]*\.[0-9]+(e[-+]?[0-9]+)?
[-+]?[0-9]+\.?e[-+]?[0-9]+
Infinite values
[-+]?inf(inity)?
NaN values
[-+]?nan($$[0-9a-f]+$$)?

Note also that NaN values usually result in checked exceptions; they are supported mainly so that they can be handled when parsing and generating external binary data. All other floating-point literals are fully legal values. (Also note that this excludes the definition of integer literals; for details see this TIP document, which explains the whole state machine.)

Ursa

Cygnus/X Ursa (the standard Ursa interpreter) is written in Java and supports Java style floating-point literals.

1.1.0 2432311.75673741.234E-101.234e-10758832d728832f1.0f758832D728832F1.0F

Vim Script

There are two ways to write floating point literals:

• [-+]?[0-9]+\.[0-9]+
• [-+]?[0-9]+\.[0-9]+[eE][-+]?[0-9]+

Examples: 12.34 +0.34 -1.0 12.34e5 0.99e-2 -1.0E+4

Note that there must always be at least one digit before and after the period (and for the exponent).

XPL0

0..11e3123.456E-300-123_456_789e+123

zkl

zkl requires something on both sides of the dot for a thing to be a float

1.0, 0.1, 3.1415, 1.e-100, 1.2e100, -1e10, -1e+10, 123.456E-300