Given an string of binary number n. Convert binary fractional n into it's decimal equivalent.
Examples:
Input: n = 110.101
Output: 6.625
Explanation: Integer part (110 = 6) and fractional part (.101 = 0.625) -> total = 6.625
Input: n = 101.1101
Output: 5.8125
Explanation: Integer part (101 = 5) and fractional part (.1101 = 0.8125) -> total = 5.8125
Following are the steps of converting binary fractional to decimal.
1) Convert the integral part of binary to decimal equivalent
- Multiply each digit separately from left side of radix point till the first digit by 20, 21, 22,... respectively.
- Add all the result coming from step 1.
- Equivalent integral decimal number would be the result obtained in step 2.
2) Convert the fractional part of binary to decimal equivalent
- Divide each digit from right side of radix point till the end by 21, 22, 23, ... respectively.
- Add all the result coming from step 1.
- Equivalent fractional decimal number would be the result obtained in step 2.
3) Add both integral and fractional part of decimal number.
Let's take an example for n = 110.101 ,
Step 1: Conversion of 110 to decimal => 1102 = (1*22) + (1*21) + (0*20) => 1102 = 4 + 2 + 0 => 1102 = 6 So equivalent decimal of binary integral is 6.
Step 2: Conversion of .101 to decimal => 0.1012 = (1*1/2) + (0*1/22) + (1*1/23) => 0.1012 = 1*0.5 + 0*0.25 + 1*0.125 => 0.1012 = 0.625 So equivalent decimal of binary fractional is 0.625
Step 3: Add result of step 1 and 2. => 6 + 0.625 = 6.625
// C++ program to demonstrate above steps of
// binary fractional to decimal conversion
#include<bits/stdc++.h>
using namespace std;
// Function to convert binary fractional to
// decimal
long double convertToDecimal(const string &n) {
int pos = n.length();
// Find the position of '.'
for(int i = 0; i < n.length(); i++)
{
if(n[i] == '.')
{
pos = i;
break;
}
}
long double intDecimal = 0.0, fracDecimal = 0.0, twos = 1.0;
// Convert integral part
for (int i = pos - 1; i >= 0; --i)
{
intDecimal += (n[i] - '0') * twos;
twos *= 2.0;
}
// Convert fractional part
twos = 2.0;
for (int i = pos + 1; i < n.length(); ++i)
{
fracDecimal += (n[i] - '0') / twos;
twos *= 2.0;
}
return intDecimal + fracDecimal;
}
// Driver code
int main()
{
string n = "110.101";
cout << convertToDecimal(n) << "\n";
n = "101.1101";
cout << convertToDecimal(n);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
// Function to convert binary fractional to
// decimal
long double convertToDecimal(const char *n) {
int pos = strlen(n);
// Find the position of '.'
for(int i = 0; i < strlen(n); i++)
{
if(n[i] == '.')
{
pos = i;
break;
}
}
long double intDecimal = 0.0, fracDecimal = 0.0, twos = 1.0;
// Convert integral part
for (int i = pos - 1; i >= 0; --i)
{
intDecimal += (n[i] - '0') * twos;
twos *= 2.0;
}
// Convert fractional part
twos = 2.0;
for (int i = pos + 1; i < strlen(n); ++i)
{
fracDecimal += (n[i] - '0') / twos;
twos *= 2.0;
}
return intDecimal + fracDecimal;
}
// Driver code
int main() {
char n[] = "110.101";
printf("%Lf\n", convertToDecimal(n));
strcpy(n, "101.1101");
printf("%Lf", convertToDecimal(n));
return 0;
}
import java.util.*;
// Function to convert binary fractional to
// decimal
public class Main {
public static double convertToDecimal(String n) {
int pos = n.length();
// Find the position of '.'
for(int i = 0; i < n.length(); i++)
{
if(n.charAt(i) == '.')
{
pos = i;
break;
}
}
double intDecimal = 0.0, fracDecimal = 0.0, twos = 1.0;
// Convert integral part
for (int i = pos - 1; i >= 0; --i)
{
intDecimal += (n.charAt(i) - '0') * twos;
twos *= 2.0;
}
// Convert fractional part
twos = 2.0;
for (int i = pos + 1; i < n.length(); ++i)
{
fracDecimal += (n.charAt(i) - '0') / twos;
twos *= 2.0;
}
return intDecimal + fracDecimal;
}
// Driver code
public static void main(String[] args) {
String n = "110.101";
System.out.println(convertToDecimal(n));
n = "101.1101";
System.out.println(convertToDecimal(n));
}
}
# Python3 program to convert binary fractional to decimal
# Function to convert binary fractional to decimal
def convertToDecimal(n):
pos = len(n)
# Find the position of '.'
for i in range(len(n)):
if n[i] == '.':
pos = i
break
intDecimal = 0.0
fracDecimal = 0.0
twos = 1.0
# Convert integral part
for i in range(pos - 1, -1, -1):
intDecimal += (ord(n[i]) - ord('0')) * twos
twos *= 2.0
# Convert fractional part
twos = 2.0
for i in range(pos + 1, len(n)):
fracDecimal += (ord(n[i]) - ord('0')) / twos
twos *= 2.0
return intDecimal + fracDecimal
# Driver code
n = "110.101"
print(convertToDecimal(n))
n = "101.1101"
print(convertToDecimal(n))
// C# program to convert binary fractional to decimal
using System;
public class GfG
{
// Function to convert binary fractional to decimal
public static double ConvertToDecimal(string n)
{
int pos = n.Length;
// Find the position of '.'
for (int i = 0; i < n.Length; i++)
{
if (n[i] == '.')
{
pos = i;
break;
}
}
double intDecimal = 0.0, fracDecimal = 0.0, twos = 1.0;
// Convert integral part
for (int i = pos - 1; i >= 0; --i)
{
intDecimal += (n[i] - '0') * twos;
twos *= 2.0;
}
// Convert fractional part
twos = 2.0;
for (int i = pos + 1; i < n.Length; ++i)
{
fracDecimal += (n[i] - '0') / twos;
twos *= 2.0;
}
return intDecimal + fracDecimal;
}
// Driver code
public static void Main()
{
string n = "110.101";
Console.WriteLine(ConvertToDecimal(n));
n = "101.1101";
Console.WriteLine(ConvertToDecimal(n));
}
}
// JavaScript program to demonstrate above steps of
// binary fractional to decimal conversion
// Function to convert binary fractional to
// decimal
function convertToDecimal(n) {
let pos = n.length;
// Find the position of '.'
for(let i = 0; i < n.length; i++)
{
if(n[i] === '.')
{
pos = i;
break;
}
}
let intDecimal = 0.0, fracDecimal = 0.0, twos = 1.0;
// Convert integral part
for (let i = pos - 1; i >= 0; --i)
{
intDecimal += (n[i].charCodeAt(0) - '0'.charCodeAt(0)) * twos;
twos *= 2.0;
}
// Convert fractional part
twos = 2.0;
for (let i = pos + 1; i < n.length; ++i)
{
fracDecimal += (n[i].charCodeAt(0) - '0'.charCodeAt(0)) / twos;
twos *= 2.0;
}
return intDecimal + fracDecimal;
}
// Driver code
let n = "110.101";
console.log(convertToDecimal(n) + "\n");
n = "101.1101";
console.log(convertToDecimal(n));
Output
6.625 5.8125
Time complexity: O(len(n))Ā
Auxiliary space: O(len(n))Ā
Where len is the total digits contain in binary number of n.