Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Extreme-floating-point-values/00-META.yaml
Normal file
4
Task/Extreme-floating-point-values/00-META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Irrational numbers
|
||||
from: http://rosettacode.org/wiki/Extreme_floating_point_values
|
||||
19
Task/Extreme-floating-point-values/00-TASK.txt
Normal file
19
Task/Extreme-floating-point-values/00-TASK.txt
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity.
|
||||
|
||||
The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe other), extreme floating point values in your language and assign them to variables.
|
||||
|
||||
Print the values of these variables if possible; and show some arithmetic with these values and variables.
|
||||
|
||||
If your language can directly enter these extreme floating point values then show it.
|
||||
|
||||
|
||||
;See also:
|
||||
* [https://www.validlab.com/goldberg/paper.pdf What Every Computer Scientist Should Know About Floating-Point Arithmetic]
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Infinity]]
|
||||
* [[Detect division by zero]]
|
||||
* [[Literals/Floating point]]
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
BEGIN {
|
||||
# This requires 1e400 to overflow to infinity.
|
||||
nzero = -0
|
||||
nan = 0 * 1e400
|
||||
pinf = 1e400
|
||||
ninf = -1e400
|
||||
|
||||
print "nzero =", nzero
|
||||
print "nan =", nan
|
||||
print "pinf =", pinf
|
||||
print "ninf =", ninf
|
||||
print
|
||||
|
||||
# When y == 0, sign of x decides if atan2(y, x) is 0 or pi.
|
||||
print "atan2(0, 0) =", atan2(0, 0)
|
||||
print "atan2(0, pinf) =", atan2(0, pinf)
|
||||
print "atan2(0, nzero) =", atan2(0, nzero)
|
||||
print "atan2(0, ninf) =", atan2(0, ninf)
|
||||
print
|
||||
|
||||
# From least to most: ninf, -1e200, 1e200, pinf.
|
||||
print "ninf * -1 =", ninf * -1
|
||||
print "pinf * -1 =", pinf * -1
|
||||
print "-1e200 > ninf?", (-1e200 > ninf) ? "yes" : "no"
|
||||
print "1e200 < pinf?", (1e200 < pinf) ? "yes" : "no"
|
||||
print
|
||||
|
||||
# NaN spreads from input to output.
|
||||
print "nan test:", (1 + 2 * 3 - 4) / (-5.6e7 + nan)
|
||||
|
||||
# NaN never equals anything. These tests should print "no".
|
||||
print "nan == nan?", (nan == nan) ? "yes" : "no"
|
||||
print "nan == 42?", (nan == 42) ? "yes" : "no"
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
subtype Consistent_Float is Float range Float'Range; -- No IEEE ideals
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure IEEE is -- Non portable, bad, never do this!
|
||||
Zero : Float := 0.0;
|
||||
PInf : Float := 1.0 / Zero;
|
||||
NInf : Float := -PInf;
|
||||
PZero : Float := 1.0 / PInf;
|
||||
NZero : Float := 1.0 / NInf;
|
||||
NaN : Float := 0.0 / Zero;
|
||||
begin
|
||||
Put_Line (" -oo = " & Float'Image (NInf));
|
||||
Put_Line (" +oo = " & Float'Image (PInf));
|
||||
Put_Line (" NaN = " & Float'Image (NaN));
|
||||
Put_Line (" -0 = " & Float'Image (NZero));
|
||||
|
||||
Put_Line (" -oo < first " & Boolean'Image (NInf < Float'First));
|
||||
Put_Line (" +oo > last " & Boolean'Image (PInf > Float'Last));
|
||||
Put_Line (" NaN = NaN " & Boolean'Image (NaN = NaN));
|
||||
Put_Line (" -0 = 0 " & Boolean'Image (NZero = 0.0));
|
||||
Put_Line (" +0 = 0 " & Boolean'Image (PZero = 0.0));
|
||||
Put_Line (" +0 < least positive " & Boolean'Image (PZero < Float'Succ (Zero)));
|
||||
Put_Line (" -0 > biggest negative " & Boolean'Image (NZero > Float'Pred (Zero)));
|
||||
|
||||
-- Validness checks
|
||||
Put_Line ("Valid -oo is " & Boolean'Image (NInf'Valid));
|
||||
Put_Line ("Valid +oo is " & Boolean'Image (PInf'Valid));
|
||||
Put_Line ("Valid NaN is " & Boolean'Image (NaN'Valid));
|
||||
|
||||
end IEEE;
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
double inf = 1/0.0;
|
||||
double minus_inf = -1/0.0;
|
||||
double minus_zero = -1/ inf ;
|
||||
double nan = 0.0/0.0;
|
||||
|
||||
printf("positive infinity: %f\n",inf);
|
||||
printf("negative infinity: %f\n",minus_inf);
|
||||
printf("negative zero: %f\n",minus_zero);
|
||||
printf("not a number: %f\n",nan);
|
||||
|
||||
/* some arithmetic */
|
||||
|
||||
printf("+inf + 2.0 = %f\n",inf + 2.0);
|
||||
printf("+inf - 10.1 = %f\n",inf - 10.1);
|
||||
printf("+inf + -inf = %f\n",inf + minus_inf);
|
||||
printf("0.0 * +inf = %f\n",0.0 * inf);
|
||||
printf("1.0/-0.0 = %f\n",1.0/minus_zero);
|
||||
printf("NaN + 1.0 = %f\n",nan + 1.0);
|
||||
printf("NaN + NaN = %f\n",nan + nan);
|
||||
|
||||
/* some comparisons */
|
||||
|
||||
printf("NaN == NaN = %s\n",nan == nan ? "true" : "false");
|
||||
printf("0.0 == -0.0 = %s\n",0.0 == minus_zero ? "true" : "false");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
#include <values.h>
|
||||
#include <math.h>
|
||||
|
||||
char * bits(double v) {
|
||||
static char s[sizeof(double) * (CHARBITS + 1)];
|
||||
int n, i, j;
|
||||
unsigned char *c = (void*)&v;
|
||||
for (i = n = 0; i < sizeof(double); i++) {
|
||||
for (j = 1 << (CHARBITS - 1); j; j >>= 1)
|
||||
s[n++] = (c[i] & j) ? '1' : '.';
|
||||
s[n++] = ' ';
|
||||
}
|
||||
s[n-1] = 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double x[] = {
|
||||
1.0, -1.0, 1.0/256, 0.0, // "normal" values
|
||||
-0.0, INFINITY, -INFINITY, NAN, -NAN, // special
|
||||
DBL_MAX, DBL_MIN // not required by task
|
||||
};
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(x) / sizeof(x[0]); i++)
|
||||
printf("%s | %g\n", bits(x[i]), x[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(def neg-inf (/ -1.0 0.0)) ; Also Double/NEGATIVE_INFINITY
|
||||
(def inf (/ 1.0 0.0)) ; Also Double/POSITIVE_INFINITY
|
||||
(def nan (/ 0.0 0.0)) ; Also Double/NaN
|
||||
(def neg-zero (/ -2.0 Double/POSITIVE_INFINITY)) ; Also -0.0
|
||||
(println " Negative inf: " neg-inf)
|
||||
(println " Positive inf: " inf)
|
||||
(println " NaN: " nan)
|
||||
(println " Negative 0: " neg-zero)
|
||||
(println " inf + -inf: " (+ inf neg-inf))
|
||||
(println " NaN == NaN: " (= Double/NaN Double/NaN))
|
||||
(println "NaN equals NaN: " (.equals Double/NaN Double/NaN))
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
// Compile this module without -O
|
||||
|
||||
import std.stdio: writeln, writefln;
|
||||
import std.string: format;
|
||||
import std.math: NaN, getNaNPayload;
|
||||
|
||||
void show(T)() {
|
||||
static string toHex(T x) {
|
||||
string result;
|
||||
auto ptr = cast(ubyte*)&x;
|
||||
foreach_reverse (immutable i; 0 .. T.sizeof)
|
||||
result ~= format("%02x", ptr[i]);
|
||||
return result;
|
||||
}
|
||||
|
||||
enum string name = T.stringof;
|
||||
writeln("Computed extreme ", name, " values:");
|
||||
|
||||
T zero = 0.0;
|
||||
T pos_inf = T(1.0) / zero;
|
||||
writeln(" ", name, " +oo = ", pos_inf);
|
||||
|
||||
T neg_inf = -pos_inf;
|
||||
writeln(" ", name, " -oo = ", neg_inf);
|
||||
|
||||
T pos_zero = T(1.0) / pos_inf;
|
||||
writeln(" ", name, " +0 (pos_zero) = ", pos_zero);
|
||||
|
||||
T neg_zero = T(1.0) / neg_inf;
|
||||
writeln(" ", name, " -0 = ", neg_zero);
|
||||
|
||||
T nan = zero / pos_zero;
|
||||
writefln(" " ~ name ~ " zero / pos_zero = %f %s", nan, toHex(nan));
|
||||
writeln();
|
||||
|
||||
writeln("Some ", T.stringof, " properties and literals:");
|
||||
writeln(" ", name, " +oo = ", T.infinity);
|
||||
writeln(" ", name, " -oo = ", -T.infinity);
|
||||
writeln(" ", name, " +0 = ", T(0.0));
|
||||
writeln(" ", name, " -0 = ", T(-0.0));
|
||||
writefln(" " ~ name ~ " nan = %f %s", T.nan, toHex(T.nan));
|
||||
writefln(" " ~ name ~ " init = %f %s", T.init, toHex(T.init));
|
||||
writeln(" ", name, " epsilon = ", T.epsilon);
|
||||
writeln(" ", name, " max = ", T.max);
|
||||
writeln(" ", name, " -max = ", -T.max);
|
||||
writeln(" ", name, " min_normal = ", -T.min_normal);
|
||||
writeln("-----------------------------");
|
||||
}
|
||||
|
||||
void main() {
|
||||
show!float;
|
||||
show!double;
|
||||
show!real;
|
||||
|
||||
writeln("Largest possible payload for float, double and real NaNs:");
|
||||
immutable float f1 = NaN(0x3F_FFFF);
|
||||
writeln(getNaNPayload(f1));
|
||||
|
||||
immutable double f2 = NaN(0x3_FFFF_FFFF_FFFF);
|
||||
writeln(getNaNPayload(f2));
|
||||
|
||||
immutable real f3 = NaN(0x3FFF_FFFF_FFFF_FFFF);
|
||||
writeln(getNaNPayload(f3));
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
import std.math: FloatingPointControl;
|
||||
|
||||
void main() {
|
||||
// Enable hardware exceptions for division by zero, overflow
|
||||
// to infinity, invalid operations, and uninitialized
|
||||
// floating-point variables.
|
||||
FloatingPointControl fpc;
|
||||
fpc.enableExceptions(FloatingPointControl.severeExceptions);
|
||||
|
||||
double f0 = 0.0;
|
||||
double y1 = f0 / f0; // generates hardware exception
|
||||
// unless it's compiled with -O)
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
program Floats;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
var
|
||||
PlusInf, MinusInf, NegZero, NotANum: Double;
|
||||
|
||||
begin
|
||||
PlusInf:= 1.0/0.0;
|
||||
MinusInf:= -1.0/0.0;
|
||||
NegZero:= -1.0/PlusInf;
|
||||
NotANum:= 0.0/0.0;
|
||||
|
||||
Writeln('Positive Infinity: ', PlusInf); // +Inf
|
||||
Writeln('Negative Infinity: ', MinusInf); // -Inf
|
||||
Writeln('Negative Zero: ', NegZero); // -0.0
|
||||
Writeln('Not a Number: ', NotANum); // Nan
|
||||
|
||||
// allowed arithmetic
|
||||
Writeln('+Inf + 2.0 = ', PlusInf + 2.0); // +Inf
|
||||
Writeln('+Inf - 10.1 = ', PlusInf - 10.1); // +Inf
|
||||
Writeln('NaN + 1.0 = ', NotANum + 1.0); // Nan
|
||||
Writeln('NaN + NaN = ', NotANum + NotANum); // Nan
|
||||
|
||||
// throws exception
|
||||
try
|
||||
Writeln('+inf + -inf = ', PlusInf + MinusInf); // EInvalidOp
|
||||
Writeln('0.0 * +inf = ', 0.0 * PlusInf); // EInlalidOp
|
||||
Writeln('1.0/-0.0 = ', 1.0 / NegZero); // EZeroDivide
|
||||
except
|
||||
on E:Exception do
|
||||
Writeln(E.Classname, ': ', E.Message);
|
||||
end;
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
class
|
||||
APPLICATION
|
||||
inherit
|
||||
ARGUMENTS
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Run application.
|
||||
local
|
||||
negInf, posInf, negZero, nan: REAL_64
|
||||
do
|
||||
negInf := -1. / 0. -- also {REAL_64}.negative_infinity
|
||||
posInf := 1. / 0. -- also {REAL_64}.positive_infinity
|
||||
negZero := -1. / posInf
|
||||
nan := 0. / 0. -- also {REAL_64}.nan
|
||||
|
||||
print("Negative Infinity: ") print(negInf) print("%N")
|
||||
print("Positive Infinity: ") print(posInf) print("%N")
|
||||
print("Negative Zero: ") print(negZero) print("%N")
|
||||
print("NaN: ") print(nan) print("%N%N")
|
||||
|
||||
print("1.0 + Infinity = ") print((1.0 + posInf)) print("%N")
|
||||
print("1.0 - Infinity = ") print((1.0 - posInf)) print("%N")
|
||||
print("-Infinity + Infinity = ") print((negInf + posInf)) print("%N")
|
||||
print("-0.0 * Infinity = ") print((negZero * posInf)) print("%N")
|
||||
print("NaN + NaN = ") print((nan + nan)) print("%N")
|
||||
print("(NaN = NaN) = ") print((nan = nan)) print("%N")
|
||||
print("(0.0 = -0.0) = ") print((0.0 = negZero)) print("%N")
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
constant inf = 1E400
|
||||
constant minus_inf = -inf
|
||||
constant nan = 0*inf
|
||||
|
||||
printf(1,"positive infinity: %f\n", inf)
|
||||
printf(1,"negative infinity: %f\n", minus_inf)
|
||||
printf(1,"not a number: %f\n", nan)
|
||||
|
||||
-- some arithmetic
|
||||
|
||||
printf(1,"+inf + 2.0 = %f\n", inf + 2.0)
|
||||
printf(1,"+inf - 10.1 = %f\n", inf - 10.1)
|
||||
printf(1,"+inf + -inf = %f\n", inf + minus_inf)
|
||||
printf(1,"0.0 * +inf = %f\n", 0.0 * inf)
|
||||
printf(1,"NaN + 1.0 = %f\n", nan + 1.0)
|
||||
printf(1,"NaN + NaN = %f\n", nan + nan)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
0.0/0.0 //->nan
|
||||
0.0/(-0.0) //->nan
|
||||
1.0/infinity //->0.0
|
||||
1.0/(-infinity) //->0.0
|
||||
1.0/0.0 //->infinity
|
||||
1.0/(-0.0) //->-infinity
|
||||
-infinity<infinity //->true
|
||||
(-0.0)<0.0 //->false
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
-0. . ! -0.0 literal negative zero
|
||||
0. neg . ! -0.0 neg works with floating point zeros
|
||||
0. -1. * . ! -0.0 calculating negative zero
|
||||
1/0. . ! 1/0. literal positive infinity
|
||||
1e3 1e3 ^ . ! 1/0. calculating positive infinity
|
||||
-1/0. . ! -1/0. literal negative infinity
|
||||
-1. 1e3 1e3 ^ * . ! -1/0. calculating negative infinity
|
||||
-1/0. neg . ! 1/0. neg works with the inifinites
|
||||
0/0. . ! NAN: 8000000000000 literal NaN, configurable with
|
||||
! arbitrary 64-bit hex payload
|
||||
1/0. 1/0. - . ! NAN: 8000000000000 calculating NaN by subtracting
|
||||
! infinity from infinity
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
1e 0e f/ f. \ inf
|
||||
-1e 0e f/ f. \ inf (output bug: should say "-inf")
|
||||
-1e 0e f/ f0< . \ -1 (true, it is -inf)
|
||||
0e 0e f/ f. \ nan
|
||||
-1e 0e f/ 1/f f0< . \ 0 (false, can't represent IEEE negative zero)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
REAL*8 BAD,NaN !Sometimes a number is not what is appropriate.
|
||||
PARAMETER (NaN = Z'FFFFFFFFFFFFFFFF') !This value is recognised in floating-point arithmetic.
|
||||
PARAMETER (BAD = Z'FFFFFFFFFFFFFFFF') !I pay special attention to BAD values.
|
||||
CHARACTER*3 BADASTEXT !Speakable form.
|
||||
DATA BADASTEXT/" ? "/ !Room for "NaN", short for "Not a Number", if desired.
|
||||
REAL*8 PINF,NINF !Special values. No sign of an "overflow" state, damnit.
|
||||
PARAMETER (PINF = Z'7FF0000000000000') !May well cause confusion
|
||||
PARAMETER (NINF = Z'FFF0000000000000') !On a cpu not using this scheme.
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
Cause various arithmetic errors to see what sort of hissy fit is thrown.
|
||||
REAL X2,X3,X4,Y4,XX,ZERO
|
||||
INTEGER IX4,IY4
|
||||
EQUIVALENCE (X4,IX4),(Y4,IY4) !To view bits without provoking special fp handling.
|
||||
REAL*4 NaN4
|
||||
PARAMETER (NaN4 = Z'FFC00000') !FFFFFFFF
|
||||
c PARAMETER (NaN4 = Z'FFFFFFFF') !FFFFFFFF
|
||||
REAL*8 NaN8,X8(5),Y8,INF8
|
||||
PARAMETER (NaN8 = Z'FFF8000000000000') !FFFFFFFF
|
||||
c PARAMETER (NaN8 = Z'FFFFFFFFFFFFFFFF')
|
||||
LOGICAL LX(5)
|
||||
INTEGER I
|
||||
X4 = NaN4
|
||||
WRITE (6,1) X4,IX4
|
||||
1 FORMAT ("X4 =",F12.4,' Hex ',Z8)
|
||||
WRITE (6,*) "Test X4 .EQ. Bad? ",X4.EQ.NaN4
|
||||
WRITE (6,*) "Test X4 .NE. Bad? ",X4.NE.NaN4
|
||||
WRITE (6,*) "Test IsNaN(X4) ",ISNAN(X4)
|
||||
WRITE (6,*) "Test Abs(bad) ",ABS(X4)
|
||||
c WRITE (6,*) "Test Exp(bad)",EXP(X4)
|
||||
Y8 = HUGE(Y8)
|
||||
WRITE(6,*) "Huge",Y8,LOG(Y8)
|
||||
Y8 = LOG(Y8)
|
||||
WRITE (6,*) "Hic",EXP(Y8)
|
||||
|
||||
X2 = 0
|
||||
X3 = 0
|
||||
ZERO = 0
|
||||
XX = 666.66
|
||||
X2 = XX + X4
|
||||
WRITE (6,*) "Test x + BAD ",X2
|
||||
WRITE (6,*) "Test 0/0 ",X3/ZERO
|
||||
WRITE (6,*) "Test 1/0 ",1/ZERO
|
||||
WRITE (6,*) "Test-1/0 ",-1/ZERO
|
||||
X2 = MIN(XX,X4)
|
||||
WRITE (6,*) "Test min(x,Bad) ",X2
|
||||
WRITE (6,*) "Test min(x,NaN4)",MIN(XX,NaN4)
|
||||
c WRITE (6,*) "Test mod(x,Bad) ",MOD(XX,X4)
|
||||
c WRITE (6,*) "Test mod(Bad,x) ",MOD(X4,XX)
|
||||
c WRITE (6,*) "Test mod(x,0) ",MOD(XX,Z)
|
||||
c WRITE (6,*) "Sqrt(Bad)",SQRT(X4)
|
||||
|
||||
DO I = 1,0,-1 !for sqrt(-1), a snarl.
|
||||
X4 = I
|
||||
X4 = X4/FLOAT(I)
|
||||
Y4 = SQRT(FLOAT(I))
|
||||
WRITE (6,10) I,I,X4,IX4,I,Y4,IY4
|
||||
10 FORMAT (I3,"/",I3," gives",F9.5," Hex ",Z8,
|
||||
1 ", Sqrt(",I3,") gives",F9.5," Hex ",Z8)
|
||||
END DO
|
||||
|
||||
Contemplate double precision.
|
||||
WRITE (6,*)
|
||||
WRITE (6,*) "Problems with IsNaN and arrays..."
|
||||
DO I = 1,5
|
||||
X8(I) = I
|
||||
END DO
|
||||
X8(3:4) = NaN8
|
||||
WRITE (6,*) "X=",X8
|
||||
WRITE (6,*) "X(2:4)=",X8(2:4)
|
||||
WRITE (6,*) "isnan(x(2:4))",ISNAN(X8(2:4))
|
||||
WRITE (6,*) "isnan(x(2))..(4))",ISNAN(X8(2)),ISNAN(X8(3)),
|
||||
1 ISNAN(X8(4))
|
||||
WRITE (6,*) "abs(x(2:4))",ABS(X8(2:4))
|
||||
WRITE (6,*) "isnan(abs(x(2:4)))",ISNAN(ABS(X8(2:4)))
|
||||
LX = ISNAN(X8)
|
||||
WRITE (6,*) "LX = isnan(X)",LX
|
||||
|
||||
XX = HUGE(XX)
|
||||
WRITE(6,*) "Huge(x)=",XX,-XX
|
||||
XX = 1/ZERO
|
||||
WRITE(6,11) XX,-XX
|
||||
11 FORMAT("1/Zero=",Z8,", neg ",Z8)
|
||||
INF8 = XX
|
||||
WRITE (6,12) INF8,-INF8
|
||||
12 FORMAT("1/Zero=",Z16,", neg ",Z16)
|
||||
WRITE (6,*) "Burp!"
|
||||
END
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
#Include "crt/math.bi"
|
||||
|
||||
Dim inf As Double = INFINITY
|
||||
Dim negInf As Double = -INFINITY
|
||||
Dim notNum As Double = NAN_
|
||||
Dim negZero As Double = 1.0 / negInf
|
||||
|
||||
Print inf, inf / inf
|
||||
Print negInf, negInf * negInf
|
||||
Print notNum, notNum + inf + negInf
|
||||
Print negZero, negZero - 1
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// compute "extreme values" from non-extreme values
|
||||
var zero float64 // zero is handy.
|
||||
var negZero, posInf, negInf, nan float64 // values to compute.
|
||||
negZero = zero * -1
|
||||
posInf = 1 / zero
|
||||
negInf = -1 / zero
|
||||
nan = zero / zero
|
||||
|
||||
// print extreme values stored in variables
|
||||
fmt.Println(negZero, posInf, negInf, nan)
|
||||
|
||||
// directly obtain extreme values
|
||||
fmt.Println(math.Float64frombits(1<<63),
|
||||
math.Inf(1), math.Inf(-1), math.NaN())
|
||||
|
||||
// validate some arithmetic on extreme values
|
||||
fmt.Println()
|
||||
validateNaN(negInf+posInf, "-Inf + Inf")
|
||||
validateNaN(0*posInf, "0 * Inf")
|
||||
validateNaN(posInf/posInf, "Inf / Inf")
|
||||
// mod is specifically named in "What every computer scientist..."
|
||||
// Go math package doc lists many special cases for other package functions.
|
||||
validateNaN(math.Mod(posInf, 1), "Inf % 1")
|
||||
validateNaN(1+nan, "1 + NaN")
|
||||
validateZero(1/posInf, "1 / Inf")
|
||||
validateGT(posInf, math.MaxFloat64, "Inf > max value")
|
||||
validateGT(-math.MaxFloat64, negInf, "-Inf < max neg value")
|
||||
validateNE(nan, nan, "NaN != NaN")
|
||||
validateEQ(negZero, 0, "-0 == 0")
|
||||
}
|
||||
|
||||
func validateNaN(n float64, op string) {
|
||||
if math.IsNaN(n) {
|
||||
fmt.Println(op, "-> NaN")
|
||||
} else {
|
||||
fmt.Println("!!! Expected NaN from", op, " Found", n)
|
||||
}
|
||||
}
|
||||
|
||||
func validateZero(n float64, op string) {
|
||||
if n == 0 {
|
||||
fmt.Println(op, "-> 0")
|
||||
} else {
|
||||
fmt.Println("!!! Expected 0 from", op, " Found", n)
|
||||
}
|
||||
}
|
||||
|
||||
func validateGT(a, b float64, op string) {
|
||||
if a > b {
|
||||
fmt.Println(op)
|
||||
} else {
|
||||
fmt.Println("!!! Expected", op, " Found not true.")
|
||||
}
|
||||
}
|
||||
|
||||
func validateNE(a, b float64, op string) {
|
||||
if a == b {
|
||||
fmt.Println("!!! Expected", op, " Found not true.")
|
||||
} else {
|
||||
fmt.Println(op)
|
||||
}
|
||||
}
|
||||
|
||||
func validateEQ(a, b float64, op string) {
|
||||
if a == b {
|
||||
fmt.Println(op)
|
||||
} else {
|
||||
fmt.Println("!!! Expected", op, " Found not true.")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
def negInf = -1.0d / 0.0d; //also Double.NEGATIVE_INFINITY
|
||||
def inf = 1.0d / 0.0d; //also Double.POSITIVE_INFINITY
|
||||
def nan = 0.0d / 0.0d; //also Double.NaN
|
||||
def negZero = -2.0d / inf;
|
||||
|
||||
println(" Negative inf: " + negInf);
|
||||
println(" Positive inf: " + inf);
|
||||
println(" NaN: " + nan);
|
||||
println(" Negative 0: " + negZero);
|
||||
println(" inf + -inf: " + (inf + negInf));
|
||||
println(" 0 * NaN: " + (0 * nan));
|
||||
println(" NaN == NaN: " + (nan == nan));
|
||||
println("NaN equals NaN: " + (nan.equals(nan)));
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
main = do
|
||||
let inf = 1/0
|
||||
let minus_inf = -1/0
|
||||
let minus_zero = -1/inf
|
||||
let nan = 0/0
|
||||
|
||||
putStrLn ("Positive infinity = "++(show inf))
|
||||
putStrLn ("Negative infinity = "++(show minus_inf))
|
||||
putStrLn ("Negative zero = "++(show minus_zero))
|
||||
putStrLn ("Not a number = "++(show nan))
|
||||
|
||||
--Some Arithmetic
|
||||
|
||||
putStrLn ("inf + 2.0 = "++(show (inf+2.0)))
|
||||
putStrLn ("inf - 10 = "++(show (inf-10)))
|
||||
putStrLn ("inf - inf = "++(show (inf-inf)))
|
||||
putStrLn ("inf * 0 = "++(show (inf * 0)))
|
||||
putStrLn ("nan + 1.0= "++(show (nan+1.0)))
|
||||
putStrLn ("nan + nan = "++(show (nan + nan)))
|
||||
|
||||
--Some Comparisons
|
||||
|
||||
putStrLn ("nan == nan = "++(show (nan == nan)))
|
||||
putStrLn ("0.0 == - 0.0 = "++(show (0.0 == minus_zero)))
|
||||
putStrLn ("inf == inf = "++(show (inf == inf)))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Inf=: _
|
||||
NegInf=: __
|
||||
NB. Negative zero cannot be represented in J to be distinct from 0.
|
||||
NaN=. _.
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
(1 % 0) , (_1 % 0)
|
||||
_ __
|
||||
(1e234 * 1e234) , (_1e234 * 1e234)
|
||||
_ __
|
||||
_ + __ NB. generates NaN error, rather than NaN
|
||||
|NaN error
|
||||
| _ +__
|
||||
|
||||
_ - _ NB. generates NaN error, rather than NaN
|
||||
|NaN error
|
||||
| _ -_
|
||||
%_
|
||||
0
|
||||
%__ NB. Under the covers, the reciprocal of NegInf produces NegZero, but this fact isn't exposed to the user, who just sees zero
|
||||
0
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
_ + _
|
||||
_
|
||||
__ + __
|
||||
__
|
||||
Inf + 0
|
||||
_
|
||||
NegInf * 0
|
||||
0
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
public class Extreme {
|
||||
public static void main(String[] args) {
|
||||
double negInf = -1.0 / 0.0; //also Double.NEGATIVE_INFINITY
|
||||
double inf = 1.0 / 0.0; //also Double.POSITIVE_INFINITY
|
||||
double nan = 0.0 / 0.0; //also Double.NaN
|
||||
double negZero = -2.0 / inf;
|
||||
|
||||
System.out.println("Negative inf: " + negInf);
|
||||
System.out.println("Positive inf: " + inf);
|
||||
System.out.println("NaN: " + nan);
|
||||
System.out.println("Negative 0: " + negZero);
|
||||
System.out.println("inf + -inf: " + (inf + negInf));
|
||||
System.out.println("0 * NaN: " + (0 * nan));
|
||||
System.out.println("NaN == NaN: " + (nan == nan));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
0/0 #=> null
|
||||
1e1000 #=> 1.7976931348623157e+308
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def infinite: 1e1000;
|
||||
def nan: 0/0;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
-0 #=> -0
|
||||
0 == -0 # => true
|
||||
infinite == infinite #=> true
|
||||
infinite == -(-infinite) #=> true
|
||||
(infinite + infinite) == infinite #=> true
|
||||
1/infinite #=> 0
|
||||
|
||||
nan == nan #=> false # N.B.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
nan | isnan #=> true
|
||||
infinite | isnan #=> false
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
infinite as $inf | 1 / $inf #=> 0
|
||||
-0 as $z | $z #=> -0
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
function showextremes()
|
||||
values = [0.0, -0.0, Inf, -Inf, NaN]
|
||||
println(1 ./ values)
|
||||
end
|
||||
|
||||
showextremes()
|
||||
|
||||
@show Inf + 2.0
|
||||
@show Inf + Inf
|
||||
@show Inf - Inf
|
||||
@show Inf * Inf
|
||||
@show Inf / Inf
|
||||
@show Inf * 0
|
||||
@show 0 == -0
|
||||
@show NaN == NaN
|
||||
@show NaN === NaN
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
// version 1.0.5-2
|
||||
|
||||
@Suppress("DIVISION_BY_ZERO", "FLOAT_LITERAL_CONFORMS_ZERO")
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val inf = 1.0 / 0.0
|
||||
val negInf = -1.0 / 0.0
|
||||
val nan = 0.0 / 0.0
|
||||
val negZero = -1.0e-325
|
||||
|
||||
println("*** Indirect ***\n")
|
||||
println("Infinity : $inf")
|
||||
println("Negative infinity : $negInf")
|
||||
println("Not a number : $nan")
|
||||
println("Negative zero : $negZero")
|
||||
|
||||
println("\n*** Direct ***\n")
|
||||
println("Infinity : ${Double.POSITIVE_INFINITY}")
|
||||
println("Negative infinity : ${Double.NEGATIVE_INFINITY}")
|
||||
println("Not a number : ${Double.NaN}")
|
||||
println("Negative zero : ${-0.0}")
|
||||
|
||||
println("\n*** Calculations ***\n")
|
||||
println("inf * inf : ${inf * inf}")
|
||||
println("inf + negInf : ${inf + negInf}")
|
||||
println("nan / nan : ${nan / nan}")
|
||||
println("negZero + 0.0 : ${negZero + 0.0}")
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
local inf=math.huge
|
||||
local minusInf=-math.huge
|
||||
local NaN=0/0
|
||||
local negativeZeroSorta=-1E-240
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
1/(1/-math.huge)==math.huge
|
||||
true
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
EXTREMES
|
||||
NEW INF,NINF,ZERO,NOTNUM,NEGZERO
|
||||
SET INF=$DOUBLE(3.0E310),NINF=$DOUBLE(-3.0E310),ZERO=$DOUBLE(0),NOTNUM=$DOUBLE(INF-INF),NEGZERO=$DOUBLE(ZERO*-1)
|
||||
WRITE "Infinity: ",INF,!
|
||||
WRITE "Infinity ",$SELECT($ISVALIDNUM(INF):"is a number",1:"is not a number"),!
|
||||
WRITE "Negative Infinity: ",NINF,!
|
||||
WRITE "Negative Infinity ",$SELECT($ISVALIDNUM(NINF):"is a number",1:"is not a number"),!
|
||||
WRITE "Zero: ",ZERO,!
|
||||
WRITE "Zero ",$SELECT($ISVALIDNUM(ZERO):"is a number",1:"is not a number"),!
|
||||
WRITE "Negative Zero: ",NEGZERO,!
|
||||
WRITE "Negative Zero ",$SELECT($ISVALIDNUM(NEGZERO):"is a number",1:"is not a number"),!
|
||||
WRITE "Not a Number: ",NOTNUM,!
|
||||
WRITE "Not a Number ",$SELECT($ISVALIDNUM(NOTNUM):"is a number",1:"is not a number"),!
|
||||
KILL INF,NINF,ZERO,NONNUM,NEGZERO
|
||||
QUIT
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Column@{ReleaseHold[
|
||||
Function[expression,
|
||||
Row@{HoldForm@InputForm@expression, " = ", Quiet@expression},
|
||||
HoldAll] /@
|
||||
Hold[1./0., 0./0., Limit[-Log[x], x -> 0], Limit[Log[x], x -> 0],
|
||||
Infinity + 1, Infinity + Infinity, 2 Infinity,
|
||||
Infinity - Infinity, 0 Infinity, ComplexInfinity + 1,
|
||||
ComplexInfinity + ComplexInfinity, 2 ComplexInfinity,
|
||||
0 ComplexInfinity, Indeterminate + 1, 0 Indeterminate]]}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols binary
|
||||
|
||||
negInf = double -1.0 / 0.0; knegInf = Double.NEGATIVE_INFINITY
|
||||
inf = double 1.0 / 0.0; kinf = Double.POSITIVE_INFINITY
|
||||
nan = double 0.0 / 0.0; knan = Double.NaN
|
||||
negZero = double -2.0 / inf; knegZero = -2.0 / Double.POSITIVE_INFINITY
|
||||
|
||||
say "Negative inf: " Rexx(negInf).right(10) '|' knegInf
|
||||
say "Positive inf: " Rexx(inf).right(10) '|' kinf
|
||||
say "NaN: " Rexx(nan).right(10) '|' knan
|
||||
say "Negative 0: " Rexx(negZero).right(10) '|' knegZero
|
||||
say "inf + -inf: " Rexx(inf + negInf).right(10) '|' (kinf + knegInf)
|
||||
say "0 * NaN: " Rexx(0 * nan).right(10) '|' (0 * knan)
|
||||
say "NaN == NaN: " Rexx(nan == nan).right(10) '|' (knan == knan)
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
echo 1e234 * 1e234 # inf
|
||||
echo 1e234 * -1e234 # -inf
|
||||
echo 1 / Inf # 0
|
||||
echo Inf + -Inf # nan
|
||||
echo NaN # nan
|
||||
|
||||
echo NaN == NaN # false
|
||||
echo 0.0 == -0.0 # true
|
||||
echo 0.0 * NaN # nan
|
||||
echo NaN * 0.0 # nan
|
||||
echo 0.0 * Inf # nan
|
||||
echo Inf * 0.0 # nan
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
# infinity;;
|
||||
- : float = infinity
|
||||
# neg_infinity;;
|
||||
- : float = neg_infinity
|
||||
# nan;;
|
||||
- : float = nan
|
||||
# -0.;;
|
||||
- : float = -0.
|
||||
# -. 0.;;
|
||||
- : float = -0.
|
||||
# 1. /. 0.;;
|
||||
- : float = infinity
|
||||
# -1. /. 0.;;
|
||||
- : float = neg_infinity
|
||||
# -. infinity;;
|
||||
- : float = neg_infinity
|
||||
# infinity +. neg_infinity;;
|
||||
- : float = nan
|
||||
# 0. /. 0.;;
|
||||
- : float = nan
|
||||
# infinity /. infinity;;
|
||||
- : float = nan
|
||||
# nan = nan;;
|
||||
- : bool = false
|
||||
# nan == nan;;
|
||||
- : bool = true
|
||||
# 0. *. infinity;;
|
||||
- : float = nan
|
||||
# 0. = -0.;;
|
||||
- : bool = true
|
||||
# 0. == -0.;;
|
||||
- : bool = false
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
(import (scheme inexact))
|
||||
|
||||
(print "infinity: " (/ 1 0))
|
||||
(print "minus infinity: " (log 0))
|
||||
|
||||
; note: (sqrt -1) function will produce 0+i complex number
|
||||
; so we need to use simpler function "fsqrt"
|
||||
|
||||
(import (owl math fp))
|
||||
(print "not-a-number: " (fsqrt -1))
|
||||
|
||||
; note: your must use equal? or eqv? but not eq? for comparison
|
||||
(print "is this is not a number? " (equal? (fsqrt -1) +nan.0))
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
declare
|
||||
Inf = 1.0e234 * 1.0e234
|
||||
MinusInf = 1.0e234 * ~1.0e234
|
||||
Zero = 1.0 / Inf
|
||||
MinusZero = 1.0 / MinusInf
|
||||
NaN = 0.0 / 0.0
|
||||
|
||||
{System.showInfo "infinite: "#Inf}
|
||||
{System.showInfo "-infinite: "#MinusInf}
|
||||
{System.showInfo "0: "#Zero}
|
||||
{System.showInfo "-0: "#MinusZero} %% seems to be identical to Zero
|
||||
{System.showInfo "NaN: "#NaN}
|
||||
|
||||
{System.showInfo "inf + -inf: "#Inf+MinusInf}
|
||||
{System.showInfo "NaN * 0: "#NaN*0.0}
|
||||
{System.showInfo "0 * NaN: "#0.0*NaN}
|
||||
{System.showInfo "inf * 0: "#Inf*0.0}
|
||||
{System.showInfo "0 * inf: "#0.0*Inf}
|
||||
|
||||
{Show NaN == NaN} %% shows 'true' !
|
||||
{Show Zero == MinusZero}
|
||||
|
||||
{Show 1.0/0.0 == Inf} %% true
|
||||
{Show 1.0/~0.0 == MinusInf} %% true
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
infinite: 1.#INF
|
||||
-infinite: -1.#INF
|
||||
0: 0.0
|
||||
-0: 0.0
|
||||
NaN: -1.#IND
|
||||
inf + -inf: -1.#IND
|
||||
NaN * 0: -1.#IND
|
||||
0 * NaN: -1.#IND
|
||||
inf * 0: -1.#IND
|
||||
0 * inf: -1.#IND
|
||||
true
|
||||
true
|
||||
true
|
||||
true
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $nzero = -0.0;
|
||||
my $nan = 0 + "nan";
|
||||
my $pinf = +"inf";
|
||||
my $ninf = -"inf";
|
||||
|
||||
printf "\$nzero = %.1f\n", $nzero;
|
||||
print "\$nan = $nan\n";
|
||||
print "\$pinf = $pinf\n";
|
||||
print "\$ninf = $ninf\n\n";
|
||||
|
||||
printf "atan2(0, 0) = %g\n", atan2(0, 0);
|
||||
printf "atan2(0, \$nzero) = %g\n", atan2(0, $nzero);
|
||||
printf "sin(\$pinf) = %g\n", sin($pinf);
|
||||
printf "\$pinf / -1 = %g\n", $pinf / -1;
|
||||
printf "\$ninf + 1e100 = %g\n\n", $ninf + 1e100;
|
||||
|
||||
printf "nan test: %g\n", (1 + 2 * 3 - 4) / (-5.6e7 * $nan);
|
||||
printf "nan == nan? %s\n", ($nan == $nan) ? "yes" : "no";
|
||||
printf "nan == 42? %s\n", ($nan == 42) ? "yes" : "no";
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Math::BigInt;
|
||||
|
||||
my $nan = Math::BigInt->bnan();
|
||||
my $pinf = Math::BigInt->binf();
|
||||
my $ninf = Math::BigInt->binf('-');
|
||||
|
||||
print "\$nan = $nan\n";
|
||||
print "\$pinf = $pinf\n";
|
||||
print "\$ninf = $ninf\n\n";
|
||||
|
||||
my $huge = Math::BigInt->new("123456789");
|
||||
$huge->bmul($huge)->bmul($huge)->bmul($huge);
|
||||
|
||||
print "\$huge = $huge\n";
|
||||
printf "\$ninf + \$huge = %s\n", $ninf->copy()->badd($huge);
|
||||
printf "\$pinf - \$huge = %s\n", $pinf->copy()->bsub($huge);
|
||||
printf "\$nan * \$huge = %s\n", $nan->copy()->bmul($huge);
|
||||
printf "\$nan == \$nan? %s\n", defined($nan->bcmp($nan)) ? "maybe" : "no";
|
||||
printf "\$nan == \$huge? %s\n", defined($nan->bcmp($huge)) ? "maybe" : "no";
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">inf</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e300</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1e300</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (works on both 32 and 64 bit)</span>
|
||||
<span style="color: #000000;">ninf</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">nan</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-(</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">/</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">nzero</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">inf</span> <span style="color: #000080;font-style:italic;">-- (not supported)</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;">" inf: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">})</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;">" ninf: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ninf</span><span style="color: #0000FF;">})</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;">" nan: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">})</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;">"*nzero: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nzero</span><span style="color: #0000FF;">})</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;">" inf+2: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</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;">" inf+ninf: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ninf</span><span style="color: #0000FF;">})</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;">" 0*inf: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">*</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">})</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;">" nan+1: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</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;">" nan+nan: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">+</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">})</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;">" inf>1e300: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">></span><span style="color: #000000;">1e300</span><span style="color: #0000FF;">})</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;">" ninf<1e300: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ninf</span><span style="color: #0000FF;"><-</span><span style="color: #000000;">1e300</span><span style="color: #0000FF;">})</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;">"*nan=nan: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">=</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">})</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;">" nan=42: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">=</span><span style="color: #000000;">42</span><span style="color: #0000FF;">})</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;">"*nan<0: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</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;">" nan>0: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(load "@lib/math.l")
|
||||
|
||||
: (exp 1000.0) # Too large for IEEE floats
|
||||
-> T
|
||||
|
||||
: (+ 1 2 NIL 3) # NaN propagates
|
||||
-> NIL
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
Define.f
|
||||
If OpenConsole()
|
||||
inf = Infinity() ; or 1/None ;None represents a variable of value = 0
|
||||
minus_inf = -Infinity() ; or -1/None
|
||||
minus_zero = -1/inf
|
||||
nan = NaN() ; or None/None
|
||||
|
||||
PrintN("positive infinity: "+StrF(inf))
|
||||
PrintN("negative infinity: "+StrF(minus_inf))
|
||||
PrintN("positive zero: "+StrF(None))
|
||||
PrintN("negative zero: "+StrF(minus_zero)) ; handles as 0.0
|
||||
PrintN("not a number: "+StrF(nan))
|
||||
PrintN("Arithmetics")
|
||||
PrintN("+inf + 2.0 = "+StrF(inf + 2.0))
|
||||
PrintN("+inf - 10.1 = "+StrF(inf - 10.1))
|
||||
PrintN("+inf + -inf = "+StrF(inf + minus_inf))
|
||||
PrintN("0.0 * +inf = "+StrF(0.0 * inf))
|
||||
PrintN("1.0/-0.0 = "+StrF(1.0/minus_zero))
|
||||
PrintN("NaN + 1.0 = "+StrF(nan + 1.0))
|
||||
PrintN("NaN + NaN = "+StrF(nan + nan))
|
||||
PrintN("Logics")
|
||||
If IsInfinity(inf): PrintN("Variable 'Infinity' is infinite"): EndIf
|
||||
If IsNAN(nan): PrintN("Variable 'nan' is not a number"): EndIf
|
||||
|
||||
Print(#CRLF$+"Press ENTER to EXIT"): Input()
|
||||
EndIf
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
>>> # Extreme values from expressions
|
||||
>>> inf = 1e234 * 1e234
|
||||
>>> _inf = 1e234 * -1e234
|
||||
>>> _zero = 1 / _inf
|
||||
>>> nan = inf + _inf
|
||||
>>> inf, _inf, _zero, nan
|
||||
(inf, -inf, -0.0, nan)
|
||||
>>> # Print
|
||||
>>> for value in (inf, _inf, _zero, nan): print (value)
|
||||
|
||||
inf
|
||||
-inf
|
||||
-0.0
|
||||
nan
|
||||
>>> # Extreme values from other means
|
||||
>>> float('nan')
|
||||
nan
|
||||
>>> float('inf')
|
||||
inf
|
||||
>>> float('-inf')
|
||||
-inf
|
||||
>>> -0.
|
||||
-0.0
|
||||
>>> # Some arithmetic
|
||||
>>> nan == nan
|
||||
False
|
||||
>>> nan is nan
|
||||
True
|
||||
>>> 0. == -0.
|
||||
True
|
||||
>>> 0. is -0.
|
||||
False
|
||||
>>> inf + _inf
|
||||
nan
|
||||
>>> 0.0 * nan
|
||||
nan
|
||||
>>> nan * 0.0
|
||||
nan
|
||||
>>> 0.0 * inf
|
||||
nan
|
||||
>>> inf * 0.0
|
||||
nan
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
>>> # But note!
|
||||
>>> 1 / -0.0
|
||||
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#106>", line 1, in <module>
|
||||
1 / -0.0
|
||||
ZeroDivisionError: float division by zero
|
||||
>>> # (Not minus infinity)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
# 0 and -0 are recognized but are both printed as simply 0.
|
||||
1/c(0, -0, Inf, -Inf, NaN)
|
||||
# Inf -Inf 0 0 NaN
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
/*REXX pgm shows smallest & largest positive numbers that can be expressed, compares 0's*/
|
||||
parse version v; say 'version=' v; say
|
||||
zero= '0.0' /*a (positive) value for zero. */
|
||||
negZero= '-0.0' /*" negative " " " */
|
||||
say 'value of zero equals negZero: ' word('no yes', 1 + (zero = negZero) )
|
||||
say 'value of zero exactly equals negZero: ' word('no yes', 1 + (zero == negZero) )
|
||||
say
|
||||
do digs=20 by 20 to 100; numeric digits digs /*use a range of digits. */
|
||||
say center(' number of decimal digits being used:' digs" ", 79, '═')
|
||||
say 'tiny=' tiny()
|
||||
say 'huge=' huge()
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
tiny: return $xnum('1e-')
|
||||
huge: return $xnum('.'copies(9, digits() )"e+")
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
$xnum: procedure; parse arg $ /*use the given mantissa value.*/
|
||||
!=10 /*use starting exponent value.*/
|
||||
do forever; _=$ || ! /*construct a REXX decimal num.*/
|
||||
if \datatype(_, 'N') then leave /*Not numeric? Then leave. */
|
||||
p=!; !=! * 10 /*save number; magnify mantissa*/
|
||||
end /*forever*/
|
||||
j=! % 2 /*halve the exponent (power). */
|
||||
do forever; _=$ || ! /* [+] Not numeric? Halve it.*/
|
||||
if \datatype(_, 'N') then do; !=p; j=j % 2
|
||||
if j==0 then leave
|
||||
end
|
||||
p=!; !=! + j /*save number; bump mantissa. */
|
||||
end /*forever*/
|
||||
return $ || !
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#lang racket
|
||||
(define division-by-zero (/ 1.0 0.0)) ;+inf.0
|
||||
(define negative-inf (- (/ 1.0 0.0))) ;-inf.0
|
||||
(define zero 0.0) ;0.0
|
||||
(define negative-zero (- 0.0)) ;-0.0
|
||||
(define nan (/ 0.0 0.0)) ;+nan.0
|
||||
|
||||
(displayln division-by-zero)
|
||||
(displayln negative-inf)
|
||||
(displayln zero)
|
||||
(displayln negative-zero)
|
||||
(displayln nan)
|
||||
|
||||
(+ zero negative-zero) ;0.0
|
||||
(- negative-inf division-by-zero) ; +nan.0
|
||||
(+ zero nan) ; +nan.0
|
||||
(= nan +nan.0) ;#f
|
||||
|
|
@ -0,0 +1,16 @@
|
|||