/* * @topic T00030 Feb 14, 2013 -- raw storage of a float data type * @brief Coercing float into an int and back into float using memcpy library function */ #include <iostream> #include <cstring> // defines memcpy() library function int main() { double price = 80.5; // 64 bit (8 bytes) -- use double for most problems float angle = 80.5; // 32 bit (4 bytes) -- use with many graphics laibraries int dummy; // 32 bit (4 bytes) memcpy( &dummy, &angle, sizeof( int ) ); // copy memory bit-by-bit from float to int std::cout << dummy << "\n"; // show raw memory that stores a float value of 80.5 float angle_copy; memcpy( &angle_copy, &dummy, sizeof( float ) ); // copy memory bit-by-bit from int to float std::cout << angle_copy << "\n"; // show that the value of 80.5 hasn't // changed after two memcpy() calls return 0; }