Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -1,21 +1,21 @@
import std.stdio, std.math;
void main() {
enum real degrees = 45.0;
enum real t0 = degrees * PI / 180.0;
import std.stdio, std.math;
enum degrees = 45.0L;
enum t0 = degrees * PI / 180.0L;
writeln("Reference: 0.7071067811865475244008");
writefln("Sine: %.20f %.20f", sin(PI_4), sin(t0));
writefln("Cosine: %.20f %.20f", cos(PI_4), cos(t0));
writefln("Tangent: %.20f %.20f", tan(PI_4), tan(t0));
writefln("Sine: %.20f %.20f", PI_4.sin, t0.sin);
writefln("Cosine: %.20f %.20f", PI_4.cos, t0.cos);
writefln("Tangent: %.20f %.20f", PI_4.tan, t0.tan);
writeln();
writeln;
writeln("Reference: 0.7853981633974483096156");
immutable real t1 = asin(sin(PI_4));
writefln("Arcsine: %.20f %.20f", t1, t1 * 180.0 / PI);
immutable real t1 = PI_4.sin.asin;
writefln("Arcsine: %.20f %.20f", t1, t1 * 180.0L / PI);
immutable real t2 = acos(cos(PI_4));
writefln("Arccosine: %.20f %.20f", t2, t2 * 180.0 / PI);
immutable real t2 = PI_4.cos.acos;
writefln("Arccosine: %.20f %.20f", t2, t2 * 180.0L / PI);
immutable real t3 = atan(tan(PI_4));
writefln("Arctangent: %.20f %.20f", t3, t3 * 180.0 / PI);
immutable real t3 = PI_4.tan.atan;
writefln("Arctangent: %.20f %.20f", t3, t3 * 180.0L / PI);
}

View file

@ -40,13 +40,12 @@ sin: procedure; arg x; x=r2r(x); numeric fuzz min(5,digits()-3)
.sinCos: parse arg z 1 p,_,i; x=x*x
do k=2 by 2; _=-_*x/(k*(k+i));z=z+_;if z=p then leave;p=z;end; return z
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits();numeric digits 11
g=.sqrtGuess(); do j=0 while p>9; m.j=p; p=p%2+1; end
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end
numeric digits d; return g/1
.sqrtGuess: if x<0 then call sqrtErr; numeric form; m.=11; p=d+d%4+2
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2
sqrt: procedure; parse arg x; if x=0 then return 0; m.=9; p=digits(); i=
numeric digits 9; if x<0 then do; x=-x; i='i'; end; numeric form; m.0=p
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2; m.1=p
do j=2 while p>9; m.j=p; p=p%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=.5*(g+x/g); end /*k*/
numeric digits m.0; return (g/1)i
e: return,
2.7182818284590452353602874713526624977572470936999595749669676277240766303535
/*Note: the "real: E subroutine returns E's accuracy that */
@ -80,9 +79,8 @@ tellErr: say; say '*** error! ***'; say; say arg(1); say; exit 13
tanErr: call tellErr 'tan('||x") causes division by zero, X="||x
AsinErr: call tellErr 'Asin(x), X must be in the range of -1 +1, X='||x
AcosErr: call tellErr 'Acos(x), X must be in the range of -1 +1, X='||x
sqrtErr: call tellErr "sqrt(x), X can't be negative, X="||x
/* ┌───────────────────────────────────────────────────────────────┐
Not included here are: (among others):
Not included here are (among others):
some of the usual higher-math functions normally associated
with trig functions: POW, GAMMA, LGGAMMA, ERF, ERFC, ROOT,
LOG (LN), LOG2, LOG10, ATAN2,
@ -90,7 +88,8 @@ sqrtErr: call tellErr "sqrt(x), X can't be negative, X="||x
(too many to name here).
Angle conversions/normalizations: degrees/radians/grads/mils
[a circle = 2 pi radians, 360 degrees, 400 grads, 6400 mils].
Some of the other trig functions (hypens added intentially):
Some of the other trig functions are (hyphens were added
intentionally):
CHORD
COT (co-tangent)
CSC (co-secant)

View file

@ -10,26 +10,26 @@ end
# Arcsine of _y_, domain [-1, 1], range [-pi/2, pi/2].
def asin(y, prec)
# Handle angles with no tangent.
return -PI / 2 if y == -1
return PI / 2 if y == 1
# Handle angles with no tangent.
return -PI / 2 if y == -1
return PI / 2 if y == 1
# Tangent of angle is y / x, where x^2 + y^2 = 1.
atan(y / sqrt(1 - y * y, prec), prec)
# Tangent of angle is y / x, where x^2 + y^2 = 1.
atan(y / sqrt(1 - y * y, prec), prec)
end
# Arccosine of _x_, domain [-1, 1], range [0, pi].
def acos(x, prec)
# Handle angle with no tangent.
return PI / 2 if x == 0
# Handle angle with no tangent.
return PI / 2 if x == 0
# Tangent of angle is y / x, where x^2 + y^2 = 1.
a = atan(sqrt(1 - x * x, prec) / x, prec)
if a < 0
a + PI(prec)
else
a
end
# Tangent of angle is y / x, where x^2 + y^2 = 1.
a = atan(sqrt(1 - x * x, prec) / x, prec)
if a < 0
a + PI(prec)
else
a
end
end