### Highlights from Conversion of Fractions from Binary to Decimal and Vice Versa

4.4375

4.4 | 16 ratings Rate this file 32 Downloads (last 30 days) File Size: 9.09 KB File ID: #5396

# Conversion of Fractions from Binary to Decimal and Vice Versa

02 Jul 2004 (Updated 07 Jul 2004)

Two functions are described below which will help in conversion of Fractional numbers.

File Information
Description

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.

MATLAB release MATLAB 6.5 (R13)
18 Jul 2012

Thank you, I found what I am looking for

05 Jun 2011

good job

26 Mar 2010

pi
= 0b11.0010010000111111011010101000100010000101101000110000100011010011001

but
>> Fr_dec2bin(pi)

ans =

11.0010010000111111

Where are all the other bits?

04 Feb 2010

i need it for implementing the distributed arithematic algorithm.i will try it out

09 May 2009

I stumbled across these functions through Google, and I wasn't even looking for Matlab. How convenient. Plenty accurate for my purposes.

03 May 2008

wat abt negative binary numbers or decimal fractions

03 Oct 2007

Fr_dec2bin(19.5325)=10011.10001000010100011
Fr_bin2dec(10011.10001000010100011)=19.5322

This does not work for me... in this case
these values should be the same

22 Sep 2007
06 Apr 2007

Well done!

01 Apr 2007

I'm satisfied. No problem in my application with this function.

27 Feb 2007

good move

09 Sep 2006

You need to define, for example, dec=1/3 (or whatever number you want to convert) in the first line of the function and it works nicely.

05 Sep 2006

There is a lot to explore. I'll need time to see if this will work for me .. Teacher of Special Education Students ...

04 Sep 2006

It seems very good function but i am sorry i am not able to run it properly. Can you please explain it how to use this programme.i want to convert filter coefficients to its equivalent 32bit binary floating point.
Pls let me know how to use this and how to export results from matlab to xilinx ise.
i am new to matlab.

10 May 2006

exact and useful

27 Oct 2005

what if I want to convert a signed decimal fraction to binary ??????? .. can we add a 2's complement generation in this function ?

20 Oct 2005

BINGO

18 Sep 2005

Just what i was looking for. Thanks!

14 Jul 2005

very accurate.cross checked with mathematica to obtain precisely the same answer.

23 Mar 2005

thank you this is exactly what is was looking for to calculate my coefficients in an easy and fast way ... THAMK YOU

23 Mar 2005

good

26 Feb 2005

THANKS! I had the same problem in working my Information Theory course HW.

05 Aug 2004

this is great