Conversion of Fractions from Binary to Decimal and Vice Versa

Two functions are described below which will help in conversion of Fractional numbers.
9.8K Downloads
Updated 7 Jul 2004

No License

The function Fr_bin2dec.m will convert a positive Binary system Fraction to a Decimal system Fraction. The function Fr_dec2bin.m will convert a positive Decimal system Fraction to a Binary system Fraction.

MATLAB itself has bin2dec.m and dec2bin.m, but there seems to be no standard MATLAB function when fractions are involved. For eg, if we wish to quickly obtain the decimal equivalent of the binary fraction : 0.000101, or say, 0.010101111, or say, 11010.1011001 etc. (They are : 0.078125, 0.341796875 and 26.6953125 resp.)

These two dual functions were developed mainly with a view to get quick results while learning Arithmetic (Entropy) Coding in School.(Now, I have added more comments to better explain the programme to other viewers.)

The results of these two functions are limited in accuracy due to the "precision" used in the function num2str.m in addition to Floating Point limits and Rounding errors. These functions have been set to a precision of 16 in the use of num2str.m. Accumulation of errors due to these limits can be seen when the two functions are tested back-to-back in pairs.

Both these files have many "Usage Egs" which will make it easy for the reader to apply these functions and test them with more critical / unique cases of his own - including possibly with some cases which may break the code ? Pl do forward me any new case that breaks the code beyond the aforesaid limitations.

Cite As

Sundar Krishnan (2024). Conversion of Fractions from Binary to Decimal and Vice Versa (https://www.mathworks.com/matlabcentral/fileexchange/5396-conversion-of-fractions-from-binary-to-decimal-and-vice-versa), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0