τau / math /

math.tau

source
/Users/niconex/Documents/tau/stdlib/math/math.tau
1# math - the usual functions on floats.2#3# Everything returns a float, whatever it was given: an integer handed to one4# of these is converted on the way into C and comes back a float, and stays5# one through the arithmetic that follows.6#7# The functions are the ones of the C math library, declared and called8# through the ffi module. There is no shared object of our own in between,9# since the interpreter is already linked against libm and dlopen(null) is the10# handle of the program itself.1112ffi = import("ffi")1314# The C library the program was linked against.15libm = dlopen(null)1617Pi = 3.14159265358979318E = 2.71828182845904519Sqrt2 = 1.414213562373095120SqrtPi = 1.77245385090551621Ln2 = 0.693147180559945322Ln10 = 2.3025850929940462324# MaxInt is the largest integer that fits in a tau int, MinInt the smallest.25MaxInt = 922337203685477580726MinInt = -9223372036854775807 - 12728Sqrt = ffi.Func(libm.sqrt, "double sqrt(double)")29Cbrt = ffi.Func(libm.cbrt, "double cbrt(double)")30Exp = ffi.Func(libm.exp, "double exp(double)")31Log = ffi.Func(libm.log, "double log(double)")32Log2 = ffi.Func(libm.log2, "double log2(double)")33Log10 = ffi.Func(libm.log10, "double log10(double)")34Sin = ffi.Func(libm.sin, "double sin(double)")35Cos = ffi.Func(libm.cos, "double cos(double)")36Tan = ffi.Func(libm.tan, "double tan(double)")37Asin = ffi.Func(libm.asin, "double asin(double)")38Acos = ffi.Func(libm.acos, "double acos(double)")39Atan = ffi.Func(libm.atan, "double atan(double)")40Sinh = ffi.Func(libm.sinh, "double sinh(double)")41Cosh = ffi.Func(libm.cosh, "double cosh(double)")42Tanh = ffi.Func(libm.tanh, "double tanh(double)")43Floor = ffi.Func(libm.floor, "double floor(double)")44Ceil = ffi.Func(libm.ceil, "double ceil(double)")45Round = ffi.Func(libm.round, "double round(double)")46Trunc = ffi.Func(libm.trunc, "double trunc(double)")4748Pow = ffi.Func(libm.pow, "double pow(double, double)")49Atan2 = ffi.Func(libm.atan2, "double atan2(double, double)")50Mod = ffi.Func(libm.fmod, "double fmod(double, double)")51Hypot = ffi.Func(libm.hypot, "double hypot(double, double)")5253# Abs is the absolute value, an integer staying an integer: it is the one case54# where going through a float would lose the largest values.55Abs = fn(x) { if x < 0 { -x } else { x } }5657# Inf returns positive infinity, or negative when sign is negative. A division58# is how a float says it, and there is no function to ask for one.59Inf = fn(sign) {60	if sign != null && sign < 0 {61		return -1.0 / 0.062	}63	return 1.0 / 0.064}6566# NaN returns a value that is not a number, and IsNaN is how to recognise one:67# != does not follow IEEE here, so x != x is not the test it is in C.68NaN = fn() { 0.0 / 0.0 }6970# The two that C answers with an int, which here is a question with a yes or a71# no.72isnan = ffi.Func(libm.isnan, "int isnan(double)")73isinf = ffi.Func(libm.isinf, "int isinf(double)")7475IsNaN = fn(x) { isnan(x) != 0 }76IsInf = fn(x) { isinf(x) != 0 }7778Min = fn(a, b) { if a < b { a } else { b } }79Max = fn(a, b) { if a > b { a } else { b } }8081# Signum is -1, 0 or 1, following the sign of x.82Signum = fn(x) {83	if x > 0 {84		return 185	}86	if x < 0 {87		return -188	}89	return 090}