Input/Output Format Assume we have sign bit, exponent, and fraction individually for every input number. For example, 54
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Input/Output Format Assume we have sign bit, exponent, and fraction individually for every input number. For example, 54
Step 2: compare exponents Number A: Exponent 129, Fraction: 0.11010000000000000000000 Number B: Exponent 130, Fraction: 1.01110000000000000000000 B is larger, so we should shift A by 1 bit and compare: Step 3: compare exponents Number A: Exponent 130, Fraction: 0.01101000000000000000000 Number B: Exponent 130, Fraction: 1.01110000000000000000000 Exponents are now equal, lets proceed. 2. Add fractions and store result in a new 25-character string. This is a very subtle process and must be done carefully. You could have various strategies for this step, e.g. write a routine to add fractions in string format or convert strings to integer and use add operator. I leave this step to you to use your creativity and find the best approach. Notice that resulting fraction must include binary point character as well. Example: 0.01101000000000000000000 1.01110000000000000000000 1.11011000000000000000000 Result Exponent is the same as B (larger number): 130 Number A Fraction: Number 8 Fraction: Result Fraction: 3. Verify if the number is normalized. If yes, go to the next step, otherwise, shift right and increment exponent or shift left and decrement exponent. In our example, number is already normalized: 1.11011000000000000000000 Result Fraction: Result Exponent: 130 4. Print results fraction, exponent, and sign bit on the screen. Note: Simplify the problem by assuming numbers are positive only.
Practice 1 Develop a C++ application to implement floating point addition algorithm explained above. Use the example above for verifying the correctness of your solution. Use following skeleton to start off your code: #include <iostream> #include <string> int main() 1 // first input: 3.25 int A sign 0: int A exponent = 128; std::string A fraction="1.10100000000000000000000"; std::cout << "1st input:" "sign: " << A sign << exponent: " << A exponent << " fraction " << A fraction << std::endl; // second input: 11.5 int B sign 0: int 8 exponent = 130; std::string B fraction="1.01110000000000000000000"; std::cout << "2nd input:" sign: " << B sign << " exponent: " << B_exponent << "fraction " << B fraction << std::endl; // implement addition algorithm here // Print sign, exponent, and fraction of final result. return 0; Verify your implementation is correct by comparing against expected result below: Sign: 0. • Exponent: 10000010 • Fraction: 1.1101100000000000000000 What to submit on Moodle? Submit your C++ code. Practice 2 Verify your code with negative numbers and get it working for following example: Number A: Sign: 0. Exponent 131, Fraction: 1.011100000000000 Number B: Sign: 1, Exponent 130, Fraction: 1.10000000000000000000000 The expected result is: Result: Sign: 0, Exponent 130, Fraction: 1.01100000000000000000000 What to submit on Moodle? Submit your C++ code.