Converting a Real Number (between 0 and 1) to Binary String
Last Updated :
13 Oct, 2022
Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print” ERROR:’
Examples:
Input : (0.625)10
Output : (0.101)2
Input : (0.72)10
Output : ERROR
Solution: First, let’s start off by asking ourselves what a non-integer number in binary looks like. By analogy to a decimal number, the binary number 0 .1012 would look like:
0. 1012 = 1 * 1/21 + 0 *1/22 + 1 * 1/23 .
Method 1: Multiply the decimal part by 2
To print the decimal part, we can multiply by 2 and check if 2*n is greater than or equal to 1. This is essentially “shifting” the fractional sum. That is:
r = 210 * n;
= 210 * 0.1012;
= 1 * 1/20 + 0 *1/21 + 1 * 1/22;
= 1.012;
If r >= 1, then we know that n had a 1 right after the decimal point. By doing this continuously, we can check every digit.
C++
#include <iostream>
#include<string>
using namespace std;
string toBinary( double n)
{
if (n >= 1 || n <= 0)
return "ERROR" ;
string answer;
double frac = 0.5;
answer.append( "." );
while (n > 0)
{
if (answer.length() >= 32)
return "ERROR" ;
double b = n * 2;
if (b >= 1)
{
answer.append( "1" );
n = b - 1;
}
else
{
answer.append( "0" );
n = b;
}
}
return answer;
}
int main()
{
double n = 0.625;
string result = toBinary(n);
cout<< "(0" << result << ") in base 2" <<endl;
double m = 0.72;
result= toBinary(m);
cout<< "(" <<result<< ")" <<endl;
}
|
Java
import java.lang.*;
import java.io.*;
import java.util.*;
class BinaryToString
{
static String printBinary( double num)
{
if (num >= 1 || num <= 0 )
return "ERROR" ;
StringBuilder binary = new StringBuilder();
binary.append( "." );
while (num > 0 )
{
if (binary.length() >= 32 )
return "ERROR" ;
double r = num * 2 ;
if (r >= 1 )
{
binary.append( 1 );
num = r - 1 ;
}
else
{
binary.append( 0 );
num = r;
}
}
return binary.toString();
}
public static void main(String[] args)
{
double num1 = 0.625 ;
String output = printBinary(num1);
System.out.println( "(0" + output + ") in base 2" );
double num2 = 0.72 ;
output = printBinary(num2);
System.out.println( "(" + output + ") " );
}
}
|
Python3
def toBinary(n):
if (n > = 1 or n < = 0 ):
return "ERROR"
answer = ""
frac = 0.5
answer = answer + "."
while (n > 0 ):
if ( len (answer) > = 32 ):
return "ERROR"
b = n * 2
if (b > = 1 ):
answer = answer + "1"
n = b - 1
else :
answer = answer + "0"
n = b
return answer
if __name__ = = '__main__' :
n = 0.625
result = toBinary(n)
print ( "(0" , result, ") in base 2" )
m = 0.72
result = toBinary(m)
print ( "(" , result, ")" )
|
C#
using System;
using System.Text;
class BinaryToString
{
static String printBinary( double num)
{
if (num >= 1 || num <= 0)
return "ERROR" ;
StringBuilder binary = new StringBuilder();
binary.Append( "." );
while (num > 0)
{
if (binary.Length >= 32)
return "ERROR" ;
double r = num * 2;
if (r >= 1)
{
binary.Append(1);
num = r - 1;
}
else
{
binary.Append(0);
num = r;
}
}
return binary.ToString();
}
public static void Main()
{
double num1 = 0.625;
String output = printBinary(num1);
Console.WriteLine( "(0 " + output + ") in base 2" );
double num2 = 0.72;
output = printBinary(num2);
Console.WriteLine( "(" + output + ") " );
}
}
|
PHP
<?php
function toBinary( $n )
{
if ( $n >= 1 || $n <= 0)
return "ERROR" ;
$answer = "" ;
$frac = 0.5;
$answer .= "." ;
while ( $n > 0)
{
if ( strlen ( $answer ) >= 32)
return "ERROR" ;
$b = $n * 2;
if ( $b >= 1)
{
$answer .= "1" ;
$n = $b - 1;
}
else
{
$answer .= "0" ;
$n = $b ;
}
}
return $answer ;
}
$n = 0.625;
$result = toBinary( $n );
echo "(0" . $result . ") in base 2\n" ;
$m = 0.72;
$result = toBinary( $m );
echo "(" . $result . ")" ;
?>
|
Javascript
<script>
function toBinary(n){
if (n >= 1 || n <= 0)
return "ERROR"
let answer = ""
let frac = 0.5
answer = answer + "."
while (n > 0){
if (answer.length >= 32){
return "ERROR"
}
b = n * 2
if (b >= 1){
answer = answer + "1"
n = b - 1
}
else {
answer = answer + "0"
n = b
}
}
return answer
}
let n = 0.625
let result = toBinary(n)
document.write(`(0 ${result}) in base 2`, "</br>" )
let m = 0.72
result = toBinary(m)
document.write(`(${result})`, "</br>" )
</script>
|
Output:
(0.101) in base 2
(ERROR)
Method 2
Alternatively, rather than multiplying the number by two and comparing it to 1, we can compare the number to . 5, then . 25, and so on. The code below demonstrates this approach.
C++
#include <iostream>
#include<string>
using namespace std;
string toBinary( double n)
{
if (n >= 1 || n <= 0)
return "ERROR" ;
string answer;
double frac = 0.5;
answer.append( "." );
while (n > 0)
{
if (answer.length() >= 32)
return "ERROR" ;
if (n >= frac)
{
answer.append( "1" );
n = n- frac;
}
else
{
answer.append( "0" );
}
frac /= 2;
}
return answer;
}
int main()
{
double n = 0.625;
string result = toBinary(n);
cout<< "(0" << result << ") in base 2" <<endl;
double m = 0.72;
result= toBinary(m);
cout<< "(" <<result<< ")" <<endl;
}
|
Java
import java.lang.*;
import java.io.*;
import java.util.*;
class BinaryToString
{
static String printBinary( double num)
{
if (num >= 1 || num <= 0 )
return "ERROR" ;
StringBuilder binary = new StringBuilder();
double frac = 0.5 ;
binary.append( "." );
while (num > 0 )
{
if (binary.length() >= 32 )
return "ERROR" ;
if (num >= frac)
{
binary.append( 1 );
num -= frac;
}
else
binary.append( 0 );
frac /= 2 ;
}
return binary.toString();
}
public static void main(String[] args)
{
double num1 = 0.625 ;
String output = printBinary(num1);
System.out.println( "(0" + output + ") in base 2" );
double num2 = 0.72 ;
output = printBinary(num2);
System.out.println( "(" + output + ") " );
}
}
|
Python3
def toBinary(n):
if (n > = 1 or n < = 0 ):
return "ERROR" ;
frac = 0.5 ;
answer = "." ;
while (n > 0 ):
if ( len (answer) > = 32 ):
return "ERROR" ;
if (n > = frac):
answer + = "1" ;
n = n - frac;
else :
answer + = "0" ;
frac = (frac / 2 );
return answer;
n = 0.625 ;
result = toBinary(n);
print ( "( 0" , result, ") in base 2" );
m = 0.72 ;
result = toBinary(m);
print ( "(" , result, ")" );
|
C#
using System;
class BinaryToString
{
static string printBinary( double num)
{
if (num >= 1 || num <= 0)
return "ERROR" ;
string binary = "" ;
double frac = 0.5;
binary += "." ;
while (num > 0)
{
if (binary.Length >= 32)
return "ERROR" ;
if (num >= frac)
{
binary += "1" ;
num -= frac;
}
else
binary += "0" ;
frac /= 2;
}
return binary;
}
public static void Main()
{
double num1 = 0.625;
String output = printBinary(num1);
Console.WriteLine( "(0" + output + ") in base 2" );
double num2 = 0.72;
output = printBinary(num2);
Console.WriteLine( "(" + output + ") " );
}
}
|
PHP
<?php
function toBinary( $n )
{
if ( $n >= 1 || $n <= 0)
return "ERROR" ;
$frac = 0.5;
$answer = "." ;
while ( $n > 0)
{
if ( strlen ( $answer ) >= 32)
return "ERROR" ;
if ( $n >= $frac )
{
$answer .= "1" ;
$n = $n - $frac ;
}
else
{
$answer .= "0" ;
}
$frac = ( $frac / 2);
}
return $answer ;
}
$n = 0.625;
$result = toBinary( $n );
print ( "(0" . $result . ") in base 2\n" );
$m = 0.72;
$result = toBinary( $m );
print ( "(" . $result . ")" );
?>
|
Javascript
<script>
function printBinary(num)
{
if (num >= 1 || num <= 0)
return "ERROR" ;
let binary = "" ;
let frac = 0.5;
binary += "." ;
while (num > 0)
{
if (binary.length >= 32)
return "ERROR" ;
if (num >= frac)
{
binary += "1" ;
num -= frac;
}
else
binary += "0" ;
frac = frac / 2;
}
return binary;
}
let num1 = 0.625;
let output = printBinary(num1);
document.write( "(0" + output +
") in base 2" + "</br>" );
let num2 = 0.72;
output = printBinary(num2);
document.write( "(" + output + ") " );
</script>
|
Output:
(0.101) in base 2
(ERROR)
Both approaches are equally good; choose the one you feel most comfortable with.
This article is contributed by Mr. Somesh Awasthi.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...