Page 1 of 1

Overview: Today we practice floating point representation today. Develop and run your code on your preferred C++ environ

Posted: Tue Jul 05, 2022 10:26 am
by answerhappygod
Overview Today We Practice Floating Point Representation Today Develop And Run Your Code On Your Preferred C Environ 1
Overview Today We Practice Floating Point Representation Today Develop And Run Your Code On Your Preferred C Environ 1 (68.8 KiB) Viewed 11 times
Overview Today We Practice Floating Point Representation Today Develop And Run Your Code On Your Preferred C Environ 2
Overview Today We Practice Floating Point Representation Today Develop And Run Your Code On Your Preferred C Environ 2 (46.5 KiB) Viewed 11 times
Overview Today We Practice Floating Point Representation Today Develop And Run Your Code On Your Preferred C Environ 3
Overview Today We Practice Floating Point Representation Today Develop And Run Your Code On Your Preferred C Environ 3 (58.75 KiB) Viewed 11 times
Overview: Today we practice floating point representation today. Develop and run your code on your preferred C++ environment, namely cpp.sh. In this practice, we want to convert a 32-bit integer to IEEE 754 32-bit floating point representation. The goal is to obtain sign bit, exponent, and fraction from an integer number. Finding sign bit is very trivial, sign bit is 1 if number is negative, and 0 otherwise. Below I explain hints on how to find exponent and fraction. To find the exponent and fraction, we should convert an integer number into normalized scientific format. For example: 1101 should be converted into 1.101 x 2³ In general, we need to convert the number into 1.F x 2 representation. But how do we find the normalized representations? Given a 32-bit normalized number, for example: 0000 0000 0000 0000 0000 0010 1000 1000 the key is to find the first none-zero bit from the left. This bit is the none-zero bit of the normalized representation. For example, in the number above, bit at significance 2^9 is the none-zero bit. How you find this bit? I leave it up to you to find the best strategy, but when you find this bit, you have the normalized representation. How? In the normalized representation formula: 1.Fx 2 Where E is the significance of the number you found, 9 in our example. And F is all the bits on the right side of it, 010001000 in our example. So the normalized representation of the number above is 1.010001000 x 2° Now that you have the normalized representation, you can extract exponent and fraction: fraction = F 00010001000 exponent = 127 + E= 127+9 = 136
Practice 1: Develop a C++ program to convert an integer to 32-bit IEEE-754 floating point representation. Print sign, exponent, and fraction and the end of your program. Verify your code by converting integer number 98765 to IEEE-754 and confirm your conversion is correct by verifying on the IEEE754 online calculator: https://www.h-schmidt.net/Float Converter/IEEE754.html What to submit on Moodle? Submit your C++ code on Moodle. Hints One algorithm is to convert decimal to string stream and then normalize and find exponent and fraction. Useful tips: To print a number in binary for debugging, use bitset. Example: 1. #include 2. #include 3. 4. int main() { 5. 9. int a 54; std::cout << std::bitset<32>(a) << std::endl; return 0;
1. #include 2. #include 3. 4. int main() { 5 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40 41. 42. 43. 44. 45. 46. 47. 48. 49. 50 binary operations that are useful here: original int a 54; std::cout << std::bitset<64>(a) << "< original" << std::endl; //shift left and insert 1 in least significant bit int b (a << 1) | 0x00000001; std::cout << std::bitset<64>(b) << "< shift-left insert 1" << std::endl; // shift left and insert 0 in Teast significant bit int g (a << 1) & 0xfffffffe; std::cout << std::bitset<64>(g) << "< shift-left insert 0" << std::endl; // load into upper 64-bit long long intca; CC << 32: std::cout<(c) <<< insert into upper 32-bit" << std::endl; // test least-significant biz (LSB) int d = 5; std::cout << std::bitset<32>(d) << " < original value" << std::endl; if ((d & 0x00000001)-1) std::cout << "1sb of number above is one\n"; } else ( } std::cout << "1sb of number above is zero\n": //shift right int e = d; std::cout << std::bitset<32>(e) <<< original number" << std::endl; ee >> 1; // shift to the right } else { } std::cout << std::bitset<32>(e) <<< shift right" << std::endl; if ((e & 0x00000001) 1) { std::cout << "1sb of number above is one\n": std::cout << "1sb of number above is zero\n": return 0;