Literals/Floating point

From Rosetta Code
Task
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.

Ada

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: <lang Ada> 3.141_592_6 1.0E-12 0.13 </lang>

C

Floating-point numbers can be given in decimal or hexadecimal. Decimal floating-point numbers must have at least one of a decimal point and an exponent part, which is marked by an E:

((\d*\.\d+|\d+\.)([eE][+-]?[0-9]+)?[flFL]?)|([0-9]+[eE][+-]?[0-9]+[flFL]?)

Hexadecimal is similar, but allowing A-F as well as 0-9. They have a binary exponent part marked with a P instead of a decimal exponent:

(0[xX]([0-9a-fA-F]*\.[0-9a-fA-F]+|[0-9a-fA-F]+\.)([pP][+-]?\d+[flFL]?)|(0[xX][0-9a-fA-F]+[pP][+-]?\d+[flFL]?)

D

D built-in floating point types include float (32-bit), double (64-bit) and real (machine hardware maximum precision floating point type, 80-bit on x86 machine) and respective complex number types. Here's information for Floating Literals.

Forth

Unlike most other languages, floating point literals in Forth are distinguished by their exponent ('E' or 'e') rather than their decimal point ('.' or ','). Numeric literals with just a decimal point are regarded as two-cell double precision integers. From the ANS Forth standards document:

Convertible string := <significand><exponent>

<significand> := [<sign>]<digits>[.<digits0>]
<exponent>    := E[<sign>]<digits0>
<sign>        := { + | - }
<digits>      := <digit><digits0>
<digits0>     := <digit>*
<digit>       := { 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 }

These are examples of valid representations of floating-point numbers in program source:

	1E   1.E   1.E0   +1.23E-1   -1.23E+1

Go

See relevant section of language reference. Basically they are base 10, need either a decimal point or an exponent, and specify no precision or representation. The exponent can be signed, but the mantissa is not. A leading minus sign would be an operator and not part of the floating point literal. Examples,

0.
1e3
1e-300
6.02E+23

J

This paragraph highlights current implementation specific details of internal types: J has a syntax for specifying numbers, but numeric constants are stored in their most compact implementation; for example, 2.1 is a floating point number, while 2.0 is an integer and 1.0 is a boolean. If the exact type of a value is important, an expression may be used; for example, 1.1-0.1 produces a floating point result.

J's numeric constant mini-language allows the specification of numbers which are not floating point, but as indicated above, numeric type in J is a semantic triviality and not a syntactic feature. (And this pervades the language. For example, 1+1 is 2, despite the result having a different type from both of the arguments. Or, for example, if maxint is the largest value represented using an integer type, maxint+1 will produce a floating point result instead of an error or a wraparound.)

Here is an informal bnf for J's numeric constant language. Note, however, that the implementation may disallow some unusual cases -- cases which are not treated as exceptional here (for example, the language specification allows 1.2e3.4 but the current implementation does not support fractional powers of 10 in numeric constants):

<lang bnf>numeric-constant ::= number-constant | number-constant whitespace numeric-constant whitespace ::= whitespacecharacter | whitespacecharacter whitespace whitespacecharacter ::= ' ' | TAB TAB is ascii 9 number-constant ::= arbitrary-constant | arbitrary-constant base-token base-constant base-token ::= 'b' | 'b-' base-constant ::= base-digits | base-digits '.' base-digits base-digits ::= base-digit | base-digit base-digits base-digit ::= digit | alpha1 | alpha2 alpha1 ::= 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m' alpha2 ::= 'n'|'o'|'p'|'q'|'r'|'s'|'t'|'u'|'v'|'w'|'x'|'y'|'z' arbitrary-constant ::= complex-constant | pi-constant | euler-constant | extended-constant pi-constant ::= complex-constant 'p' complex-constant euler-constant ::= complex-constant 'x' complex-constant extended-constant ::= signed-digits 'x' | signed-digits 'r' signed-digits complex-constant ::= exponential-constant | exponential-constant complex-token exponential-constant complex-token ::= 'ad' | 'ar' | 'j' exponential-constant ::= signed-constant | signed-constant 'e' signed-constant signed-constant ::= decimal-constant | '_' decimal-constant decimal-constant ::= digits | digits '.' digits signed-digits ::= digits | '_' digits digits ::= digit | digit digits digit ::= '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'</lang>

e indicates exponential or scientific notation (number on left multiplied by 10 raised to power indicated by number on right)

ad, ar and j are used to describe complex numbers (angle in degrees, in radians, and rectangular form)

p and infix x are analogous to e except the base is pi or the base of natural logarithms

r and x are also used for arbitrary precision numbers, r indication a ration and a trailing x indicating an extended precision integer.

b is used for arbitrary bases, and letters a-z indicate digit values 10 through 35 when they follow a b

Floating point examples:

<lang j> 0 1 _2 3.4 3e4 3p4 3x4 0 1 _2 3.4 30000 292.227 163.794

  16bcafe.babe _16b_cafe.babe _10b11

51966.7 46818.7 _9</lang>

Note that all the values in an array are the same type, thus the 0, 1 and 2 in the above example are floating point because they do not appear by themselves. Note also that by default J displays no more than six significant digits of floating point values.

Java

<lang java>1. //double equal to 1.0 1.0 //double 2432311.7567374 //double 1.234E-10 //double 1.234e-10 //double 758832d //double 728832f //float 1.0f //float 758832D //double 728832F //float 1.0F //float 1 / 2. //double 1 / 2 //int equal to 0 </lang> Values that are outside the bounds of a type will give compiler errors when trying to force them to that type.

Lua

<lang lua>3.14159 314.159E-2</lang>

PARI/GP

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

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

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

<lang PL/I> 1.2345e-4 decimal floating-point 7e5 decimal floating-point 1.234_567_89 decimal floating-point.

111.0101e7b binary floating-point equals 111.0101 * 2**7

                                    or 7.3125 * 2**7

1e5b binary floating-point equals 1 * 2**5 </lang>

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.

<lang ebnf>FLOAT

   :   '.' DIGITS (Exponent)?
   |   DIGITS '.' Exponent
   |   DIGITS ('.' (DIGITS (Exponent)?)? | Exponent)
   ;

DIGITS : ( '0' .. '9' )+ ;

Exponent

   :    ('e' | 'E') ( '+' | '-' )? DIGITS
   ;</lang>

Examples <lang python> 2.3 # 2.2999999999999998 .3 # 0.29999999999999999 .3e4 # 3000.0 .3e+34 # 2.9999999999999998e+33 .3e-34 # 2.9999999999999999e-35 2.e34 # 1.9999999999999999e+34 </lang>

REXX

All values in REXX are strings, so a value could hold such things as: <lang rexx>something = 127 something = 1.27e2 something = 1.27E2 something = 1.27E+2</lang> To forceably express a value in exponentional notation, REXX has a built-in function (FORMAT) to to this.

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

 0

by the FORMAT subroutine. <lang rexx>something = -.00478 say something say format(something,,,,0)</lang> Output:

-0.0047800
-4.78E-3

This particular invocation of FORMAT (above) with the fourth parameter equal to 0 (zero) forces exponential notation.

There are other options for the FORMAT built-in subroutine to force any number of digits before and/or after the decimal point.

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.)