Convert the fractional whole number to binary
Converting numbers from one numerical system to another is a fundamental concept in computer science and mathematics. In this article, we'll explore how to convert fractional whole numbers to binary using a program. We'll define the problem, provide a detailed explanation with suitable examples, present the algorithm, and analyze the time complexity of the code.
Problem Statement
The problem is to convert a given fractional whole number (a decimal number) into its binary representation. This involves converting both the integer part and the fractional part of the number into binary form.
Example
Let's take the number 12.55 as an example. To convert it to binary, we need to convert both the integer part (12) and the fractional part (0.55) to binary form.
- Integer Part (12): 12 in binary is 1100.
- Fractional Part (0.55):
- Multiply 0.55 by 2: 1.1, take the integer part (1).
- Subtract 1 from 1.1: 0.1, multiply by 2: 0.2, take the integer part (0).
- Multiply 0.2 by 2: 0.4, take the integer part (0).
- Multiply 0.4 by 2: 0.8, take the integer part (0).
- Multiply 0.8 by 2: 1.6, take the integer part (1).
- Subtract 1 from 1.6: 0.6, multiply by 2: 1.2, take the integer part (1).
- Subtract 1 from 1.2: 0.2, multiply by 2: 0.4, take the integer part (0).
- Multiply 0.4 by 2: 0.8, take the integer part (0).
- Multiply 0.8 by 2: 1.6, take the integer part (1).
- ... and so on.
Example 1
----------
num = 12.55
12 = 1100 (Binary of whole number)
0.10000000000000142 X 2 = 0.20000000000000284 0
0.20000000000000284 X 2 = 0.4000000000000057 0
0.4000000000000057 X 2 = 0.8000000000000114 0
0.8000000000000114 X 2 = 1.6000000000000227 1
0.6000000000000227 X 2 = 1.2000000000000455 1
0.20000000000004547 X 2 = 0.40000000000009095 0
0.40000000000009095 X 2 = 0.8000000000001819 0
0.8000000000001819 X 2 = 1.6000000000003638 1
0.6000000000003638 X 2 = 1.2000000000007276 1
0.2000000000007276 X 2 = 0.4000000000014552 0
0.4000000000014552 X 2 = 0.8000000000029104 0
0.8000000000029104 X 2 = 1.6000000000058208 1
0.6000000000058208 X 2 = 1.2000000000116415 1
0.20000000001164153 X 2 = 0.40000000002328306 0
0.40000000002328306 X 2 = 0.8000000000465661 0
0.8000000000465661 X 2 = 1.6000000000931323 1
0.6000000000931323 X 2 = 1.2000000001862645 1
0.20000000018626451 X 2 = 0.40000000037252903 0
0.40000000037252903 X 2 = 0.8000000007450581 0
0.8000000007450581 X 2 = 1.6000000014901161 1
0.6000000014901161 X 2 = 1.2000000029802322 1
0.20000000298023224 X 2 = 0.4000000059604645 0
0.4000000059604645 X 2 = 0.800000011920929 0
0.800000011920929 X 2 = 1.600000023841858 1
0.6000000238418579 X 2 = 1.2000000476837158 1
0.20000004768371582 X 2 = 0.40000009536743164 0
0.40000009536743164 X 2 = 0.8000001907348633 0
0.8000001907348633 X 2 = 1.6000003814697266 1
0.6000003814697266 X 2 = 1.2000007629394531 1
0.20000076293945312 X 2 = 0.40000152587890625 0
0.40000152587890625 X 2 = 0.8000030517578125 0
0.8000030517578125 X 2 = 1.600006103515625 1
0.600006103515625 X 2 = 1.20001220703125 1
0.20001220703125 X 2 = 0.4000244140625 0
0.4000244140625 X 2 = 0.800048828125 0
0.800048828125 X 2 = 1.60009765625 1
0.60009765625 X 2 = 1.2001953125 1
0.2001953125 X 2 = 0.400390625 0
0.400390625 X 2 = 0.80078125 0
0.80078125 X 2 = 1.6015625 1
0.6015625 X 2 = 1.203125 1
0.203125 X 2 = 0.40625 0
0.40625 X 2 = 0.8125 0
0.8125 X 2 = 1.625 1
0.625 X 2 = 1.25 1
0.25 X 2 = 0.5 0
0.5 X 2 = 1.0 1
// Result
1100.100011001100110011001100110011001100110011001101
or (fractional of 16 bit)
1100.10001100110011001
Example 2
-----------
num = 99.14
99 = 1100011 (Binary of whole number)
Calculate fractional
0.14 X 2 = 0.28 0
0.28 X 2 = 0.56 0
0.56 X 2 = 1.12 1
0.1200000000000001 X 2 = 0.2400000000000002 0
0.2400000000000002 X 2 = 0.4800000000000004 0
0.4800000000000004 X 2 = 0.9600000000000009 0
0.9600000000000009 X 2 = 1.9200000000000017 1
0.9200000000000017 X 2 = 1.8400000000000034 1
0.8400000000000034 X 2 = 1.6800000000000068 1
0.6800000000000068 X 2 = 1.3600000000000136 1
0.36000000000001364 X 2 = 0.7200000000000273 0
0.7200000000000273 X 2 = 1.4400000000000546 1
0.44000000000005457 X 2 = 0.8800000000001091 0
0.8800000000001091 X 2 = 1.7600000000002183 1
0.7600000000002183 X 2 = 1.5200000000004366 1
0.5200000000004366 X 2 = 1.0400000000008731 1
0.040000000000873115 X 2 = 0.08000000000174623 0
0.08000000000174623 X 2 = 0.16000000000349246 0
0.16000000000349246 X 2 = 0.3200000000069849 0
0.3200000000069849 X 2 = 0.6400000000139698 0
0.6400000000139698 X 2 = 1.2800000000279397 1
0.2800000000279397 X 2 = 0.5600000000558794 0
0.5600000000558794 X 2 = 1.1200000001117587 1
0.12000000011175871 X 2 = 0.24000000022351742 0
0.24000000022351742 X 2 = 0.48000000044703484 0
0.48000000044703484 X 2 = 0.9600000008940697 0
0.9600000008940697 X 2 = 1.9200000017881393 1
0.9200000017881393 X 2 = 1.8400000035762787 1
0.8400000035762787 X 2 = 1.6800000071525574 1
0.6800000071525574 X 2 = 1.3600000143051147 1
0.36000001430511475 X 2 = 0.7200000286102295 0
0.7200000286102295 X 2 = 1.440000057220459 1
0.440000057220459 X 2 = 0.880000114440918 0
0.880000114440918 X 2 = 1.760000228881836 1
0.7600002288818359 X 2 = 1.5200004577636719 1
0.5200004577636719 X 2 = 1.0400009155273438 1
0.04000091552734375 X 2 = 0.0800018310546875 0
0.0800018310546875 X 2 = 0.160003662109375 0
0.160003662109375 X 2 = 0.32000732421875 0
0.32000732421875 X 2 = 0.6400146484375 0
0.6400146484375 X 2 = 1.280029296875 1
0.280029296875 X 2 = 0.56005859375 0
0.56005859375 X 2 = 1.1201171875 1
0.1201171875 X 2 = 0.240234375 0
0.240234375 X 2 = 0.48046875 0
0.48046875 X 2 = 0.9609375 0
0.9609375 X 2 = 1.921875 1
0.921875 X 2 = 1.84375 1
0.84375 X 2 = 1.6875 1
0.6875 X 2 = 1.375 1
0.375 X 2 = 0.75 0
0.75 X 2 = 1.5 1
0.5 X 2 = 1.0 1
Given Number : 99.14
Result .00100011110101110000101000111101011100001010001111011
or 16 bits
1100011.0010001111010111
The binary representation of the fractional part of 0.55 is approximately 0.1000110011001100...
Therefore, the complete binary representation of 12.55 is 1100.1000110011001100...
Idea to Solve the Problem
To solve this problem, we need to separate the integer and fractional parts of the given number. Then, we'll convert each part to its binary representation and combine them to get the final binary representation of the number.
Pseudocode
Here's the pseudocode for the solution:
function binaryNo(num)
if num < 0
return
end if
whole = integer part of num
fractional = num - whole
result = ""
while whole > 0
result = (whole % 2) + result
whole = whole / 2
end while
result += "."
bit = 0
while fractional > 0 and bit < 16
fractional = fractional * 2
if integer part of fractional is 1
fractional = fractional - 1
result += "1"
else
result += "0"
end if
bit += 1
end while
output "Given Number:", num
output "Result:", result
end function
main
task = new Conversion
task.binaryNo(12.55)
task.binaryNo(99.14)
end main
Algorithm Explanation
- The
binaryNo
function takes a single parameter,num
, representing the fractional whole number to be converted. - It separates the integer and fractional parts of the number using the modulo operation and subtraction.
- The integer part is converted to binary using a loop that continuously divides the integer by 2 and appends the remainder to the result.
- A decimal point is added to the result to prepare for the fractional part's conversion.
- The fractional part is converted to binary using a loop that multiplies the fractional part by 2 and checks the integer part of the result. If it's 1, the result gets a "1," otherwise, a "0."
- The process is repeated for up to 16 bits of the fractional part.
- The final binary representation is printed along with the original number.
Code Solution
// Java Program
// Convert the fractional whole number to binary
public class Conversion
{
public void binaryNo(double num)
{
if (num < 0)
{
return;
}
// Get whole number
int whole = (int) num;
// Get approximate fraction part
double fractional = (num - whole);
String result = "";
// Find binary of whole number
while (whole > 0)
{
result = (whole % 2) + result;
whole /= 2;
}
result += ".";
int bit = 0;
// Find binary of fractional number
// Consider it up to 16 digit
while (fractional > 0 && bit < 16)
{
fractional *= 2;
if ((int) fractional == 1)
{
fractional = fractional - 1;
result += "1";
}
else
{
result += "0";
}
// increase counter of a fraction bits
bit += 1;
}
System.out.print("\n Given Number : " + num);
// Display calculated result
System.out.print("\n Result " + result);
}
public static void main(String[] args)
{
Conversion task = new Conversion();
// Test Case
task.binaryNo(12.55);
task.binaryNo(99.14);
}
}
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ Program
// Convert the fractional whole number to binary
class Conversion
{
public: void binaryNo(double num)
{
if (num < 0)
{
return;
}
// Get whole number
int whole = (int) num;
// Get approximate fraction part
double fractional = (num - whole);
string result = "";
// Find binary of whole number
while (whole > 0)
{
result = to_string(whole % 2) + result;
whole /= 2;
}
result += ".";
int bit = 0;
// Find binary of fractional number
// Consider it up to 16 digit
while (fractional > 0 && bit < 16)
{
fractional *= 2;
if ((int) fractional == 1)
{
fractional = fractional - 1;
result += "1";
}
else
{
result += "0";
}
// increase counter of a fraction bits
bit += 1;
}
cout << "\n Given Number : " << num;
// Display calculated result
cout << "\n Result " << result;
}
};
int main()
{
Conversion task = Conversion();
// Test Case
task.binaryNo(12.55);
task.binaryNo(99.14);
return 0;
}
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
// Include namespace system
using System;
// C# Program
// Convert the fractional whole number to binary
public class Conversion
{
public void binaryNo(double num)
{
if (num < 0)
{
return;
}
// Get whole number
int whole = (int) num;
// Get approximate fraction part
double fractional = (num - whole);
String result = "";
// Find binary of whole number
while (whole > 0)
{
result = (whole % 2) + result;
whole /= 2;
}
result += ".";
int bit = 0;
// Find binary of fractional number
// Consider it up to 16 digit
while (fractional > 0 && bit < 16)
{
fractional *= 2;
if ((int) fractional == 1)
{
fractional = fractional - 1;
result += "1";
}
else
{
result += "0";
}
// increase counter of a fraction bits
bit += 1;
}
Console.Write("\n Given Number : " + num);
// Display calculated result
Console.Write("\n Result " + result);
}
public static void Main(String[] args)
{
Conversion task = new Conversion();
// Test Case
task.binaryNo(12.55);
task.binaryNo(99.14);
}
}
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
<?php
// Php Program
// Convert the fractional whole number to binary
class Conversion
{
public function binaryNo($num)
{
if ($num < 0)
{
return;
}
// Get whole number
$whole = (int) $num;
// Get approximate fraction part
$fractional = ($num - $whole);
$result = "";
// Find binary of whole number
while ($whole > 0)
{
$result = ($whole % 2) . $result;
$whole = intval($whole / 2);
}
$result .= ".";
$bit = 0;
// Find binary of fractional number
// Consider it up to 16 digit
while ($fractional > 0 && $bit < 16)
{
$fractional *= 2;
if ((int) $fractional == 1)
{
$fractional = $fractional - 1;
$result .= "1";
}
else
{
$result .= "0";
}
// increase counter of a fraction bits
$bit += 1;
}
echo "\n Given Number : ". $num;
// Display calculated result
echo "\n Result ". $result;
}
}
function main()
{
$task = new Conversion();
$task->binaryNo(12.55);
$task->binaryNo(99.14);
}
main();
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
// Node Js Program
// Convert the fractional whole number to binary
class Conversion
{
binaryNo(num)
{
if (num < 0)
{
return;
}
// Get whole number
var whole = parseInt(num);
// Get approximate fraction part
var fractional = (num - whole);
var result = "";
// Find binary of whole number
while (whole > 0)
{
result = (whole % 2) + result;
whole = parseInt(whole / 2);
}
result += ".";
var bit = 0;
// Find binary of fractional number
// Consider it up to 16 digit
while (fractional > 0 && bit < 16)
{
fractional *= 2;
if (parseInt(fractional) == 1)
{
fractional = fractional - 1;
result += "1";
}
else
{
result += "0";
}
// increase counter of a fraction bits
bit += 1;
}
process.stdout.write("\n Given Number : " + num);
// Display calculated result
process.stdout.write("\n Result " + result);
}
}
function main()
{
var task = new Conversion();
// Test Case
task.binaryNo(12.55);
task.binaryNo(99.14);
}
main();
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
# Python 3 Program
# Convert the fractional whole number to binary
class Conversion :
def binaryNo(self, num) :
if (num < 0) :
return
# Get whole number
whole = int(num)
# Get approximate fraction part
fractional = (num - whole)
result = ""
# Find binary of whole number
while (whole > 0) :
result = str(whole % 2) + result
whole = int(whole / 2)
result += "."
bit = 0
# Find binary of fractional number
# Consider it up to 16 digit
while (fractional > 0 and bit < 16) :
fractional *= 2
if (int(fractional) == 1) :
fractional = fractional - 1
result += "1"
else :
result += "0"
# increase counter of a fraction bits
bit += 1
print("\n Given Number : ", num, end = "")
# Display calculated result
print("\n Result ", result, end = "")
def main() :
task = Conversion()
# Test Case
task.binaryNo(12.55)
task.binaryNo(99.14)
if __name__ == "__main__": main()
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
# Ruby Program
# Convert the fractional whole number to binary
class Conversion
def binaryNo(num)
if (num < 0)
return
end
# Get whole number
whole = (num).to_i
# Get approximate fraction part
fractional = (num - whole)
result = ""
# Find binary of whole number
while (whole > 0)
result = (whole % 2).to_s + result
whole /= 2
end
result += "."
bit = 0
# Find binary of fractional number
# Consider it up to 16 digit
while (fractional > 0 && bit < 16)
fractional *= 2
if ((fractional).to_i == 1)
fractional = fractional - 1
result += "1"
else
result += "0"
end
# increase counter of a fraction bits
bit += 1
end
print("\n Given Number : ", num)
# Display calculated result
print("\n Result ", result)
end
end
def main()
task = Conversion.new()
# Test Case
task.binaryNo(12.55)
task.binaryNo(99.14)
end
main()
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
// Scala Program
// Convert the fractional whole number to binary
class Conversion
{
def binaryNo(num: Double): Unit = {
if (num < 0)
{
return;
}
// Get whole number
var whole: Int = (num).toInt;
// Get approximate fraction part
var fractional: Double = (num - whole);
var result: String = "";
// Find binary of whole number
while (whole > 0)
{
result = ""+(whole % 2) + result;
whole = (whole / 2).toInt;
}
result += ".";
var bit: Int = 0;
// Find binary of fractional number
// Consider it up to 16 digit
while (fractional > 0 && bit < 16)
{
fractional *= 2;
if ((fractional).toInt == 1)
{
fractional = fractional - 1;
result += "1";
}
else
{
result += "0";
}
// increase counter of a fraction bits
bit += 1;
}
print("\n Given Number : " + num);
// Display calculated result
print("\n Result " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Conversion = new Conversion();
// Test Case
task.binaryNo(12.55);
task.binaryNo(99.14);
}
}
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
// Swift 4 Program
// Convert the fractional whole number to binary
class Conversion
{
func binaryNo(_ num: Double)
{
if (num < 0)
{
return;
}
// Get whole number
var whole: Int = Int(num);
// Get approximate fraction part
var fractional: Double = (num - Double(whole));
var result: String = "";
// Find binary of whole number
while (whole > 0)
{
result = String(whole % 2) + result;
whole /= 2;
}
result += ".";
var bit: Int = 0;
// Find binary of fractional number
// Consider it up to 16 digit
while (fractional > 0 && bit < 16)
{
fractional *= 2;
if (Int(fractional) == 1)
{
fractional = fractional - 1;
result += "1";
}
else
{
result += "0";
}
// increase counter of a fraction bits
bit += 1;
}
print("\n Given Number : ", num, terminator: "");
// Display calculated result
print("\n Result ", result, terminator: "");
}
}
func main()
{
let task: Conversion = Conversion();
// Test Case
task.binaryNo(12.55);
task.binaryNo(99.14);
}
main();
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
// Kotlin Program
// Convert the fractional whole number to binary
class Conversion
{
fun binaryNo(num: Double): Unit
{
if (num < 0)
{
return;
}
// Get whole number
var whole: Int = num.toInt();
// Get approximate fraction part
var fractional: Double = (num - whole.toDouble());
var result: String = "";
// Find binary of whole number
while (whole > 0)
{
result = (whole % 2).toString() + result;
whole /= 2;
}
result += ".";
var bit: Int = 0;
// Find binary of fractional number
// Consider it up to 16 digit
while (fractional > 0 && bit < 16)
{
fractional *= 2;
if (fractional.toInt() == 1)
{
fractional = fractional - 1;
result += "1";
}
else
{
result += "0";
}
// increase counter of a fraction bits
bit += 1;
}
print("\n Given Number : " + num);
// Display calculated result
print("\n Result " + result);
}
}
fun main(args: Array < String > ): Unit
{
var task: Conversion = Conversion();
// Test Case
task.binaryNo(12.55);
task.binaryNo(99.14);
}
Output
Given Number : 12.55
Result 1100.1000110011001100
Given Number : 99.14
Result 1100011.0010001111010111
Resultant Output Explanation
Running the program results in the following output:
Given Number : 12.55
Result: 1100.1000110011001
Given Number : 99.14
Result: 1100011.00100011110101
The output showcases the binary representations of the given fractional whole numbers. For example, the first line indicates that the binary representation of 12.55 starts with "1100" for the integer part and continues with the approximate binary representation of the fractional part.
Time Complexity
The time complexity of the conversion process depends on the number of bits needed to represent the integer and fractional parts. The integer part requires logarithmic time with respect to the base 2 of the input number. The fractional part's conversion has a fixed iteration count (up to 16 bits). Thus, the time complexity can be considered O(log n), where n is the given number.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment