Files
std/math/log10.sakura
T
2026-12-06 13:12:06 -06:00

105 lines
3.3 KiB
Plaintext

#! @file log10.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the base 10 logarithm.
#!
#! Ported from musl 1.2.5, which is Copyright © 2005-2020 Rich Felker, et al.,
#! under the MIT license. Its `log10` is from FreeBSD's e_log10.c, which says:
#!
#! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
#!
#! Developed at SunSoft, a Sun Microsystems, Inc. business.
#! Permission to use, copy, modify, and distribute this
#! software is freely granted, provided that this notice
#! is preserved.
#!
#! It is within 1 ulp of the true value for `f64`. An `f32` is worked in `f64`
#! and rounded once more.
module std::math;
#! The base 10 logarithm of a real, the power 10 is raised to to give it.
#! @tparam T The type of real.
#! @param x The real.
#! @returns log10 x: 0 for 1, negative infinity for zero, infinity for
#! infinity, and NaN below zero.
pub def log10<T>(x: T): T where T: real {
return log10_double(x as f64) as T;
}
# The data of `log10`, from musl's log10.c.
# 1 / ln 10, as a high part of 33 bits and a low part.
const log10_inverse_ln10_high: f64 = 0.4342944818781689;
const log10_inverse_ln10_low: f64 = 2.5082946711645275e-11;
# log10 2, as a high part and a low part.
const log10_log10_2_high: f64 = 0.30102999566361177;
const log10_log10_2_low: f64 = 3.694239077158931e-13;
# The polynomial of (ln(1 + f) - f + f^2 / 2) / s, in s^2, where s is
# f / (2 + f): its coefficients from s^2's.
const log10_lg1: f64 = 0.6666666666666735;
const log10_lg2: f64 = 0.3999999999940942;
const log10_lg3: f64 = 0.2857142874366239;
const log10_lg4: f64 = 0.22222198432149784;
const log10_lg5: f64 = 0.1818357216161805;
const log10_lg6: f64 = 0.15313837699209373;
const log10_lg7: f64 = 0.14798198605116586;
# log10 x, from musl's log10.c.
def log10_double(x: f64): f64 {
var v := x;
var hx := high_word(v);
var k: i32 = 0;
if hx < 0x00100000 || (hx >> 31) != 0 {
# Zero, below zero, or subnormal.
return -1.0 / (v * v) if (bits_of(v) << 1) == 0;
return (v - v) / 0.0 if (hx >> 31) != 0;
# A subnormal, scaled up by 2^54.
k -= 54;
v *= 18014398509481984.0;
hx = high_word(v);
} else if hx >= 0x7FF00000 {
return v;
} else if hx == 0x3FF00000 && (bits_of(v) << 32) == 0 {
return 0.0;
}
# x is 2^k (1 + f), where 1 + f is in [sqrt(2) / 2, sqrt(2)).
hx += 0x3FF00000 - 0x3FE6A09E;
k += ((hx >> 20) as i32) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
v = with_high_word(v, hx);
val f := v - 1.0;
val hfsq := 0.5 * f * f;
val s := f / (2.0 + f);
val z := s * s;
var w := z * z;
val t1 := w * (log10_lg2 + w * (log10_lg4 + w * log10_lg6));
val t2 := z * (log10_lg1 + w * (log10_lg3 + w * (log10_lg5 + w * log10_lg7)));
val r := t2 + t1;
# hi + lo is f - hfsq + s * (hfsq + r), about ln(1 + f), hi of 32 bits.
val hi := real_of_bits(bits_of(f - hfsq) & 0xFFFFFFFF00000000);
val lo := f - hi - hfsq + s * (hfsq + r);
# high + low is log10(1 + f) + k log10 2.
var high := hi * log10_inverse_ln10_high;
val dk := k as f64;
val y := dk * log10_log10_2_high;
var low := dk * log10_log10_2_low + (lo + hi) * log10_inverse_ln10_low + lo * log10_inverse_ln10_high;
# y added in extra precision.
w = y + high;
low += (y - w) + high;
high = w;
return low + high;
}