This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,7 @@
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]]

View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1,7 @@
2
2.
.3
45e6
45e+6
78e-9
1.2E34

View file

@ -0,0 +1,8 @@
/^([0-9]+(\.[0-9]*)?|\.[0-9]+)([Ee][-+]?[0-9]+)?$/ {
print $0 " is a literal number."
next
}
{
print $0 " is not valid."
}

View file

@ -0,0 +1,3 @@
3.141_592_6
1.0E-12
0.13

View file

@ -0,0 +1,13 @@
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 -.

View file

@ -0,0 +1,11 @@
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

View file

@ -0,0 +1 @@
main = print [0.1,23.3,35e-1,56E+2,14.67e1]

View file

@ -0,0 +1,3 @@
procedure main()
every write( ![ 1., .1, 0.1, 2e10, 2E10, 3e-1, .4e2, 1.41e2, 8.e+3, 3.141e43 ])
end

View file

@ -0,0 +1,23 @@
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'

View file

@ -0,0 +1,4 @@
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

View file

@ -0,0 +1,13 @@
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

View file

@ -0,0 +1,2 @@
3.14159
314.159E-2

View file

@ -0,0 +1,48 @@
> 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)

View file

@ -0,0 +1,5 @@
> type( 2.3, 'hfloat' );
false
> type( HFloat( 2.3 ), 'hfloat' );
true

View file

@ -0,0 +1,15 @@
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.}

View file

@ -0,0 +1,20 @@
/* 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

View file

@ -0,0 +1,4 @@
.12
0.1234
1.2e3
7E-10

View file

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

View file

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

View file

@ -0,0 +1,6 @@
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

View file

@ -0,0 +1,6 @@
something = 127
something = '127' /*exactly the same as the above. */
something = 1.27e2
something = 1.27E2
something = 1.27E+2
something = ' + 0001.27e+00000000000000002 '

View file

@ -0,0 +1,3 @@
something = -.00478
say something
say format(something,,,,0)

View file

@ -0,0 +1,10 @@
.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

View file

@ -0,0 +1,5 @@
2.0
45e6
45e+6
78e-9
1.2E34

View file

@ -0,0 +1,3 @@
2r1010.0 -> 10.0
2r0.01 -> 0.25
2r1010e5 -> 320.0

View file

@ -0,0 +1 @@
2r1010e2r0101 -> 320.0