numtoa
numtoa copied to clipboard
Support for floating point
Can the same triple and quad digit lookup table technique be applied to f32 and f64? I would love to have something faster than dtoa.
It should be able to work perfectly with floating point numbers too, but I don't know anything about how floating points are decoded. You would just need to get an integer representation of the whole number and the fractional number.
There are some macros in the dtoa crate to get you started.
#![allow(dead_code)]
#[macro_use] mod diyfp;
use std::{mem, ops};
diyfp! {
floating_type: f64,
significand_type: u64,
exponent_type: isize,
diy_significand_size: 64,
significand_size: 52,
exponent_bias: 0x3FF,
mask_type: u64,
exponent_mask: 0x7FF0000000000000,
significand_mask: 0x000FFFFFFFFFFFFF,
hidden_bit: 0x0010000000000000,
cached_powers_f: [0; 0],
cached_powers_e: [0; 0],
min_power: (-348),
}
fn main() {
let x = 2.7182818284f64;
let fp = unsafe { diyfp::DiyFp::from(x) };
println!("{:?}", fp);
}
The output is DiyFp { f: 6121026514735115, e: -51 }. That means the original number is f * 2^e.
$ python -c 'print 6121026514735115 * 2**-51'
2.7182818284