The magic depends on your platform.
One possibility is that your CPU has a special instruction to copy floating point numbers into integral registers.
Of course someone has to design these CPUs, so this is not really an explanation for the algorithm at hand.
A platform might be using a floating point format that goes like this (actually, this is a fixed-point format for the sake of example):
[sIIIIFFFF]
where s
is the sign, the I
s are the part before the dot, the F
s are the part after the dot, e.g. (dot is virtual and only for presentation)
- 47.5000
[sIIII.FFFF]
in this case conversion is almost trivial and can be implemented using bitshifting:
-47.5000
>> 4
---------------
-47
And like in this example, commodity C++ implementations use a floating point representation often referred to as IEEE Floating Point, see also IEEE 754-1985. These are more complicated than fixed-point numbers, as they really designate a simple formula of the form _s*mn, however, they have a well defined interpretation and you can unfold them into something more suitable.