////////////////////////////////////////////////////////////////////////////////
//                        Coypright (C) Aviral Mittal.
////////////////////////////////////////////////////////////////////////////////
//   All rights reserved. Reproducion in whole or in part is prohibited without 
//   written consent of copyright Owner.The professional use of this material 
//   is subject to the copy right owners approval in written. 
////////////////////////////////////////////////////////////////////////////////
//                   Comments welcome on avimit@yahoo.com
//                   Visit http://www.vlsiip.com for more free stuff
////////////////////////////////////////////////////////////////////////////////
// This program converts a binary number into fractional number
// The input given to this program will be a binary number like 11100101
// Output will be 0.23456
// Example: ./bin2point 10101010
// Above is an example, where the user is trying to convert 0.45823 into binary
// ./bin2point 10101010
//  0.664062
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
//Prototype Declaration
long int  po(short int x,short int y);
//Prototype Declaration Ends

//Variables Declaration
char error = 0; //Error Flag
float frac_out ;
float poof2;
char i;
//Variables Declaration Ends
int main(int argc, char* argv[]) {
//Initializations
  frac_out = 0.0;
  poof2 = 0;
  i = 1;
//Initializations Ends
  if(argc == 2) {
    error = 0;
////////////////////////////////////////////////////////////////////////////////
// Main Program Here
////////////////////////////////////////////////////////////////////////////////
    frac_out = 0.0;
    while(*argv[1] != '\0') {
        if(*argv[1]-48 == 0 || *argv[1]-48 == 1) {
        //printf("Input char is %d\n",*argv[1]-48);
        poof2 = (float) 1/po(2,i);
        frac_out = frac_out + poof2 * (*argv[1]-48);
        //printf("poof2 = %f\n",poof2);
        *argv[1]++;
        i++;
      }//if(*argv[1]-48 == 0 || *argv[1]-48 == 1)
      else {
        *argv[1] = '\0';
        printf("\n");
        printf("***********************************************************\n");
        printf("This Program Converts a binary Number into a fractional part\n");
        printf("Error: Incorrect Inputs\n");
        printf("Inputs can only be binary Digits i.e 0 or 1\n");
        printf("Example Usage:\n");
        printf("unix> ./a.out 10101010\n");
        printf("0.664062\n");
        printf("***********************************************************\n");
        printf("\n");
        error = 1;
      }
    }//while(*argv[1] != '\0')
    if(error == 0) {
      printf("%f\n",frac_out);
    }//if(error == 0)
////////////////////////////////////////////////////////////////////////////////
  }//if(argc == 2)
  else {
    error = 1;
    printf("\n");
    printf("***********************************************************\n");
    printf("This Program Converts a binary Number into a fractional part\n");
    printf("Error: Incorrect Inputs\n");
    printf("Example Usage:\n");
    printf("unix> ./a.out 10101010\n");
    printf("0.664062\n");
    printf("***********************************************************\n");
    printf("\n");
  } //else of if(argc == 2)
  //printf("error = %d\n",error);

} //main

//Functions Declaration Section
long int  po(short int x,short int y)
{
  //to Calculate 10^4, po(10,4);
  int j;
  long int ret=1;
  for(j=0;j<y;j++) {
    ret = ret*x;
  }
  return(ret);
}
//Function Declaration Ends
