I'm wondering how the truncate function works. C++ is just an example i'm using because it's a language I know. I've searched online and I can't seem to find how it works, simply how to use it and what it does. here for example only shows w3schools and geeks4geeks pages on the input and output of the function, remarks, etc.. I'm wondering about what the underlying algorithm/process is. Thanks in advance for any help.
13 Replies 13
I'm guessing you mean std::trunc. If you want to know how your particular C++ implementation implements it, you can examine the machine code or, for open-source implementations, the source code.
For example, GCC appears to generate a bunch of SIMD instuctions (which I must admit I don't understand; feel free to try and figure it out).
Do you mean std::trunc?
Note, don't use geeks4geeks is it a low quality site with lots of bad examples.
It's a question, not an "advice". Please repost it as a question.
What exactly do you mean by 'algorithm'? It's a single CPU instruction. The requirements are set out in the C++ Standard. That's all you need to know, unless you are implementing it, in which case you would surely already know the answer.
So tell us the platform you are implementing for. But surely it has IEEE754 floating-point? and therefore CPU instructions that do exactly what you want? or rather what the C++ standard wants?
Here's glibc's implementation: https://github.com/bminor/glibc/blob/master/sysdeps/ieee754/dbl-64/s_trunc.c
It'll be implementation-dependent and hardware-dependent so you'd need to specify which library and where. If you want to know just how you could implement it as an exercise, "identify which bits correspond to the fractional part and zero them out" would be the natural approach considering the usual binary representation of floating point numbers. Caveats being special values like NaN.
@nugget "Since std is not open source" - what? The C++ standard (which is what specifies what std::trunc and everything else should do) is publicly available and several implementations (by GNU, LLVM and others) are available as Open Source. Sure, there are also commercial implementations, but even Microsoft's implementation is freely available. So, what are you on about? What you are saying here makes no sense.
If a processor does not provide a truncate instruction, or we wish to implement truncate in software for some other reason, then the algorithm would typically be something like:
- If the value
xis a NaN or an infinity, then returnx + 0.- (The use of
x + 0here, instead ofx, will generate a floating-point exception ifxis a signaling NaN.)
- (The use of
- If the absolute value of
xis less than 1, then return 0, with the sign ofx(copysign(0, x)).- (This handles cases where the entire significand is eliminated by the truncation, simplifying the remaining work.)
- Otherwise, get the bits that encode the value of
x. - Use the bits in the exponent field to determine how many bits of the significand field are fraction bits. Clear those bits.
- Return the value represented by the updated bits.