static void Main(string[] args) { try { // Input two numbers. var firstNumber = float.Parse(Console.ReadLine()); var secondNumber = float.Parse(Console.ReadLine()); // Instance for FloatToIEEE class. FloatToIEEE floatObj = new FloatToIEEE(); // Instance for performing all the mathematical conversions. MathematicalConversion mathInstance = new MathematicalConversion(); // Instance for adding the two numbers. Addition additionObj = new Addition(); // String storing the sum of two numbers. string sumOfNumbers = additionObj.Add(floatObj.NormalizeTwoNumbers(firstNumber, secondNumber)); // Converting the result back to binary. float result = mathInstance.BinaryToFloat(sumOfNumbers); // Result of the two numbers is displayed. Console.WriteLine(result); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Normalizes two numbers and returns the numbers in string array. /// </summary> /// <param name="firstNumber"></param> /// <param name="secondNumber"></param> /// <returns></returns> public string[] NormalizeTwoNumbers(float firstNumber, float secondNumber) { // Instance for FloatToIEEE class. FloatToIEEE floatObj = new FloatToIEEE(); // Binary representation of the numbers. var binaryFirstNumber = floatObj.FloatToBinary(firstNumber); var binarySecondNumber = floatObj.FloatToBinary(secondNumber); // Normalized Numbers to common exponent (i.e 2^0) here. string[] normalizedNumbers = new string[2]; normalizedNumbers[0] = floatObj.Normalize(binaryFirstNumber.Power, binaryFirstNumber.Mantissa); normalizedNumbers[1] = floatObj.Normalize(binarySecondNumber.Power, binarySecondNumber.Mantissa); return(normalizedNumbers); }