Advice

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.

@user207421 I do want to implement it, however I surely do not know, hence why I asked the question. I want to know how to code the truncation algorithm contained inside the single cpu instruction.

@Igor Tandetnik yes, i'm asking what the underlying algorithm is behind std::trunc, as this function would be used to then round a number. Since std is not open source, would you be able to tell me what goes on behind the scenes there? Thanks in advance.

@n.m. coudl be an AI I did not select "advice" when I opened this question, I selected a label from a drop-down which didn't even say advice, and it was marked as advice. I'm not going to close and re-open the question and you can't edit the type of question directly.

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?

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.

Note that the glibc "Use generic implementation" is not really generic. It presumes IEEE 754 representation, and presumes the memory layout (e.g., could have big-endian int but little-endian IEEE 754 on the same hardware).

@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 x is a NaN or an infinity, then return x + 0.
    • (The use of x + 0 here, instead of x, will generate a floating-point exception if x is a signaling NaN.)
  • If the absolute value of x is less than 1, then return 0, with the sign of x (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.

Your Reply

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Reply", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.