Literals/Floating point: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 615: Line 615:
A literal floating point number is written with a . and with or without an exponential notation :
A literal floating point number is written with a . and with or without an exponential notation :


<lang Oforth>
<lang Oforth>3.14
3.14
1.0e-12
1.0e-12
0.13
0.13
1000.0
1000.0
.22
.22</lang>
</lang>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==

Revision as of 19:15, 9 March 2015

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.

See also Literals/Integer.

Cf. Extreme floating point values

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>

Aime

<lang aime>3.14 5.0 8r # without the "r"(eal) suffix, "8" would be an integer .125</lang>

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

<lang awk>2 2. .3 45e6 45e+6 78e-9 1.2E34</lang>

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.

<lang awk>/^([0-9]+(\.[0-9]*)?|\.[0-9]+)([Ee][-+]?[0-9]+)?$/ { print $0 " is a literal number." next }

{ print $0 " is not valid." }</lang>

A leading plus or minus sign (as in +23 or -14) is not part of the literal; it is a unary operator. This is easy to check if you know that exponentiation has a higher precedence than unary minus; -14 ** 2 acts like -(14 ** 2), not like (-14) ** 2.

BBC BASIC

<lang bbcbasic> REM Floating-point literal syntax:

     REM  [-]{digit}[.]{digit}[E[-]{digit}]
     
     REM Examples:
     PRINT -123.456E-1
     PRINT 1000.0
     PRINT 1E-5
     
     REM Valid but non-standard examples:
     PRINT 67.
     PRINT 8.9E
     PRINT .33E-
     PRINT -.</lang>

Output:

  -12.3456
      1000
      1E-5
        67
       8.9
      0.33
         0

bc

A literal floating point number can be written as .NUMBER, NUMBER. or NUMBER.NUMBER where NUMBER consists of the hexadecimal digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. If digits in the number are greater than or equal to the current value of ibase (i.e. the input number radix) the behaviour is undefined.

Examples:

12.34   .34   99.   ABC.DEF

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]?)

Clojure

Clojure supports both standard and scientific notation.

user=> 1.
1.0
user=> 1.0
1.0
user=> 3.1415
3.1415
user=> 1.234E-10
1.234E-10
user=> 1e100
1.0E100
user=> (Float/valueOf "1.0f")
1.0

Clojure also supports returning ratios (fractions) if you divide integers. These are not subject to roundoff error. If you do specify a floating point in the division, it will return a floating point value.

user=> (/ 1 3)
1/3
user=> (/ 1.0 3)
0.3333333333333333

Common Lisp

The grammar for floating point literals in EBNF (ISO/IEC 14977):

float = [ sign ], { decimal-digit }, decimal-point, decimal-digit, { decimal-digit }, [exponent]  
      | [ sign ], decimal-digit, { decimal-digit }, [ decimal-point, { decimal-digit } ], exponent ;   
exponent = exponent-marker, [ sign ], decimal-digit, { decimal-digit } ;
sign = "+" | "-" ;
decimal-point = "." ;
decimal-digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
exponent-marker = "e" | "E" | "s" | "S" | "d" | "D" | "f" | "F" | "l" | "L" ;

Common Lisp implementations can provide up to 4 different float subtypes: short-float, single-float, double-float and long-float. The exponent marker specifies the type of the literal. "e"/"E" denotes the default floating point subtype (it is initially single-float but you can set it with the global variable *READ-DEFAULT-FLOAT-FORMAT* to any of the other subtypes). The standard only recommends a minimum precision and exponent size for each subtype and an implementation doesn't have to provide all of them:

Format       | Minimum Precision | Minimum Exponent Size  
--------------------------------------------------
Short (s/S)  |    13 bits        |      5 bits
Single (f/F) |    24 bits        |      8 bits
Double (d/D) |    50 bits        |      8 bits
Long (l/L)   |    50 bits        |      8 bits

Some examples:

> 1.0
1.0
> -.1
-0.1
> -1e-4
-1.0E-4
> 1d2
100.0d0
> .1f3
100.0
> .001l-300
1.0L-303

Note that 123. is not a floating point number but an integer:

> (floatp 123.)
NIL
> (integerp 123.)
T

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.

Eiffel

Floating point literals are of the form D.DeSD, where D represents a sequence of decimal digits, and S represents an optional sign. A leading "+" or "-" indicates a unary plus or minus feature and is not considered part of the literal.

Examples:<lang Eiffel> 1. 1.23 1e-5 .5 1.23E4 </lang>

Erlang

Floating point literal examples: 1.0 , -1.0 , 1.2e3 , 1.2e-3 and 1.2E3 , 1.2E-3 .

Euphoria

<lang euphoria> printf(1,"Exponential:\t%e, %e, %e, %e\n",{-10.1246,10.2356,16.123456789,64.12}) printf(1,"Floating Point\t%03.3f, %04.3f, %+3.3f, %3.3f\n",{-10.1246,10.2356,16.123456789,64.12}) printf(1,"Floating Point or Exponential: %g, %g, %g, %g\n",{10,16.123456789,64,123456789.123}) </lang>

Output:
Exponential:    -1.012460e+001, 1.023560e+001, 1.612346e+001, 6.412000e+001
Floating Point  -10.125, 10.236, +16.123, 64.120
Floating Point or Exponential:  10, 16.1235, 64, 1.23457e+008

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

GAP

<lang gap>-3.14 22.03e4 4.54e-5</lang>

gecho

<lang gecho> 0.0 -1 -1.2 -1.4324 3 4 / </lang>

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

Groovy

Solution: <lang groovy>println 1.00f // float (IEEE-32) println 1.00d // double (IEEE-64) println 1.00 // BigDecimal (scaled BigInteger) println 1.00g // BigDecimal println 1.00e0 // BigDecimal

assert 1.00f instanceof Float assert 1.00d instanceof Double assert 1.00 instanceof BigDecimal assert 1.00g instanceof BigDecimal assert 1.00e0 instanceof BigDecimal</lang>

Output:
1.0
1.0
1.00
1.00
1.00

Haskell

Haskell supports decimal representation of float literals, with or without an exponent. For more information, see the relevant portion of the Haskell 98 Report.

<lang haskell>main = print [0.1,23.3,35e-1,56E+2,14.67e1] </lang>

Output:

[0.1,23.3,3.5,5600.0,146.7]

Icon and Unicon

Real literals can be represented in two forms by (a) decimal literals, or (b) exponent literals. There is no sign as + and - are unary operators.

The program below shows a full range of valid real literals. <lang Icon>procedure main() every write( ![ 1., .1, 0.1, 2e10, 2E10, 3e-1, .4e2, 1.41e2, 8.e+3, 3.141e43 ]) end</lang>

The function write will cause the real values to be coerced as string constants. Icon/Unicon will format these as it sees fit resorting to exponent forms only where needed.

The IPL library routine printf provides a broader range of formatting choices.

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.

Lasso

<lang Lasso>0.0 0.1 -0.1 1.2e3 1.3e+3 1.2e-3</lang>

Lua

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

Maple

Maple distinguishes "software floats" (of arbitrary precision) and "hardware floats" (of machine precision). To get the latter, use the "HFloat" constructor. <lang Maple> > 123.456; # decimal notation

                               123.456

> 1.23456e2; # scientific notation

                               123.456

> Float( 23, -2 ); # float constructor notation, by mantissa and exponent

                                 0.23

> Float( .123456, 3 ); # again

                               123.456

> Float( 1.23456, 2 ); # again

                               123.456

> Float( 12.3456, 1 ); # again

                               123.456

> HFloat( 1.23456, 2 ); # hardware float constructor

                           123.456000000000

> HFloat( 123.456 ); # again

                           123.456000000000

> 2.3^30; # large floats are printed using scientific notation

                                         11
                          0.7109434879 10

> 2/3; # NOT a float!

                                 2/3

> evalf( 2/3 ); # but you can get one

                             0.6666666667

> 0.0; # zero

                                  0.

> -0.0; # negative zero

                                 -0.

> Float(infinity); # positive infinity

                           Float(infinity)

> Float(-infinity); # minus infinity

                           Float(-infinity)

> Float(undefined); # "NaN", not-a-number

                           Float(undefined)

</lang> Whether a given float is a software or hardware float can be determined by using "type". <lang Maple> > type( 2.3, 'hfloat' );

                                false

> type( HFloat( 2.3 ), 'hfloat' );

                                 true

</lang> (There is also a type "sfloat" for software floats, and the type "float", which covers both.)

Mathematica

<lang Mathematica>These numbers are given in the default output format. Large numbers are given in scientific notation. {6.7^-4,6.7^6,6.7^8} {0.00049625,90458.4,4.06068*10^6}

This gives all numbers in scientific notation. ScientificForm[%] {4.9625*10^(-4),9.04584*10^(4),4.06068*10^(6)}

This gives the numbers in engineering notation, with exponents arranged to be multiples of three. EngineeringForm[%] {496.25*10^(-6),90.4584*10^(3),4.06068*10^(6)}

In accounting form, negative numbers are given in parentheses, and scientific notation is never used. AccountingForm[{5.6,-6.7,10.^7}] {5.6,(6.7),10000000.}</lang>

Maxima

<lang maxima>/* Maxima has machine floating point (usually double precision IEEE 754), and arbitrary length "big floats" */

/* Here are ordinary floats */ 3.14159 2.718e0 1.2345d10 1.2345e10 1.2345f10

/* And big floats (always with a "b" for the exponent) */ 3.14159b0 2.718b0 1.2345b10

/* Before computing with big float, one must set precision to some value (default is 16 decimal digits) */ fpprec: 40$

bfloat(%pi); 3.141592653589793238462643383279502884197b0</lang>

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.

<lang NetRexx>/* NetRexx */ options replace format comments java crossref symbols nobinary

numeric digits 40 -- make lots of space for big numbers numeric 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.5 fv = -1.5; say '-1.5'.right(20) '==' normalize(fv).right(20) -- -1.5 fv = 15e-1; say '15e-1'.right(20) '==' normalize(fv).right(20) -- 1.5 fv = 3e-12; say '3e-12'.right(20) '==' normalize(fv).right(20) -- 3E-12 fv = 3e+12; say '3e+12'.right(20) '==' normalize(fv).right(20) -- 3000000000000 fv = 17.3E-12; say '17.3E-12'.right(20) '==' normalize(fv).right(20) -- 1.73E-11 fv = 17.3E+12; say '17.3E+12'.right(20) '==' normalize(fv).right(20) -- 17300000000000 fv = 17.3E+40; say '17.3E+40'.right(20) '==' normalize(fv).right(20) -- 1.73E+41 fv = 0.033e+9; say '0.033e+9'.right(20) '==' normalize(fv).right(20) -- 33000000 fv = 0.033e-9; say '0.033e-9'.right(20) '==' normalize(fv).right(20) -- 3.3E-11 say

say 'Sample using primitive type "float":' ff = float ff = float 15e-1; say '15e-1'.right(20) '==' normalize(ff).right(20) -- 1.5 ff = float 17.3E-12; say '17.3E-12'.right(20) '==' normalize(ff).right(20) -- 1.73E-11 ff = float 17.3E+12; say '17.3E+12'.right(20) '==' normalize(ff).right(20) -- 17300000000000 ff = float 0.033E+9; say '0.033E+9'.right(20) '==' normalize(ff).right(20) -- 33000000 ff = float 0.033E-9; say '0.033E-9'.right(20) '==' normalize(ff).right(20) -- 3.3E-11 say

say 'Sample using primitive type "double":' fd = double fd = 15e-1; say '15e-1'.right(20) '==' normalize(fd).right(20) -- 1.5 fd = 17.3E-12; say '17.3E-12'.right(20) '==' normalize(fd).right(20) -- 1.73E-11 fd = 17.3E+12; say '17.3E+12'.right(20) '==' normalize(fd).right(20) -- 17300000000000 fd = 17.3E+40; say '17.3E+40'.right(20) '==' normalize(fd).right(20) -- 1.73E+41 fd = 0.033E+9; say '0.033E+9'.right(20) '==' normalize(fd).right(20) -- 33000000 fd = 0.033E-9; say '0.033E-9'.right(20) '==' normalize(fd).right(20) -- 3.3E-11 say

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

</lang> 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

<lang nim>var x: float x = 2.3 x = 2.0 x = 0.3 x = 123_456_789.000_000_1 x = 2e10 x = 2.5e10 x = 2.523_123E10 x = 5.2e-10

var y = 2'f32 # Automatically a float32 var z = 2'f64 # Automatically a float64 </lang>

Objeck

<lang objeck> 3 + .14159 3.14159 314.159E-2 </lang>

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:

<lang ocaml>0.5 1.0 1. (* it is not possible to write only "1" because OCaml is strongly typed,

        and this would be interpreted as an integer *)

1e-10 3.14159_26535_89793</lang>

Oforth

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

<lang Oforth>3.14 1.0e-12 0.13 1000.0 .22</lang>

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 161,614,249 decimal digits
64-bit 694,127,911,065,419,642 decimal digits

where 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

<lang perl># Standard notations: .5; 0.5; 1.23345e10; 1.23445e-10;

  1. The numbers can be grouped:

100_000_000; # equals to 100000000 </lang>

Perl 6

Floating point numbers (the Num type) are written various forms of scientific notation: <lang perl6>6.02e23 # standard E notation

10<6.02 * 10 ** 23> # radix notation
5<11.002 * 10 ** 23> # exponent is still decimal
5<11.002*:5<20>**:5<43>> # all in base 5</lang>

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 (not yet implemented).

PHP

More information about floating point numbers in PHP. <lang PHP>.12 0.1234 1.2e3 7E-10 </lang> 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

<lang PL/I> 1.2345e-4 decimal floating-point 7e5 decimal floating-point 1.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**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>

Racket

<lang racket>

  1. lang racket

.2 2. 2.+0i  ; zero imaginary part 2e0

  1. x10.8 ; hex float
  2. o1e2  ; oct float

2.0f0  ; single float 1.0t0  ; extended 80-bit float (when available on platform) </lang>

Output:

0.2
2.0
2.0
2.0
16.5
64.0
2.0f0
1.0t0

REXX

All values in REXX are strings, so a value could hold such things as: <lang rexx>something = 127 something = '127' /*exactly the same as the above. */ something = 1.27e2 something = 1.27E2 something = 1.27E+2 something = ' + 0001.27e+00000000000000002 '</lang> To forcibly express a value in exponential notation, REXX has a built-in function (FORMAT) to do 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.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

Scala

Library: 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.0 1.0 //Double, a 64-bit IEEE-754 floating point number (equivalent to Java's double primitive type) 2432311.7567374 //Double 1.234E-10 //Double 1.234e-10 //Double 758832d //Double 728832f //32-bit IEEE-754 floating point number (equivalent to Java's float primitive type) 1.0f //Float 758832D //Double 728832F //Float 1.0F //Float 1 / 2. //Double 1 / 2 //Int equal to 0

// Constants Float.MinPositiveValue Float.NaN Float.PositiveInfinity Float.NegativeInfinity

Double.MinPositiveValue Double.NaN Double.PositiveInfinity Double.NegativeInfinity </lang> Values that are outside the bounds of a type will give compiler-time errors when trying to force them to that type.

Scheme

<lang scheme> .2  ; 0.2 2.  ; 2.0 2e3  ; 2000 2.+3.i  ; complex floating-point number

in Scheme, floating-point numbers are inexact numbers

(inexact? 2.)

#t

(inexact? 2)

#f</lang>

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: <lang seed7> 3.14159265358979 1.0E-12 0.1234 </lang> The functions str and the operators digits and parse create and accept float literals with sign.

Original source: [1]

Sidef

<lang ruby>say 1.234; say .1234; say 1234e-5; say 12.34e5;</lang>

Output:
1.234
0.1234
0.01234
1234000

Smalltalk

<lang smalltalk>2.0 45e6 45e+6 78e-9 1.2E34</lang>

base 2 mantissa: <lang smalltalk>2r1010.0 -> 10.0 2r0.01 -> 0.25 2r1010e5 -> 320.0</lang>

base 2 mantissa and base 2 exponent: <lang smalltalk>2r1010e2r0101 -> 320.0</lang>

Swift

<lang Swift>let double = 1.0 as Double // Double precision let float = 1.0 as Float // Single precision let scientific = 1.0E-12

// Swift does not feature type coercion for explicit type declaration let sum = double + float // Error

let div = 1.1 / 2 // Double let div1 = 1 / 2 // 0</lang>

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

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

<lang XPL0>0. .1 1e3 123.456E-300 -123_456_789e+123</lang>

zkl

zkl requires something on both sides of the dot for a thing to be a float <lang zkl>1.0, 0.1, 3.1415, 1.e-100, 1.2e100, -1e10, -1e+10, 123.456E-300</lang>