decimal icon indicating copy to clipboard operation
decimal copied to clipboard

guidance on writing f64, f32 conversion

Open jonathanstrong opened this issue 8 years ago • 2 comments

Hey - thanks for your work on this library. I'm using it for a project and interested in writing Into and possibly From for rust's primitive floats. However, my background is Python (and Decimal), not decNumber or C/C++ and I'm fairly new to Rust. Do you have any guidance on where to start? Is it simply a matter of bridging this library to functions that are already part of decNumber?

jonathanstrong avatar Jun 06 '17 14:06 jonathanstrong

Hi @jonathanstrong, I'm interested in the same (with pretty much the same background as you)

Did you make any notable progress on this issue?

pashadia avatar Jan 31 '18 09:01 pashadia

I've ended up spending a lot of time on it, and it's a huge pain in the ass. I'm not any closer to a performant function to convert back and forth between f64 or f32. It's very difficult to find good info on the memory layout of various integer and floating point formats. Trying to grok the decNumber code is nightmarish compared to Rust (I have never worked in C, and limited exposure to C++). It's utterly baffling the decNumber library includes conversion functions for 32-bit integers but not 64-bit, when 32-bit are basically worthless since max value of an u32 is like 4 billion and change. And nothing for f32/f64 (that I have found). Look in the code for a From<u64> impl that fills a [u8; 34] array and uses it to call a "BCD" function (that stands for Binary Decimal Format). I was able to reverse engineer that for a From<d128> for u64 impl. With u64, depending on how much precision you need, you can scale the number by a fixed factor and use integer math until converting back to d128 at the end (which is very fast). FYI d128 is pretty slow compared to primitives.

jonathanstrong avatar Feb 01 '18 01:02 jonathanstrong