Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Literals-Floating-point/00-META.yaml
Normal file
3
Task/Literals-Floating-point/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Literals/Floating_point
|
||||
note: Basic language learning
|
||||
14
Task/Literals-Floating-point/00-TASK.txt
Normal file
14
Task/Literals-Floating-point/00-TASK.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
Programming languages have different ways of expressing floating-point literals.
|
||||
|
||||
|
||||
;Task:
|
||||
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.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Literals/Integer]]
|
||||
* [[Extreme floating point values]]
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// 64-bit floating point literals:
|
||||
2.3
|
||||
0.3e+34
|
||||
|
||||
// single precision (32-bit) floating point literals:
|
||||
2.3s
|
||||
0.3e+34s
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
XS4 DC E'1.23456E-4' short floating-point
|
||||
|
||||
XDPI DC D'3.141592653589793' long floating-point
|
||||
XD1 DC D'0' long floating-point
|
||||
XD2 DC D'1' long floating-point
|
||||
XD3 DC D'-1' long floating-point
|
||||
XD4 DC D'1.2345E-4' long floating-point
|
||||
|
||||
XQPI DC L'3.14159265358979323846264338327950' extended
|
||||
|
||||
* short floating-point - 32 bits - 4 bytes : 6 decimal digits
|
||||
* long floating-point - 64 bits - 8 bytes : 16 decimal digits
|
||||
* extended floating-point - 128 bits - 16 bytes : 33 decimal digits
|
||||
|
||||
* absolute approximate range: 5e-79 to 7e75
|
||||
|
|
@ -0,0 +1 @@
|
|||
byte $DB,$0F,$49,$40 ;3.141592654
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Pi:
|
||||
DC.L $40490FDB
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
# 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;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
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.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
2
|
||||
2.
|
||||
.3
|
||||
45e6
|
||||
45e+6
|
||||
78e-9
|
||||
1.2E34
|
||||
|
|
@ -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."
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
3.141_592_6
|
||||
1.0E-12
|
||||
0.13
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
3.14
|
||||
5.0
|
||||
8r # without the "r"(eal) suffix, "8" would be an integer
|
||||
.125
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
pi: 3.14
|
||||
print [pi "->" type pi]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
123→float{L₁}
|
||||
float{L₁}→I
|
||||
|
|
@ -0,0 +1 @@
|
|||
12.25→A
|
||||
|
|
@ -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 -.
|
||||
30
Task/Literals-Floating-point/C++/literals-floating-point.cpp
Normal file
30
Task/Literals-Floating-point/C++/literals-floating-point.cpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
// a numeric literal with decimal point is a double
|
||||
auto double1 = 2.5;
|
||||
|
||||
// an 'f' of 'F' suffix means the literal is a flaot
|
||||
auto float1 = 2.5f;
|
||||
|
||||
// an 'l' or 'L' suffix means a long double
|
||||
auto longdouble1 = 2.5l;
|
||||
|
||||
// a number after an 'e' or 'E' is the base 10 exponent
|
||||
auto double2 = 2.5e-3;
|
||||
auto float2 = 2.5e3f;
|
||||
|
||||
// a '0x' prefix means the literal is hexadecimal. the 'p' is base 2 the exponent
|
||||
auto double3 = 0x1p4;
|
||||
auto float3 = 0xbeefp-8f;
|
||||
|
||||
std::cout << "\ndouble1: " << double1;
|
||||
std::cout << "\nfloat1: " << float1;
|
||||
std::cout << "\nlongdouble1: " << longdouble1;
|
||||
std::cout << "\ndouble2: " << double2;
|
||||
std::cout << "\nfloat2: " << float2;
|
||||
std::cout << "\ndouble3: " << double3;
|
||||
std::cout << "\nfloat3: " << float3;
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
double d = 1;
|
||||
d = 1d;
|
||||
d = 1D;
|
||||
d = 1.2; //double is the default if there's no suffix
|
||||
d = 1.2d; //The suffix is redundant here
|
||||
d = .2;
|
||||
d = 12e-12;
|
||||
d = 12E-12;
|
||||
d = 1_234e-1_2; //digit separators are allowed since C# 7
|
||||
float f = 1;
|
||||
f = 1f;
|
||||
f = 1F;
|
||||
f = 1.2f;
|
||||
f = .2f;
|
||||
f = 12e-12f;
|
||||
f = 12E-12f;
|
||||
f = 1_234e-1_2f;
|
||||
decimal m = 1;
|
||||
m = 1m;
|
||||
m = 1m;
|
||||
m = 1.2m;
|
||||
m = .2m;
|
||||
m = 12e-12m;
|
||||
m = 12E-12m;
|
||||
m = 1_234e-1_2m;
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
procedure FloatLiterals(Memo: TMemo);
|
||||
{Delphi has multiple floating number formats}
|
||||
var R48: Real48; {48-bit real number}
|
||||
var SI: Single; {32-bit real number}
|
||||
var D: Double; {64-bit real number}
|
||||
var E: Extended; {80-bit real number}
|
||||
var Cmp: Comp; {}
|
||||
var Cur: Currency; {Fixed point number for currency}
|
||||
begin
|
||||
{Various formats that can be used on input or
|
||||
as constants assigned to various reals.
|
||||
Pascal automaically converts integer to
|
||||
reals as long as the real has enough precision
|
||||
to hold the value. Consequently, all of the
|
||||
following constants can be assigned to a real.}
|
||||
D:=1234;
|
||||
D:=1.234;
|
||||
D:=1234E-4;
|
||||
D:=$7F;
|
||||
{Reals can also be output in various formats.}
|
||||
D:=123456789.1234;
|
||||
Memo.Lines.Add(FloatToStrF(D,ffGeneral,18,4));
|
||||
Memo.Lines.Add(FloatToStrF(D,ffExponent,18,4));
|
||||
Memo.Lines.Add(FloatToStrF(D,ffFixed,18,4));
|
||||
Memo.Lines.Add(FloatToStrF(D,ffNumber,18,4));
|
||||
Memo.Lines.Add(FloatToStrF(D,ffCurrency,18,4));
|
||||
Memo.Lines.Add(Format('%10.4f',[D]));
|
||||
end;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
var x = 42.02
|
||||
var y = 0.174e-17
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
decimal = 57.1
|
||||
decimalWithE = 5710E-2
|
||||
hexadecimal = 0x39.1999999999
|
||||
print decimal
|
||||
print decimalWithE
|
||||
print hexadecimal
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
1.
|
||||
1.23
|
||||
1e-5
|
||||
.5
|
||||
1.23E4
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
real r := 1;
|
||||
r := 23.2r;
|
||||
r := 1.2e+11r;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
iex(180)> 0.123
|
||||
0.123
|
||||
iex(181)> -123.4
|
||||
-123.4
|
||||
iex(182)> 1.23e4
|
||||
1.23e4
|
||||
iex(183)> 1.2e-3
|
||||
0.0012
|
||||
iex(184)> 1.23E4
|
||||
1.23e4
|
||||
iex(185)> 10_000.0
|
||||
1.0e4
|
||||
iex(186)> .5
|
||||
** (SyntaxError) iex:186: syntax error before: '.'
|
||||
|
||||
iex(186)> 2. + 3
|
||||
** (CompileError) iex:186: invalid call 2.+(3)
|
||||
|
||||
iex(187)> 1e4
|
||||
** (SyntaxError) iex:187: syntax error before: e4
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
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})
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
3.14 ! basic float
|
||||
+3.14 ! Optional signs
|
||||
-3.14
|
||||
10e5 ! exponents signified by e or E
|
||||
10E+5 ! with optional signs
|
||||
+10e-5
|
||||
1. ! equivalent to 1.0
|
||||
.5 ! equivalent to 0.5
|
||||
1/2. ! floating point approximation of a ratio (0.5)
|
||||
1/3. ! 0.3333333333333333
|
||||
1/0. ! positive infinity
|
||||
-1/0. ! negative infinity
|
||||
0/0. ! not-a-number
|
||||
! hexadecimal, octal, and binary float literals are supported.
|
||||
! they require a base 2 exponent expressed as a decimal
|
||||
! preceded by p or P.
|
||||
0x1.0p3 ! 8.0
|
||||
-0x1.0P-3 ! -0.125
|
||||
0b1.010001p3 ! 10.125
|
||||
0o1.21p3 ! 10.125
|
||||
! comma separators are allowed
|
||||
1,234.123,456 ! 1234.123456
|
||||
|
||||
|
||||
! normalized hex form ±0x1.MMMMMMMMMMMMMp±EEEE allows any floating-point
|
||||
! number to be specified precisely according to IEEE 754 representation
|
||||
+0x1.1234567891234p+0002 ! 4.28444444440952
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
;;Numeric literals with a decimal component are treated as floating point.
|
||||
3.14159 ;3.14159
|
||||
|
||||
;;An exponent can be specified via "e" or "E" and is always floating point.
|
||||
2.3456e7 ;23456000.0
|
||||
|
||||
;;Hexadecimal literals are supported, including exponents via "p" or "P".
|
||||
0x1234.abcd ;4660.6710968018
|
||||
0x1234.56p3 ;37282.6875
|
||||
|
||||
;;Underscores can optionally be used to split numbers into readable chunks.
|
||||
123_456.789 ;123456.789
|
||||
0x1234_5678.9a ;305419896.60156
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
' FB 1.05.0 Win64 (default dialect)
|
||||
|
||||
Dim a As Double = 123.456
|
||||
Dim b As Double = -123.0
|
||||
Dim c As Double = -123.0d
|
||||
Dim d As Double = -123e
|
||||
Dim e As Double = 743.1e+13
|
||||
Dim f As Double = 743.1D-13
|
||||
Dim g As Double = 743.1E13
|
||||
Dim h As Single = 743D! Rem ! overrides D
|
||||
Dim i As Single = 3.1!
|
||||
Dim j As Single = -123.456e-7f
|
||||
Dim k As Double = 0#
|
||||
Dim l As Double = 3.141592653589e3#
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
local fn DoIt
|
||||
print "Single:"
|
||||
single s = 1.0 : print s
|
||||
s = 123.456 : print s
|
||||
s = -123.0 : print s
|
||||
s = 43.1e+5 : print s
|
||||
s = 43.1E5 : print s
|
||||
s = 3.1 : print s
|
||||
s = -123.456e-2 : print s
|
||||
s = 3.141592653589e3 : print s
|
||||
print
|
||||
|
||||
print "Double"
|
||||
double d = 1.0 : print d
|
||||
d = 123.456 : print d
|
||||
d = -123.0 : print d
|
||||
d = 43.1e+13 : print d
|
||||
d = 43.1E13 : print d
|
||||
d = 3.1 : print d
|
||||
d = -123.456e-7 : print d
|
||||
d = 3.141592653589e3 : print d
|
||||
print
|
||||
|
||||
print "Float:"
|
||||
float f = 1.0 : print f
|
||||
f = 123.456 : print f
|
||||
f = -123.0 : print f
|
||||
f = 43.1e+5 : print f
|
||||
f = 43.1E5 : print f
|
||||
f = 3.1 : print f
|
||||
f = -123.456e-2 : print f
|
||||
f = 3.141592653589e3 : print f
|
||||
print
|
||||
|
||||
print "CFNumberRef:"
|
||||
CFNumberRef c = @1.0 : print c
|
||||
c = @123.456 : print c
|
||||
c = @-123.0 : print c
|
||||
c = @43.1e+13 : print c
|
||||
c = @743.1E13 : print c
|
||||
c = @3.1 : print c
|
||||
c = @-123.456e-7 : print c
|
||||
c = @3.141592653589e37 : print c
|
||||
end fn
|
||||
|
||||
fn DoIt
|
||||
|
||||
HandleEvents
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
-3.14
|
||||
22.03e4
|
||||
4.54e-5
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
0.0
|
||||
-1
|
||||
-1.2
|
||||
-1.4324
|
||||
3 4 /
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1 @@
|
|||
main = print [0.1,23.3,35e-1,56E+2,14.67e1]
|
||||
|
|
@ -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
|
||||
23
Task/Literals-Floating-point/J/literals-floating-point-1.j
Normal file
23
Task/Literals-Floating-point/J/literals-floating-point-1.j
Normal 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'
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
1.0 => 1
|
||||
1.2 => 1.2
|
||||
1e10 => 10000000000
|
||||
1e100 => 1e+100
|
||||
1e1234 => 1.7976931348623157e+308
|
||||
.1 => 0.1
|
||||
.1e1 => 1
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
0.1
|
||||
.1
|
||||
1.
|
||||
1e-1 # scientific notation
|
||||
1e+10
|
||||
1e-10
|
||||
0x01p-1 # hex float
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
val d = 1.0 // Double
|
||||
val d2 = 1.234e-10 // Double
|
||||
val f = 728832f // Float
|
||||
val f2 = 7.28832e5F // Float
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
0.0
|
||||
0.1
|
||||
-0.1
|
||||
1.2e3
|
||||
1.3e+3
|
||||
1.2e-3
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
put 0.23
|
||||
-- 0.2300
|
||||
|
||||
-- activate higher printing precision
|
||||
the floatPrecision = 8
|
||||
|
||||
put -.23
|
||||
-- -0.23000000
|
||||
|
||||
put 9.00719925474099e15
|
||||
-- 9.00719925474099e15
|
||||
|
||||
-- result is NOT a float
|
||||
put 2/3
|
||||
-- 0
|
||||
|
||||
-- casting integer to float
|
||||
put float(2)/3
|
||||
-- 0.66666667
|
||||
|
||||
-- casting string to float
|
||||
put float("0.23")
|
||||
-- 0.23000000
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
3.14159
|
||||
314.159E-2
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Def ExpType$(x)=Type$(x)
|
||||
Print ExpType$(-12)="Double", -12
|
||||
Print ExpType$(12.)="Double", 12.
|
||||
Print ExpType$(12.e-5)="Double", 12.e-5
|
||||
Print ExpType$(.1)="Double", .1
|
||||
Print ExpType$(-12~)="Single", -12~
|
||||
Print ExpType$(12.~)="Single", 12.~
|
||||
Print ExpType$(12.e-5~)="Single", 12.e-5~
|
||||
Print ExpType$(.1~)="Single", .1~
|
||||
|
|
@ -0,0 +1 @@
|
|||
li.s f0,2.5 ;loads the single-precision float 2.5 (0x40200000) into register f0
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
la $t0,pi
|
||||
lwc1 $f0,0($t0) ;load pi into $f0
|
||||
|
||||
li $v0,10 ;exit command
|
||||
syscall ;return to linux
|
||||
|
||||
pi:
|
||||
.word 0x40490FDB ;IEEE-754 representation of 3.1415927
|
||||
|
|
@ -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)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
> type( 2.3, 'hfloat' );
|
||||
false
|
||||
|
||||
> type( HFloat( 2.3 ), 'hfloat' );
|
||||
true
|
||||
|
|
@ -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.}
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
/* 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
|
||||
12
Task/Literals-Floating-point/Nim/literals-floating-point.nim
Normal file
12
Task/Literals-Floating-point/Nim/literals-floating-point.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
3 + .14159
|
||||
3.14159
|
||||
314.159E-2
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
3.14
|
||||
1.0e-12
|
||||
0.13
|
||||
1000.0
|
||||
.22
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
.12
|
||||
0.1234
|
||||
1.2e3
|
||||
7E-10
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
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
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
-->
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">1e+12</span> <span style="color: #000080;font-style:italic;">-- (same as 1e12)</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">1e-12</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">5</span> <span style="color: #000080;font-style:italic;">-- (same as 5.0)
|
||||
--?1. -- (illegal, use 1 or 1.0)</span>
|
||||
<span style="color: #0000FF;">?.</span><span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (same as 0.1)</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span> <span style="color: #000080;font-style:italic;">-- 0.333333</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%g %G\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e-30</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
FLOAT
|
||||
: '.' DIGITS (Exponent)?
|
||||
| DIGITS '.' Exponent
|
||||
| DIGITS ('.' (DIGITS (Exponent)?)? | Exponent)
|
||||
;
|
||||
|
||||
DIGITS : ( '0' .. '9' )+ ;
|
||||
|
||||
Exponent
|
||||
: ('e' | 'E') ( '+' | '-' )? DIGITS
|
||||
;
|
||||
|
|
@ -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
|
||||
|
|
@ -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 '
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
something = -.00478
|
||||
say something
|
||||
say format(something,,,,0)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
#lang racket
|
||||
.2
|
||||
2.
|
||||
2.+0i ; zero imaginary part
|
||||
2e0
|
||||
#x10.8 ; hex float
|
||||
#o1e2 ; oct float
|
||||
2.0f0 ; single float
|
||||
1.0t0 ; extended 80-bit float (when available on platform)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
2e2 # same as 200e0, 2e2, 200.0e0 and 2.0e2
|
||||
6.02e23
|
||||
-2e48
|
||||
1e-9
|
||||
1e0
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
2.3 // Normal floating point literal
|
||||
3. // 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 value
|
||||
1_000.2_f32 // Underscores may appear anywhere in the number for clarity.
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
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
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
3.14159265358979
|
||||
1.0E-12
|
||||
0.1234
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
say 1.234;
|
||||
say .1234;
|
||||
say 1234e-5;
|
||||
say 12.34e5;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
2.0
|
||||
45e6
|
||||
45e+6
|
||||
78e-9
|
||||
1.2E34
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
2r1010.0 -> 10.0
|
||||
2r0.01 -> 0.25
|
||||
2r1010e5 -> 320.0. "hint: = 10*(2ˆ5)"
|
||||
|
|
@ -0,0 +1 @@
|
|||
2r1010e2r0101 -> 320.0 "hint: = 10*(2ˆ5)"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
3.1i
|
||||
2.0+4.5i
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
.3
|
||||
1.5
|
||||
-1.5e10
|
||||
3.15e-100
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
1.
|
||||
1.0
|
||||
2432311.7567374
|
||||
1.234E-10
|
||||
1.234e-10
|
||||
758832d
|
||||
728832f
|
||||
1.0f
|
||||
758832D
|
||||
728832F
|
||||
1.0F
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
// Floating-point Literals:
|
||||
//
|
||||
// If present,the exponent must be of the form:
|
||||
//
|
||||
// eNNN...N
|
||||
// ENNN...N
|
||||
// e-NNN...N
|
||||
// E-NNN...N
|
||||
// e+NNN...N
|
||||
// E+NNN...N
|
||||
//
|
||||
// If present, length suffix must be:
|
||||
//
|
||||
// f F (FLOAT64_T)
|
||||
// f32 F32 (FLOAT32_T)
|
||||
// f64 F64 (FLOAT64_T)
|
||||
// fd Fd fD FD (FLOATD_T) -- boost::multiprecision::
|
||||
// cpp_dec_float<100, int64_t>
|
||||
//
|
||||
// The presence of "." "E" "e" "F" or "f" indicates a floating point literal.
|
||||
//
|
||||
// A literal can start with "-" "." or a decimal digit, but not "+" or "_".
|
||||
// There must be at least one digit, so forms like ".F" ".e+11_f32" or just "."
|
||||
// are not recognized as floating point literals.
|
||||
//
|
||||
// Floating-point literal examples:
|
||||
|
||||
@SAY 0. .0 0.0 1. .1 123.123 ;// FLOAT64_T
|
||||
@SAY -0. -.0 -0.0 -1. -.1 -123.123 ;// FLOAT64_T
|
||||
|
||||
@SAY -0.E1 .0e0 0.0E6 -1.e6 -.1E8 12.12e44 -0E0 1e20 ;// FLOAT64_T
|
||||
@SAY -0.e+1 .0E+0 0.0e+6 -1.E+6 -.1e+8 12.12E+44 -0e+0 1E+20 ;// FLOAT64_T
|
||||
@SAY -0.E-1 .0e-0 0.0E-6 -1.e-6 -.1E-8 12.12e-44 -0E-0 1e-20 ;// FLOAT64_T
|
||||
@SAY -0E9999999999999999 .0e+9999999999 0.E-999999999999999999 ;// FLOAT64_T
|
||||
@SAY -8e0000000000000299 .6E+0000000299 5.e-000000000000000299 ;// FLOAT64_T
|
||||
|
||||
@SAY 0f -0f 0F -0f .0F -1234f 12.F 12.34f ;// FLOAT64_T
|
||||
@SAY 0F32 -0f32 0f32 -0f32 .0f32 -1234F32 12.f32 12.34F32 ;// FLOAT32_T
|
||||
@SAY 0f64 -0f64 0F64 -0f64 .0F64 -1234f64 12.F64 12.34f64 ;// FLOAT64_T
|
||||
@SAY 0fD -0fd 0FD -0fd .0Fd -1234fD 12.FD 12.34fD ;// FLOATD_T
|
||||
|
||||
@SAY -0.E1f .0e0F 0.0E6f64 -1.e6F64 -.1E8f 12.12e44f64 -0E0F 1e20f64 ;// FLOAT64_T
|
||||
@SAY -0.e+1f32 .0E+0F32 0.0e+6f32 -1.E+6F32 -.1e+8f32 12.12E+34F32 -0e+0f32 1E+20F32;// FLOAT32_T
|
||||
@SAY -0.E-1fd .0e-0fD 0.0E-6Fd -1.e-6FD -.1E-8fd 12.12e-44fD -0E-0Fd 1e-20FD ;// FLOATD_T
|
||||
@SAY -0E9999999999999999f32 .0e+9999999999F32 0.E-999999999999999999f32 ;// FLOAT32_T
|
||||
@SAY -8e0000000000000299f .6E+0000000299f64 5.e-00000000000000000000299F64 ;// FLOAT64_T
|
||||
@SAY -8e9999999999999999fD .6E+99999999999fD 5.e-12345678987654321FD ;// FLOATD_T
|
||||
|
||||
// note: _ (underscores) can appear in the main numeric part of the literal
|
||||
// after the first digit and before any length suffix:
|
||||
|
||||
@SAY -10_000__f 1__0._55__ -1__.__ .0___44 1_._2__E-23F32 debug:;
|
||||
|
||||
// Underscores can also appear in the exponent, after the first digit:
|
||||
|
||||
@SAY -1_E-0__2_f32 1.e+0___5_5____ -1.0_E123_456_789_987_654_321__fD debug:;
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
Sub Main()
|
||||
Dim d As Double ' 8 Bytes, type specifier = #
|
||||
Dim s As Single ' 4 Bytes, type specifier = !
|
||||
d = -12.3456
|
||||
d = 1000#
|
||||
d = 0.00001
|
||||
d = 67#
|
||||
d = 8.9
|
||||
d = 0.33
|
||||
d = 0#
|
||||
d = 2# * 10 ^ 3
|
||||
d = 2E+50
|
||||
d = 2E-50
|
||||
s = -12.3456!
|
||||
s = 1000!
|
||||
s = 0.00001!
|
||||
s = 67!
|
||||
s = 8.9!
|
||||
s = 0.33!
|
||||
s = 0!
|
||||
s = 2! * 10 ^ 3
|
||||
End Sub
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
Option Explicit
|
||||
Public Declare Function RtlCompareMemory Lib "ntdll.dll" _
|
||||
(ByRef Source1 As Any, ByRef Source2 As Any, ByVal Length As Long) As Long
|
||||
|
||||
Public Function IsNAN(ByRef d As Double) As Boolean
|
||||
Dim d1 As Double
|
||||
d1 = NaN()
|
||||
IsNAN = (RtlCompareMemory(d, d1, 8) = 8)
|
||||
End Function
|
||||
|
||||
Public Function NaN() As Double
|
||||
On Error Resume Next ' ignore the error
|
||||
NaN = 0 / 0
|
||||
End Function
|
||||
|
||||
Sub Main()
|
||||
Dim d1 As Double
|
||||
Dim d2 As Double
|
||||
d1 = NaN()
|
||||
d2 = d1
|
||||
Debug.Assert IsNAN(d2)
|
||||
Debug.Print CStr(d2)
|
||||
End Sub
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
var f = 123.45
|
||||
var g = 0.12345 // .12345 not allowed
|
||||
var h = 1.234e2
|
||||
var i = -0.0
|
||||
System.print([f, g, h, i])
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
0.
|
||||
.1
|
||||
1e3
|
||||
123.456E-300
|
||||
-123_456_789e+123
|
||||
|
|
@ -0,0 +1 @@
|
|||
byte &DB,&0F,&49,&40 ;0x40490FDB or 3.141592654 (single-precision)
|
||||
|
|
@ -0,0 +1 @@
|
|||
1.0, 0.1, 3.1415, 1.e-100, 1.2e100, -1e10, -1e+10, 123.456E-300
|
||||
Loading…
Add table
Add a link
Reference in a new issue