use alloc::string::String;
use core::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
};
use super::{cast::is_pg_space, interval::strtod};
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)]
pub enum Float8Error {
#[error("invalid input syntax for type double precision")]
InvalidInputSyntax,
#[error("\"{0}\" is out of range for type double precision")]
OutOfRange(String),
}
#[derive(Debug, Clone, Copy)]
pub struct PgFloat8(f64);
impl PgFloat8 {
pub fn parse(text: &str) -> Result<Self, Float8Error> {
let bytes = text.as_bytes();
let start = bytes.iter().take_while(|&&byte| is_pg_space(byte)).count();
let sign = usize::from(bytes.get(start) == Some(&b'+'));
if sign == 1 && bytes.get(start + 1) == Some(&b'-') {
return Err(Float8Error::InvalidInputSyntax);
}
let (value, end, out_of_range) =
strtod(bytes, start + sign).ok_or(Float8Error::InvalidInputSyntax)?;
if out_of_range && (value == 0.0 || value.is_infinite()) {
return Err(Float8Error::OutOfRange(String::from(&text[start..end])));
}
if !bytes[end..].iter().all(|&byte| is_pg_space(byte)) {
return Err(Float8Error::InvalidInputSyntax);
}
Ok(Self(value))
}
#[must_use]
pub const fn value(self) -> f64 {
self.0
}
}
impl From<f64> for PgFloat8 {
fn from(value: f64) -> Self {
Self(value)
}
}
impl fmt::Display for PgFloat8 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let value = self.0;
if value.is_nan() {
return f.write_str("NaN");
}
if value.is_sign_negative() {
f.write_str("-")?;
}
if value.is_infinite() {
return f.write_str("Infinity");
}
if value == 0.0 {
return f.write_str("0");
}
let (digits, last_power) = shortest(value.abs());
let mut text = StackText::default();
fmt::Write::write_fmt(&mut text, format_args!("{digits}"))?;
let length = i32::try_from(text.length).expect("a u64 has at most 20 digits");
write_decimal(f, text.as_str(), last_power + length - 1)
}
}
fn shortest(value: f64) -> (u64, i32) {
let fewest = decimal(value, None).0.ilog10() + 1;
let found = (fewest..=17).find_map(|count| {
let nearest = decimal(value, Some(count));
[nearest, next(nearest, count), previous(nearest, count)]
.into_iter()
.find(|&candidate| strictly_between_midpoints(value, candidate))
});
found.expect("17 digits always fit strictly between the midpoints")
}
fn decimal(value: f64, count: Option<u32>) -> (u64, i32) {
let mut text = StackText::default();
let written = match count {
Some(count) => {
let fraction = usize::try_from(count - 1).expect("at most 16 fraction digits");
fmt::Write::write_fmt(&mut text, format_args!("{value:.fraction$e}"))
}
None => fmt::Write::write_fmt(&mut text, format_args!("{value:e}")),
};
written.expect("a double's exponential form fits 32 bytes");
let (significand, exponent) = text
.as_str()
.split_once('e')
.expect("an exponent is written");
let exponent: i32 = exponent.parse().expect("the exponent is an integer");
let (mut digits, mut fraction_digits) = (0_u64, 0);
for (index, digit) in significand.bytes().filter(u8::is_ascii_digit).enumerate() {
digits = digits * 10 + u64::from(digit - b'0');
fraction_digits = index;
}
let fraction_digits = i32::try_from(fraction_digits).expect("at most 16 fraction digits");
(digits, exponent - fraction_digits)
}
fn next((digits, last_power): (u64, i32), count: u32) -> (u64, i32) {
if digits + 1 == 10_u64.pow(count) {
(10_u64.pow(count - 1), last_power + 1)
} else {
(digits + 1, last_power)
}
}
fn previous((digits, last_power): (u64, i32), count: u32) -> (u64, i32) {
if digits == 10_u64.pow(count - 1) {
(10_u64.pow(count) - 1, last_power - 1)
} else {
(digits - 1, last_power)
}
}
fn strictly_between_midpoints(value: f64, (digits, last_power): (u64, i32)) -> bool {
let mut text = StackText::default();
fmt::Write::write_fmt(&mut text, format_args!("{digits}e{last_power}"))
.expect("a decimal of at most 17 digits fits 32 bytes");
let reads_back = text.as_str().parse::<f64>() == Ok(value);
let bits = value.to_bits();
let (fraction, biased) = (bits & ((1 << 52) - 1), (bits >> 52) & 0x7ff);
let (significand, power) = if biased == 0 {
(fraction, -1074)
} else {
let biased = i32::try_from(biased).expect("an 11-bit exponent");
(fraction | 1 << 52, biased - 1075)
};
let above = (2 * significand + 1, power - 1);
let below = if fraction == 0 && biased > 1 {
(4 * significand - 1, power - 2)
} else {
(2 * significand - 1, power - 1)
};
reads_back
&& !equals_dyadic((digits, last_power), above)
&& !equals_dyadic((digits, last_power), below)
}
fn equals_dyadic((digits, last_power): (u64, i32), (odd, power): (u64, i32)) -> bool {
let (digits, twos, digit_fives) = split_two_five(digits);
let (odd, _, odd_fives) = split_two_five(odd);
digits == odd && twos + last_power == power && digit_fives + last_power == odd_fives
}
fn split_two_five(mut value: u64) -> (u64, i32, i32) {
let (mut twos, mut fives) = (0, 0);
while value.is_multiple_of(2) {
value /= 2;
twos += 1;
}
while value.is_multiple_of(5) {
value /= 5;
fives += 1;
}
(value, twos, fives)
}
#[derive(Default)]
struct StackText {
bytes: [u8; 32],
length: usize,
}
impl StackText {
fn as_str(&self) -> &str {
core::str::from_utf8(&self.bytes[..self.length]).expect("only ASCII is written")
}
}
impl fmt::Write for StackText {
fn write_str(&mut self, text: &str) -> fmt::Result {
let end = self.length + text.len();
let slot = self.bytes.get_mut(self.length..end).ok_or(fmt::Error)?;
slot.copy_from_slice(text.as_bytes());
self.length = end;
Ok(())
}
}
fn write_decimal(f: &mut fmt::Formatter<'_>, digits: &str, exponent: i32) -> fmt::Result {
let length = i32::try_from(digits.len()).expect("a double has at most 17 digits");
if !(-4..15).contains(&exponent) {
let (first, rest) = digits.split_at(1);
f.write_str(first)?;
if !rest.is_empty() {
write!(f, ".{rest}")?;
}
let sign = if exponent < 0 { '-' } else { '+' };
return write!(f, "e{sign}{:02}", exponent.unsigned_abs());
}
if exponent < 0 {
f.write_str("0.")?;
for _ in 0..(-exponent - 1) {
f.write_str("0")?;
}
return f.write_str(digits);
}
if exponent >= length - 1 {
f.write_str(digits)?;
for _ in 0..(exponent - length + 1) {
f.write_str("0")?;
}
return Ok(());
}
let point = usize::try_from(exponent + 1).expect("the exponent is nonnegative here");
let (whole, fraction) = digits.split_at(point);
write!(f, "{whole}.{fraction}")
}
impl PartialEq for PgFloat8 {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for PgFloat8 {}
impl PartialOrd for PgFloat8 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PgFloat8 {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
(false, false) => self
.0
.partial_cmp(&other.0)
.expect("two doubles that are not NaN are ordered"),
}
}
}
impl Hash for PgFloat8 {
fn hash<H: Hasher>(&self, state: &mut H) {
let canonical = if self.0.is_nan() {
f64::NAN
} else if self.0 == 0.0 {
0.0
} else {
self.0
};
canonical.to_bits().hash(state);
}
}