Binary Representation of Real Numbers

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer science, binary is a numbering system that uses only two digits, 0 and 1. It is widely used in digital electronics and computer systems. In this challenge, we will write a Python program to print the binary representation of a given real number between 0 and 1.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/bit_manipulation("`Bit Manipulation`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/bit_manipulation -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} python/conditional_statements -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} python/while_loops -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} python/lists -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} python/tuples -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} python/function_definition -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} python/classes_objects -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} python/encapsulation -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} python/build_in_functions -.-> lab-268818{{"`Binary Representation of Real Numbers`"}} end

Print Binary

Problem

Write a Python function that takes a real number between 0 and 1 as input and returns its binary representation as a string. If the length of the representation is greater than 32, return 'ERROR'.

Requirements

To solve this problem, we need to ensure the following requirements:

  • The input must be a float.
  • The output must be a string.
  • The range of the input is between 0 and 1, but the values 0 and 1 are not inclusive.
  • The result must include a trailing zero and decimal point.
  • The leading zero and decimal point are counted in the 32 character limit.
  • We cannot assume that the inputs are valid.
  • We can assume that the program fits in memory.

Example Usage

Here are some examples of how the function should behave:

  • None -> 'ERROR'
  • Out of bounds (0, 1) -> 'ERROR'
  • 0.625 -> 0.101
  • 0.987654321 -> 'ERROR'

Summary

In this challenge, we learned how to write a Python program to print the binary representation of a given real number between 0 and 1. We also defined the requirements for the program and provided some examples of how it should behave.

Other Algorithm Tutorials you may like