The magic depends on your platform.
One of the possibilities is that your processor has special instructions for copying floating-point numbers into integral registers.
Of course, someone should design these processors, so this is actually not an explanation of the algorithm.
The platform can use a floating point format that is similar to this (actually this is a fixed-point format for example):
[sIIIIFFFF]
where s is the sign, I is the part before the point, F is the part after the point, for example. (the point is virtual and for presentation only)
- 47.5000 [sIIII.FFFF]
in this case, the conversion is almost trivial and can be implemented using the bitrate:
-47.5000 >> 4 --------------- -47
And, as in this example, the C ++ implementation on the market uses a floating point view, which is often called the IEEE floating point , see also IEEE 754-1985 . They are more complex than fixed-point numbers, because they do mean a simple formula of the form _s * m n however they have a well-defined interpretation, and you can expand them into something more suitable.
Sebastian mach
source share