BASIC: scientific functions and OPTION ANGLE DEGREES
ASIN, ACOS, ANGLE, SEC, CSC, COT; SINH, COSH, TANH and their inverses; LOG10, LOG2; IP, FP, CEIL, ROUND, TRUNCATE; MOD; MIN and MAX of any number of values; FACT; PI, DEG, RAD; OPTION ANGLE DEGREES|RADIANS. Out-of-domain values give ILLEGAL FUNCTION CALL. basic/test-math.py checks each against Python's math (within 3 ulp); the manual and README cover them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
79aef8ed38
commit
25c8286cf2
8 files changed
+2985
-2375
No files matched your search
+133
-1
@@ -1,5 +1,6 @@
|
||||
/* Single-precision maths for BASIC: sinf cosf tanf atanf expf logf sqrtf
|
||||
floorf. Range reduction by Cody and Waite, minimax polynomials as in the
|
||||
floorf, and from them asinf acosf atan2f sinhf coshf tanhf asinhf acoshf
|
||||
atanhf log1pf log10f log2f and an exact fmodf. Range reduction by Cody and Waite, minimax polynomials as in the
|
||||
Cephes library; good to an ulp or two, which BASIC's 6-7 printed digits
|
||||
don't show. sqrtf (the FPU's FSQRT.S when there is one) and floorf are
|
||||
exact. (newlib's versions are larger and
|
||||
@@ -190,3 +191,134 @@ float floorf(float x)
|
||||
else if (u.i << 1) u.f = -1.0f;
|
||||
return u.f;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------- built on the above */
|
||||
static const float PI_F = 3.14159265358979f, LN2_F = 0.693147180559945f;
|
||||
|
||||
/* log(1 + u), accurate for small u: Goldberg's trick, since w - 1 is exact */
|
||||
float log1pf(float u)
|
||||
{
|
||||
float w = 1.0f + u;
|
||||
if (w == 1.0f) return u;
|
||||
return logf(w) * (u / (w - 1.0f));
|
||||
}
|
||||
|
||||
float asinf(float x) /* |x| <= 1 */
|
||||
{
|
||||
return atanf(x / sqrtf((1.0f - x) * (1.0f + x)));
|
||||
}
|
||||
|
||||
float acosf(float x) /* |x| <= 1 */
|
||||
{
|
||||
return 2.0f * atanf(sqrtf((1.0f - x) / (1.0f + x)));
|
||||
}
|
||||
|
||||
float atan2f(float y, float x)
|
||||
{
|
||||
float a;
|
||||
if (x == 0.0f) return y > 0 ? PI_F / 2 : y < 0 ? -PI_F / 2 : 0.0f;
|
||||
a = atanf(y / x);
|
||||
if (x < 0) a += y < 0 ? -PI_F : PI_F;
|
||||
return a;
|
||||
}
|
||||
|
||||
/* e^|x| / 2 for large |x|, without overflowing on the way */
|
||||
static float half_exp(float a)
|
||||
{
|
||||
float h = expf(0.5f * a);
|
||||
return (0.5f * h) * h;
|
||||
}
|
||||
|
||||
float sinhf(float x)
|
||||
{
|
||||
float a = x < 0 ? -x : x, r, z, e;
|
||||
if (a < 1.0f) { /* the series: no cancellation */
|
||||
z = a * a;
|
||||
r = a + a * z * (1.6666667163e-1f + z * (8.3333337680e-3f + z * (1.9841270114e-4f
|
||||
+ z * (2.7557318845e-6f + z * 2.5052108385e-8f))));
|
||||
} else if (a < 10.0f) {
|
||||
e = expf(a);
|
||||
r = 0.5f * (e - 1.0f / e);
|
||||
} else r = half_exp(a);
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
|
||||
float coshf(float x)
|
||||
{
|
||||
float a = x < 0 ? -x : x, e;
|
||||
if (a >= 10.0f) return half_exp(a);
|
||||
e = expf(a);
|
||||
return 0.5f * (e + 1.0f / e);
|
||||
}
|
||||
|
||||
float tanhf(float x)
|
||||
{
|
||||
float a = x < 0 ? -x : x, r;
|
||||
r = a >= 10.0f ? 1.0f : sinhf(a) / coshf(a);
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
|
||||
float asinhf(float x)
|
||||
{
|
||||
float a = x < 0 ? -x : x, r;
|
||||
if (a > 1e9f) r = logf(a) + LN2_F;
|
||||
else r = log1pf(a + a * a / (1.0f + sqrtf(1.0f + a * a)));
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
|
||||
float acoshf(float x) /* x >= 1 */
|
||||
{
|
||||
float t;
|
||||
if (x > 1e9f) return logf(x) + LN2_F;
|
||||
t = x - 1.0f;
|
||||
return log1pf(t + sqrtf(2.0f * t + t * t));
|
||||
}
|
||||
|
||||
float atanhf(float x) /* |x| < 1 */
|
||||
{
|
||||
float a = x < 0 ? -x : x, r;
|
||||
r = 0.5f * log1pf(2.0f * a / (1.0f - a));
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
|
||||
/* log of x = m * 2^e, m in [sqrt(1/2), sqrt(2)): returns log(m), sets e */
|
||||
static float log_split(float x, int *e)
|
||||
{
|
||||
fbits u = { x };
|
||||
int k = 0;
|
||||
if (u.i < 0x00800000) { u.f *= 16777216.0f; k = -24; } /* subnormal */
|
||||
k += (int)(u.i >> 23) - 127;
|
||||
u.i = (u.i & 0x007FFFFF) | 0x3F800000; /* [1, 2) */
|
||||
if (u.f > 1.41421356f) { u.f *= 0.5f; k++; }
|
||||
*e = k;
|
||||
return logf(u.f);
|
||||
}
|
||||
|
||||
float log2f(float x) /* x > 0: exact for powers of 2 */
|
||||
{
|
||||
int e;
|
||||
float y = log_split(x, &e);
|
||||
return y * 1.44269504088896341f + (float)e;
|
||||
}
|
||||
|
||||
float log10f(float x) /* x > 0 */
|
||||
{
|
||||
int e;
|
||||
float y = log_split(x, &e), f = (float)e;
|
||||
/* log10(2) and log10(e) each split in two, as in Cephes */
|
||||
return ((f * 2.48745663981195213739e-4f + y * 7.00731903251827651129e-4f)
|
||||
+ y * 4.3359375e-1f) + f * 3.0078125e-1f;
|
||||
}
|
||||
|
||||
/* exactly x - n*y, n = x/y truncated, by binary long division: each step
|
||||
subtracts t from r with t <= r < 2t, which is exact */
|
||||
float fmodf(float x, float y)
|
||||
{
|
||||
float r = x < 0 ? -x : x, d = y < 0 ? -y : y, t;
|
||||
if (d == 0.0f || r < d) return x;
|
||||
t = d;
|
||||
while (t <= 0.5f * r) t += t;
|
||||
for (; t >= d; t *= 0.5f)
|
||||
if (r >= t) r -= t;
|
||||
return x < 0 ? -r : r;
|
||||
}
|
||||
Reference in new issue
Block a user