Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.06 KB | None | 0 0
  1. /*  Donald Marovich
  2.     IFT 510 -Troy McDaniels
  3.     Activity 1
  4.     Fall 2019 ASU MSIT Online
  5.     Please do not duplicate or steal without permission. theitguy@asu.edu
  6. */
  7.  
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Text;
  12.  
  13. namespace  FloatingPoint
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {   /////////////////////////////////////////////////////////////////////////////////////
  19.             //Create variables
  20.             List<int> rhsList = new List<int>();
  21.             string userInput1;
  22.  
  23.  
  24.             //User input via Console.ReadLine()
  25.             Console.WriteLine("Hello, what number needs converted to floating point?");
  26.             userInput1 = Console.ReadLine();
  27.             Console.WriteLine("You entered: " + userInput1);
  28.  
  29.  
  30.             //Parses string to integer for left hand side(lhs) and right hand side (rhs)
  31.             //https://stackoverflow.com/questions/8026600/split-double-into-two-int-one-int-before-decimal-point-and-one-after
  32.             string[] beenSplit = userInput1.Split('.');
  33.             int lhs = int.Parse(beenSplit[0]);
  34.             int rhs = int.Parse(beenSplit[1]);
  35.             int lhsPositive = Math.Abs(lhs);
  36.  
  37.  
  38.             //Determine Sign-bit
  39.             double signInput = Convert.ToDouble(userInput1);
  40.             int signBit = 0;
  41.             if (signInput >= 0)
  42.             {
  43.                 signBit = 0;
  44.             }
  45.             else
  46.             {
  47.                 signBit = 1;
  48.             }
  49.             string signBitString = Convert.ToString(signBit);
  50.  
  51.  
  52.             //Test Line
  53.             //Console.WriteLine("LHS is... " + lhs);
  54.             //Console.WriteLine("RHS is... " + rhs);
  55.             //Console.WriteLine("LHS Positive integer is " + lhsPositive);
  56.             //Console.WriteLine("");
  57.             //Console.WriteLine("signBitString = " + signBitString);
  58.  
  59.  
  60.             /////////////////////////////////////////////////////////////////////////////////////
  61.  
  62.  
  63.             //Convert LHS to binary value, first as string, then to int
  64.             string lhsBinaryString = Convert.ToString(lhsPositive, 2);
  65.             ///Console.WriteLine("lhsBinaryString... " + lhsBinaryString);            
  66.  
  67.  
  68.             ////////////////////////////////////////////////////////////////////////////////////////
  69.  
  70.             //work with fractional
  71.             //N is equal to number of characters in RHS
  72.             //main function test = RHS * 2 * (.1^N)
  73.             //test >_ 1 = 1, test < 1 = 0
  74.             //take the test result and add it onto array
  75.             //takes the character string lentgh of the right hand side as
  76.             //an integer and makes it equal to N*/
  77.             int N = rhs.ToString().Length;
  78.            
  79.             //Test Line
  80.             //Console.WriteLine("N is equal to... " + N);
  81.            
  82.             //variable for our while loop
  83.             int j = N;
  84.             string rhsPublicString = "";
  85.  
  86.             //storage for while loop
  87.             double finalTest;
  88.  
  89.             //loop to test for each bit
  90.             while (j > 0)
  91.             {
  92.                 //variable to get the number needed to multply result by
  93.                 double theResult = Math.Pow(.1, N);
  94.                 //Test Line
  95.                 //Console.WriteLine("");
  96.                 //Console.WriteLine("While loop: " + (j-1));
  97.                
  98.                 //math1 - first multiply by 2
  99.                 int test1 = rhs * 2;
  100.                
  101.                 //Test Line
  102.                 //Console.WriteLine("RHS * 2 = " + test1);
  103.  
  104.                
  105.                 //converts to double so we can multiply by .1-.0001 etc to get the correct result
  106.                 double test2 = Convert.ToDouble(test1);
  107.                 //Test Line
  108.                 //Console.WriteLine("....... test2 Double version: " + test2);
  109.                
  110.                 //moving the decimal
  111.                 double test3 = test2 * theResult;
  112.                 //Console.WriteLine("............. test2 * theResult " + test3);
  113.  
  114.                 //rounds the result to the same length we began with
  115.                 test3 = Math.Round(test3, N);
  116.                 finalTest = test3;
  117.  
  118.                 //Test Line
  119.                 //Console.WriteLine("After the math the decimal should have moved... " + finalTest);
  120.                
  121.                     //Test if test3 is = 0/1
  122.                     if (finalTest < 1)
  123.                     {
  124.                         rhsList.Add(0);
  125.                         //Console.WriteLine("Result below 1, adding 0 to list.");
  126.  
  127.                     }
  128.                     else
  129.                     {
  130.                         rhsList.Add(1);
  131.                         //Console.WriteLine("Result equal or above 1, adding 1 to list.");
  132.  
  133.                     }
  134.  
  135.                 //Want to multiply the result by two on the next loop
  136.                 string finalTestString = Convert.ToString(finalTest);
  137.                 string[] ftSplit = finalTestString.Split('.');
  138.                 int c = ftSplit.Length;
  139.                 int c3;
  140.  
  141.                     if (c > 1)
  142.                     {
  143.                         int c2 = int.Parse(ftSplit[1]);
  144.                         c3 = c2;
  145.                     }
  146.                     else
  147.                     {
  148.                         c3 = 0;
  149.                     }
  150.  
  151.                 //not sure why i wrote this probably delete
  152.                 rhs = c3;
  153.  
  154.                 //"downerate" j and N
  155.                 j--;
  156.                 N--;
  157.  
  158.                 //convert the list to an int
  159.                 //rhsINT is our bit form of the RHS!!!
  160.                 //int rhsINT = 0;
  161.                 string rhsBinaryS = "";
  162.                 foreach (int entry in rhsList)
  163.                 {
  164.                     //rhsINT = 10 * rhsINT + entry;
  165.                     rhsBinaryS = rhsBinaryS + entry;
  166.                 }
  167.                 rhsPublicString = Convert.ToString(rhsBinaryS);
  168.  
  169.  
  170.             }
  171.  
  172.  
  173.             //Test Line  
  174.             //Console.WriteLine("");
  175.             //Console.WriteLine("End of While Loop!");
  176.             Console.WriteLine("The LHS bits are: " + lhsBinaryString);
  177.             Console.WriteLine("The RHS bits are: " + rhsPublicString);
  178.             //Console.WriteLine("N is now equal to: " + N);
  179.  
  180.  
  181.             ////////////////////////////////////////////////////////////////
  182.  
  183.             //convert and combine together lhs and rhs into string          
  184.             string fullBinaryString = lhsBinaryString + rhsPublicString;
  185.             int fullBinaryIntLen = fullBinaryString.Length;
  186.             Console.WriteLine("Together, the bits are: " + fullBinaryString);
  187.             Console.WriteLine("");
  188.             //Console.WriteLine("the double of the bits are " + fullBinaryInt);
  189.  
  190.             //length of the bits
  191.             int fullBinaryLen = fullBinaryString.Length;
  192.             //Console.WriteLine("The length of bits is:" + fullBinaryLen);
  193.  
  194.             //Determine the exponent value
  195.             int expValue = 0; //= rhsZeroInt - 1;
  196.             int manInt = 0;
  197.             int rhsZeroInt1 = 0;
  198.  
  199.             //Determine if exponent is postive or negative  
  200.             //if 1 is on lhs, don't move or move to left
  201.             //else 0 on lhs, move to the right
  202.            
  203.             if (lhsPositive > 0)
  204.             {
  205.                 char [] trimChars = {'0'};
  206.                 string lhsZeroString0 = lhsBinaryString.TrimStart(trimChars);
  207.                 //Console.WriteLine("lhsBinaryString = " + lhsBinaryString);
  208.                 //Console.WriteLine("lhsZeroStrin0 ==== " + lhsZeroString0);
  209.  
  210.                 int lenLeftover = lhsZeroString0.Length;
  211.  
  212.                 int lhsZeroInt = lhsBinaryString.Length - lenLeftover;
  213.                 //Console.WriteLine("Length of LHS is " + lhsBinaryString.Length);
  214.                 //Console.WriteLine("LHS Zeros = " + lhsZeroInt);
  215.  
  216.                 expValue = lhsBinaryString.Length - lhsZeroInt - 1;                
  217.                 expValue = System.Math.Abs(expValue);
  218.                 Console.WriteLine("The nomalization value is " + expValue);
  219.  
  220.  
  221.                 //for mantissa
  222.                 int manInt1 = lhsBinaryString.Length - lenLeftover - 1;
  223.                 manInt = Math.Abs(manInt1);
  224.  
  225.  
  226.  
  227.             }
  228.             else
  229.             {
  230.                 //Console.WriteLine("The RHS bits are: " + rhsPublicString);
  231.                 string rhsZeroString = rhsPublicString.TrimStart('0');
  232.                 //Console.WriteLine("The leftovers after taking zeros off: " + rhsZeroString);
  233.  
  234.                 int rhsZeroInt =  rhsZeroString.Length - rhsPublicString.Length - 1;
  235.                 rhsZeroInt1 = Math.Abs(rhsZeroInt);
  236.                 //Console.WriteLine("rhsZeroInt: " + rhsZeroInt);
  237.  
  238.                 expValue = rhsZeroInt;
  239.                 Console.WriteLine("The normalization value is " + expValue);
  240.  
  241.                 //for mantissa
  242.                 int manInt1 = rhsZeroInt + 1;
  243.                 manInt = Math.Abs(manInt1);
  244.  
  245.             }
  246.             ////////////////////////////////////////////////////////////////
  247.  
  248.             //determine exponent bit value
  249.             //exponent value + 127 = exponent value > binary
  250.             int expValue2 = expValue + 127;
  251.             //Console.WriteLine("expValue2 ------ " + expValue2);
  252.  
  253.             string expBinaryString = Convert.ToString(expValue2, 2);
  254.             expBinaryString = expBinaryString.PadLeft(8, '0');
  255.             //Console.WriteLine("expBinaryString ----- " + expBinaryString);
  256.  
  257.  
  258.             ////////////////////////////////////////////////////////////////
  259.  
  260.             //bring it all together
  261.             //signbit + exponent
  262.             string floatingPointBinary1 = "";
  263.             floatingPointBinary1 = signBitString + "|" + expBinaryString;
  264.  
  265.             //Console.WriteLine("Answer before mantissa: " + floatingPointBinary1);
  266.  
  267.             //add mantissa
  268.             string mantissa;
  269.             string fullwithdec = lhsBinaryString + "." + rhsPublicString;
  270.             //Console.WriteLine("String fullwithdec: " + fullwithdec);
  271.  
  272.             int expPos = System.Math.Abs(expValue);
  273.             //Console.WriteLine("expPos = " + expPos);
  274.  
  275.             //if lhs > 0 , length of lhs - zeroes - 1
  276.             //else rhs, rhsZeroes + 1
  277.             //int manInt = expPos - 1;
  278.             //Console.WriteLine("manInt = " + manInt);
  279.  
  280.  
  281.             if (lhsPositive > 0)
  282.             {
  283.                 mantissa = fullBinaryString.Remove(0, manInt);
  284.                 //Console.WriteLine("mantissa = " + mantissa);
  285.             }
  286.             else
  287.             {
  288.                 //int index = rhsPublicString.Length - rhsZeroInt1;
  289.                 int delet = lhsBinaryString.Length + manInt + 1;
  290.                 mantissa = fullBinaryString.Remove(0, delet);
  291.             }
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.             floatingPointBinary1 = floatingPointBinary1 + "|" + mantissa;
  300.  
  301.             //pad 0's
  302.             //Used to pad or ensure certain number of bits ie, range extension
  303.             //need to count number of characters in string and pad to make 8 bits
  304.             //https://docs.microsoft.com/en-us/dotnet/api/system.string.padleft?view=netframework-4.8
  305.             //Console.WriteLine("before padding right: " + floatingPointBinary1);
  306.            
  307.             string floatingpointBinary2 = floatingPointBinary1.PadRight(34, '0');
  308.  
  309.  
  310.  
  311.             //Return answers
  312.             Console.WriteLine("Sign bit: " + signBit);
  313.             Console.WriteLine("Biased Exponent is: " + expValue);
  314.             Console.WriteLine("Biased Exponent in binary: " + expBinaryString);
  315.             Console.WriteLine("The Significand is: " + mantissa);
  316.             Console.WriteLine("Our Full Answer with signbit, biased exponent, and significant:");
  317.             Console.WriteLine(floatingpointBinary2);
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.             ////////////////////////////////////////////////////////////////
  325.             /////////////////////////EXTRA CODE/////////////////////////////
  326.             ////////////////////////////////////////////////////////////////
  327.            
  328.             /*
  329.  
  330.             */
  331.  
  332.  
  333.         }
  334.     }
  335. }
  336. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement