Files
sakurac/core/containers/decimal.sakura
T
2026-12-06 13:52:57 -06:00

350 lines
8.9 KiB
Plaintext

#! @file decimal.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the conversion of decimal text to the nearest 64-bit float, which
#! `real::parse` reads numbers with.
#!
#! Most numbers have few enough digits, and a small enough exponent, that one
#! multiplication or division of two exact floats gives the nearest float. The
#! rest are converted as Go's `strconv` converts them: the number is held as
#! decimal digits, and multiplied and divided by powers of two, which a decimal
#! holds exactly, until its 53 bits can be read off and rounded.
module core;
# Digits a decimal holds; past them, only whether any was not zero matters.
const decimal_capacity: u64 = 800;
# Digits a shift by 60 bits can add, which is how far one shift goes.
const decimal_room: u64 = 19;
# The largest integer every integer below which a float holds exactly.
const decimal_exact: u64 = 9007199254740992;
# How far to shift a decimal whose point is a number of digits from where it should
# be, so the shift moves it by no more than that.
const decimal_shifts: u64[9] = [1, 3, 6, 9, 13, 16, 19, 23, 26];
# A number as decimal digits, each from 0 to 9, with the point `point` digits from
# the first, which may be before it or past the last.
struct decimal_digits {
var digits: u8[decimal_capacity + decimal_room];
var count: u64;
var point: i64;
var truncated: bool; #! Whether a digit past those held was not zero.
}
# The float nearest the number some digits write, which may hold a `.`, times ten
# to an exponent, or `nothing` where it is too large for one.
def decimal_digits::nearest(written: strview, exponent: i64): maybe<f64> {
# The first 19 digits after the leading zeros, and whether those after were all
# zero, which is all one exact multiplication needs.
var mantissa := 0u;
var held := 0u;
var scale := exponent;
var exact: bool = true;
var dotted: bool = false;
for byte in written {
if byte == '.' {
dotted = true;
continue;
}
if held == 0 && byte == '0' {
if dotted {
scale--;
}
continue;
}
if held < 19 {
mantissa = mantissa * 10 + byte.digit_value;
held++;
if dotted {
scale--;
}
continue;
}
if byte != '0' {
exact = false;
}
if !dotted {
scale++;
}
}
if mantissa == 0 {
return 0.0;
}
if exact && mantissa <= decimal_exact && scale >= -22 && scale <= 22 {
val whole := mantissa as f64;
return scale < 0 ? whole / decimal_digits::power_of_ten(-scale) : whole * decimal_digits::power_of_ten(scale);
}
var number: decimal_digits;
number.read(written, exponent);
return number.to_float();
}
# Reads the digits some text writes, without their leading zeros.
def decimal_digits::read(&mut this, written: strview, exponent: i64): void {
var dotted: bool = false;
for byte in written {
if byte == '.' {
dotted = true;
continue;
}
if this.count == 0 && byte == '0' {
if dotted {
this.point--;
}
continue;
}
if !dotted {
this.point++;
}
if this.count < decimal_capacity {
this.digits[this.count] = byte.digit_value as u8;
this.count++;
} else if byte != '0' {
this.truncated = true;
}
}
this.point += exponent;
this.trim();
}
# The float nearest the number, as Go's `strconv` finds it, or `nothing` where it
# is too large for one.
def decimal_digits::to_float(&mut this): maybe<f64> {
if this.count == 0 || this.point < -330 {
return 0.0;
}
if this.point > 310 {
return nothing{};
}
# Halve or double the number, counting how often, until it is in [0.5, 1).
var exponent: i64 = 0;
while this.point > 0 {
val by := decimal_digits::shift_for(this.point);
this.shift(-by);
exponent += by;
}
while this.point < 0 || (this.point == 0 && this.digits[0] < 5) {
val by := decimal_digits::shift_for(-this.point);
this.shift(by);
exponent -= by;
}
# A float's significand is in [1, 2), and below the smallest exponent a float
# holds, the number is subnormal, with fewer bits.
exponent--;
if exponent < -1022 {
val by := -1022 - exponent;
this.shift(-by);
exponent += by;
}
if exponent > 1023 {
return nothing{};
}
# Its 53 bits, rounded, which may round up to one bit more.
this.shift(53);
var mantissa := this.rounded();
if mantissa == decimal_exact {
mantissa = mantissa >> 1;
exponent++;
if exponent > 1023 {
return nothing{};
}
}
return (mantissa as f64) * decimal_digits::power_of_two(exponent - 52);
}
# Multiplies the number by two to a power, which divides it where the power is
# negative, sixty bits at a time.
def decimal_digits::shift(&mut this, by: i64): void {
if this.count == 0 {
return;
}
var left := by;
while left > 60 {
this.shift_left(60);
left -= 60;
}
while left < -60 {
this.shift_right(60);
left += 60;
}
if left > 0 {
this.shift_left(left as u64);
}
if left < 0 {
this.shift_right((-left) as u64);
}
}
# Multiplies the number by two to a power of at most 60, writing the digits from
# the last into the room past those held, then moving them to the front.
def decimal_digits::shift_left(&mut this, by: u64): void {
var read := this.count;
var write := this.count + decimal_room;
var carry := 0u;
while read > 0 {
read--;
carry += (this.digits[read] as u64) << by;
val quotient := carry / 10;
write--;
this.digits[write] = (carry - quotient * 10) as u8;
carry = quotient;
}
while carry > 0 {
val quotient := carry / 10;
write--;
this.digits[write] = (carry - quotient * 10) as u8;
carry = quotient;
}
val made := this.count + decimal_room - write;
val first := (&mut this.digits) as *mut u8;
memcpy(first, first + write, made);
for k in decimal_capacity..made {
if this.digits[k] != 0 {
this.truncated = true;
}
}
this.point += (made - this.count) as i64;
this.count = made < decimal_capacity ? made : decimal_capacity;
this.trim();
}
# Divides the number by two to a power of at most 60.
def decimal_digits::shift_right(&mut this, by: u64): void {
var read := 0u;
var write := 0u;
var carry := 0u;
# Enough of the first digits to divide, counting zeros past the last as digits.
while (carry >> by) == 0 {
if read >= this.count {
if carry == 0 {
this.count = 0;
return;
}
while (carry >> by) == 0 {
carry = carry * 10;
read++;
}
break;
}
carry = carry * 10 + this.digits[read];
read++;
}
this.point -= (read as i64) - 1;
val mask := (1u << by) - 1;
while read < this.count {
val next := this.digits[read];
this.digits[write] = (carry >> by) as u8;
write++;
carry = (carry & mask) * 10 + next;
read++;
}
while carry > 0 {
val digit := carry >> by;
carry = (carry & mask) * 10;
if write < decimal_capacity {
this.digits[write] = digit as u8;
write++;
} else if digit > 0 {
this.truncated = true;
}
}
this.count = write;
this.trim();
}
# The integer part of the number, rounded by the digits after the point, halfway
# going to an even integer.
def decimal_digits::rounded(&this): u64 {
if this.point > 20 {
return 0xFFFFFFFFFFFFFFFF;
}
var whole := 0u;
var at: i64 = 0;
while at < this.point && at < (this.count as i64) {
whole = whole * 10 + this.digits[at as u64];
at++;
}
while at < this.point {
whole = whole * 10;
at++;
}
if this.rounds_up() {
whole++;
}
return whole;
}
# Whether the digits after the point round the integer part up: past halfway, or
# exactly halfway with an odd integer part, or halfway with digits left out past it.
def decimal_digits::rounds_up(&this): bool {
if this.point < 0 || this.point >= (this.count as i64) {
return false;
}
val at := this.point as u64;
if this.digits[at] == 5 && at + 1 == this.count {
if this.truncated {
return true;
}
return at > 0 && this.digits[at - 1] % 2 == 1;
}
return this.digits[at] >= 5;
}
# Drops the zeros after the last digit that is not one.
def decimal_digits::trim(&mut this): void {
while this.count > 0 && this.digits[this.count - 1] == 0 {
this.count--;
}
if this.count == 0 {
this.point = 0;
}
}
def decimal_digits::shift_for(digits: i64): i64 {
return digits < 9 ? decimal_shifts[digits as u64] as i64 : 27;
}
# Ten to a power from 0 to 22, which a float holds exactly.
def decimal_digits::power_of_ten(power: i64): f64 {
var made: f64 = 1;
for _ in 0..power {
made *= 10;
}
return made;
}
# Two to a power from -1074 to 1023, which a float holds exactly.
def decimal_digits::power_of_two(power: i64): f64 {
var made: f64 = 1;
var left := power;
while left > 0 {
made *= 2;
left--;
}
while left < 0 {
made *= 0.5;
left++;
}
return made;
}