コード例 #1
0
        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);
            }
        }
コード例 #2
0
        /// <summary>
        /// It converts a single precision number into a struct with properties
        /// signBit exponent mantissa power of the exponent.
        /// </summary>
        /// <param name="data">
        /// Input a number of type Float and get its binary representation.
        /// </param>
        /// <example> This sample shows how to call the SaveData
        /// method from a wireless device.
        /// <returns>IEEE754 object</returns>
        /// <see cref="http://class.ece.iastate.edu/arun/Cpre305/ieee754/ie4.html"/>
        public IEEE754 FloatToBinary(float number)
        {
            bool    signBit  = false;
            int     i        = 0;
            float   mantissa = 0;
            IEEE754 result   = new IEEE754();
            MathematicalConversion mathObj = new MathematicalConversion();

            if (number.ToString().Contains('-'))
            {
                number *= -1;
                signBit = true;
            }

            i        = GetExponent(number);
            mantissa = number / (float)Math.Pow(2, i);
            int    exponent         = 127 + i;
            string binaryConversion = mathObj.DecimalToBinary(exponent);
            int    binaryExponent   = Convert.ToInt32(binaryConversion);

            mantissa = mantissa - (int)mantissa;
            StringBuilder mantissaBinary = new StringBuilder(mathObj.Mantissa(mantissa));

            mantissaBinary.Insert(0, '1');

            result.SignBit  = (byte)((signBit) ? 1 : 0);
            result.Exponent = binaryExponent;
            result.Mantissa = mantissaBinary.ToString();
            result.Power    = i;
            return(result);
        }