Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Donald Marovich
- IFT 510 -Troy McDaniels
- Activity 1
- Fall 2019 ASU MSIT Online
- Please do not duplicate or steal without permission. theitguy@asu.edu
- */
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- namespace FloatingPoint
- {
- class Program
- {
- static void Main(string[] args)
- { /////////////////////////////////////////////////////////////////////////////////////
- //Create variables
- List<int> rhsList = new List<int>();
- string userInput1;
- //User input via Console.ReadLine()
- Console.WriteLine("Hello, what number needs converted to floating point?");
- userInput1 = Console.ReadLine();
- Console.WriteLine("You entered: " + userInput1);
- //Parses string to integer for left hand side(lhs) and right hand side (rhs)
- //https://stackoverflow.com/questions/8026600/split-double-into-two-int-one-int-before-decimal-point-and-one-after
- string[] beenSplit = userInput1.Split('.');
- int lhs = int.Parse(beenSplit[0]);
- int rhs = int.Parse(beenSplit[1]);
- int lhsPositive = Math.Abs(lhs);
- //Determine Sign-bit
- double signInput = Convert.ToDouble(userInput1);
- int signBit = 0;
- if (signInput >= 0)
- {
- signBit = 0;
- }
- else
- {
- signBit = 1;
- }
- string signBitString = Convert.ToString(signBit);
- //Test Line
- //Console.WriteLine("LHS is... " + lhs);
- //Console.WriteLine("RHS is... " + rhs);
- //Console.WriteLine("LHS Positive integer is " + lhsPositive);
- //Console.WriteLine("");
- //Console.WriteLine("signBitString = " + signBitString);
- /////////////////////////////////////////////////////////////////////////////////////
- //Convert LHS to binary value, first as string, then to int
- string lhsBinaryString = Convert.ToString(lhsPositive, 2);
- ///Console.WriteLine("lhsBinaryString... " + lhsBinaryString);
- ////////////////////////////////////////////////////////////////////////////////////////
- //work with fractional
- //N is equal to number of characters in RHS
- //main function test = RHS * 2 * (.1^N)
- //test >_ 1 = 1, test < 1 = 0
- //take the test result and add it onto array
- //takes the character string lentgh of the right hand side as
- //an integer and makes it equal to N*/
- int N = rhs.ToString().Length;
- //Test Line
- //Console.WriteLine("N is equal to... " + N);
- //variable for our while loop
- int j = N;
- string rhsPublicString = "";
- //storage for while loop
- double finalTest;
- //loop to test for each bit
- while (j > 0)
- {
- //variable to get the number needed to multply result by
- double theResult = Math.Pow(.1, N);
- //Test Line
- //Console.WriteLine("");
- //Console.WriteLine("While loop: " + (j-1));
- //math1 - first multiply by 2
- int test1 = rhs * 2;
- //Test Line
- //Console.WriteLine("RHS * 2 = " + test1);
- //converts to double so we can multiply by .1-.0001 etc to get the correct result
- double test2 = Convert.ToDouble(test1);
- //Test Line
- //Console.WriteLine("....... test2 Double version: " + test2);
- //moving the decimal
- double test3 = test2 * theResult;
- //Console.WriteLine("............. test2 * theResult " + test3);
- //rounds the result to the same length we began with
- test3 = Math.Round(test3, N);
- finalTest = test3;
- //Test Line
- //Console.WriteLine("After the math the decimal should have moved... " + finalTest);
- //Test if test3 is = 0/1
- if (finalTest < 1)
- {
- rhsList.Add(0);
- //Console.WriteLine("Result below 1, adding 0 to list.");
- }
- else
- {
- rhsList.Add(1);
- //Console.WriteLine("Result equal or above 1, adding 1 to list.");
- }
- //Want to multiply the result by two on the next loop
- string finalTestString = Convert.ToString(finalTest);
- string[] ftSplit = finalTestString.Split('.');
- int c = ftSplit.Length;
- int c3;
- if (c > 1)
- {
- int c2 = int.Parse(ftSplit[1]);
- c3 = c2;
- }
- else
- {
- c3 = 0;
- }
- //not sure why i wrote this probably delete
- rhs = c3;
- //"downerate" j and N
- j--;
- N--;
- //convert the list to an int
- //rhsINT is our bit form of the RHS!!!
- //int rhsINT = 0;
- string rhsBinaryS = "";
- foreach (int entry in rhsList)
- {
- //rhsINT = 10 * rhsINT + entry;
- rhsBinaryS = rhsBinaryS + entry;
- }
- rhsPublicString = Convert.ToString(rhsBinaryS);
- }
- //Test Line
- //Console.WriteLine("");
- //Console.WriteLine("End of While Loop!");
- Console.WriteLine("The LHS bits are: " + lhsBinaryString);
- Console.WriteLine("The RHS bits are: " + rhsPublicString);
- //Console.WriteLine("N is now equal to: " + N);
- ////////////////////////////////////////////////////////////////
- //convert and combine together lhs and rhs into string
- string fullBinaryString = lhsBinaryString + rhsPublicString;
- int fullBinaryIntLen = fullBinaryString.Length;
- Console.WriteLine("Together, the bits are: " + fullBinaryString);
- Console.WriteLine("");
- //Console.WriteLine("the double of the bits are " + fullBinaryInt);
- //length of the bits
- int fullBinaryLen = fullBinaryString.Length;
- //Console.WriteLine("The length of bits is:" + fullBinaryLen);
- //Determine the exponent value
- int expValue = 0; //= rhsZeroInt - 1;
- int manInt = 0;
- int rhsZeroInt1 = 0;
- //Determine if exponent is postive or negative
- //if 1 is on lhs, don't move or move to left
- //else 0 on lhs, move to the right
- if (lhsPositive > 0)
- {
- char [] trimChars = {'0'};
- string lhsZeroString0 = lhsBinaryString.TrimStart(trimChars);
- //Console.WriteLine("lhsBinaryString = " + lhsBinaryString);
- //Console.WriteLine("lhsZeroStrin0 ==== " + lhsZeroString0);
- int lenLeftover = lhsZeroString0.Length;
- int lhsZeroInt = lhsBinaryString.Length - lenLeftover;
- //Console.WriteLine("Length of LHS is " + lhsBinaryString.Length);
- //Console.WriteLine("LHS Zeros = " + lhsZeroInt);
- expValue = lhsBinaryString.Length - lhsZeroInt - 1;
- expValue = System.Math.Abs(expValue);
- Console.WriteLine("The nomalization value is " + expValue);
- //for mantissa
- int manInt1 = lhsBinaryString.Length - lenLeftover - 1;
- manInt = Math.Abs(manInt1);
- }
- else
- {
- //Console.WriteLine("The RHS bits are: " + rhsPublicString);
- string rhsZeroString = rhsPublicString.TrimStart('0');
- //Console.WriteLine("The leftovers after taking zeros off: " + rhsZeroString);
- int rhsZeroInt = rhsZeroString.Length - rhsPublicString.Length - 1;
- rhsZeroInt1 = Math.Abs(rhsZeroInt);
- //Console.WriteLine("rhsZeroInt: " + rhsZeroInt);
- expValue = rhsZeroInt;
- Console.WriteLine("The normalization value is " + expValue);
- //for mantissa
- int manInt1 = rhsZeroInt + 1;
- manInt = Math.Abs(manInt1);
- }
- ////////////////////////////////////////////////////////////////
- //determine exponent bit value
- //exponent value + 127 = exponent value > binary
- int expValue2 = expValue + 127;
- //Console.WriteLine("expValue2 ------ " + expValue2);
- string expBinaryString = Convert.ToString(expValue2, 2);
- expBinaryString = expBinaryString.PadLeft(8, '0');
- //Console.WriteLine("expBinaryString ----- " + expBinaryString);
- ////////////////////////////////////////////////////////////////
- //bring it all together
- //signbit + exponent
- string floatingPointBinary1 = "";
- floatingPointBinary1 = signBitString + "|" + expBinaryString;
- //Console.WriteLine("Answer before mantissa: " + floatingPointBinary1);
- //add mantissa
- string mantissa;
- string fullwithdec = lhsBinaryString + "." + rhsPublicString;
- //Console.WriteLine("String fullwithdec: " + fullwithdec);
- int expPos = System.Math.Abs(expValue);
- //Console.WriteLine("expPos = " + expPos);
- //if lhs > 0 , length of lhs - zeroes - 1
- //else rhs, rhsZeroes + 1
- //int manInt = expPos - 1;
- //Console.WriteLine("manInt = " + manInt);
- if (lhsPositive > 0)
- {
- mantissa = fullBinaryString.Remove(0, manInt);
- //Console.WriteLine("mantissa = " + mantissa);
- }
- else
- {
- //int index = rhsPublicString.Length - rhsZeroInt1;
- int delet = lhsBinaryString.Length + manInt + 1;
- mantissa = fullBinaryString.Remove(0, delet);
- }
- floatingPointBinary1 = floatingPointBinary1 + "|" + mantissa;
- //pad 0's
- //Used to pad or ensure certain number of bits ie, range extension
- //need to count number of characters in string and pad to make 8 bits
- //https://docs.microsoft.com/en-us/dotnet/api/system.string.padleft?view=netframework-4.8
- //Console.WriteLine("before padding right: " + floatingPointBinary1);
- string floatingpointBinary2 = floatingPointBinary1.PadRight(34, '0');
- //Return answers
- Console.WriteLine("Sign bit: " + signBit);
- Console.WriteLine("Biased Exponent is: " + expValue);
- Console.WriteLine("Biased Exponent in binary: " + expBinaryString);
- Console.WriteLine("The Significand is: " + mantissa);
- Console.WriteLine("Our Full Answer with signbit, biased exponent, and significant:");
- Console.WriteLine(floatingpointBinary2);
- ////////////////////////////////////////////////////////////////
- /////////////////////////EXTRA CODE/////////////////////////////
- ////////////////////////////////////////////////////////////////
- /*
- */
- }
- }
- }
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement