Share your knowledge at the LQ Wiki.
 LinuxQuestions.org how to convert binary fractions to decimal
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 11-18-2006, 07:26 AM #1 jabka Member   Registered: Sep 2005 Distribution: Debian unstable && Kazit (reformated knoppix) Posts: 194 Rep: how to convert binary fractions to decimal im trieing to convert 101011.01 to octal dec and hex : so i took 101011.01 and to dec : 43 + 0 * 2^{-1} + 1* 2^{-2}= 43.25 101011.01 to oct. 53+ 0*2^-1 + 2^{-2} = 53.25 and to hex : 2b.25 but it was wrong (i try to go back by same option and i get other nombers)
 11-18-2006, 07:45 AM #2 dmail Member   Registered: Oct 2005 Posts: 970 Rep: I don't understand what your are trying to do and what a "binary fraction" is, as it doesn't make sense. Do yo mean floating point numbers? Last edited by dmail; 11-18-2006 at 07:47 AM.
11-18-2006, 09:12 AM   #3
Flesym
Member

Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep:
Quote:
 Originally Posted by dmail I don't understand what your are trying to do and what a "binary fraction" is, as it doesn't make sense. Do yo mean floating point numbers?
Oh, a binary fraction does make sense, of course! -It is the same as a decimal fraction, but with the base of 2 instead of 10.

jabka: Your algorithm for converting is not the best one, but basically correct. It even (and only) works for base 10. For any other base the 2^{-2} is not 0.25. Actually it is a fourth relative to the base. Means: for oct it would be 0.2 and for hex 0.4. I think you will be able to apply this to your problem...

Last edited by Flesym; 11-18-2006 at 09:38 AM.

11-18-2006, 09:42 AM   #4
xhi
Senior Member

Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep:
Quote:
 Originally Posted by dmail I don't understand what your are trying to do and what a "binary fraction" is, as it doesn't make sense. Do yo mean floating point numbers?
yeh im with you dmail. that just doesnt make any sense.

11-18-2006, 09:56 AM   #5
dmail
Member

Registered: Oct 2005
Posts: 970

Rep:
Quote:
 Oh, a binary fraction does make sense, of course! -It is the same as a decimal fraction, but with the base of 2 instead of 10
I stand corrected I see what you are talking about is fixed point binary fractions( a relic from the past); which is not the same as a decimal fraction.
What does confuse me is where your input comes from, but that's another matter.

Last edited by dmail; 11-18-2006 at 09:57 AM.

11-18-2006, 10:01 AM   #6
Flesym
Member

Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep:
Quote:
 yeh im with you dmail. that just doesnt make any sense.
Why not? A fractional number is a number like any other and can be converted from base to base like any other. Have a look here and search for "fraction" (for example):
http://en.wikipedia.org/wiki/Binary_numeral_system

 11-18-2006, 10:04 AM #7 dmail Member   Registered: Oct 2005 Posts: 970 Rep: Why not? Well as I see it (which maybe wrong , bad day lol) a decimal fraction is a floating point not fixed point. oops sorry flesym, that wasn't even to me. /me hides
11-18-2006, 10:04 AM   #8
xhi
Senior Member

Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep:
Quote:
 Originally Posted by Flesym Why not? A fractional number is a number like any other and can be converted from base to base like any other. Have a look here and search for "fraction" (for example): http://en.wikipedia.org/wiki/Binary_numeral_system
yeh.. i had started some searching right after i posted because i had never saw a fraction like that. i was not saying you were not correct, i was just saying that it didnt make sense to me (aka im ignorant to it ).. thanks for the link.

 11-18-2006, 08:20 PM #9 randyding Member   Registered: May 2004 Posts: 552 Rep: The way I do it, you take the binary number and ignore the decimal point (or is it called a binary point?) and convert it to decimal. 10101101=173 Then you divide by 4 to get 43.25 because there is 2 digits to the right of the point. If there was 3 digits to the right of the point then divide by 8, and so on.
12-15-2006, 10:16 PM   #10
dogpatch
Member

Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 451
Blog Entries: 4

Rep:
Quote:
 Originally Posted by randyding The way I do it, you take the binary number and ignore the decimal point (or is it called a binary point?) and convert it to decimal. 10101101=173 Then you divide by 4 to get 43.25 because there is 2 digits to the right of the point. If there was 3 digits to the right of the point then divide by 8, and so on.
That method works nicely for converting to decimal, but not for octal or hex.

Since the radix of both octal and hex are powers of two, the easiest way to convert to those bases is to convert blocks of 3 binary digits for octal and of 4 binary digits for hex. Here's what i mean, using jabka's binary number 101011.01
Code:
```   binary: 101 011 . 010
to octal:   5   3 .   2

binary: 0010 1011 . 0100
to hex:    2    b .    4```

Last edited by dogpatch; 12-15-2006 at 10:19 PM.

 12-16-2006, 03:26 AM #11 jlinkels LQ Guru   Registered: Oct 2003 Location: Bonaire, Leeuwarden Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE Posts: 5,194 Rep: "Binary fractions" make sense because you can stick with integer math instead of floating point, which is (was?) an order of magnitude fasters on processors which do not (did not?) have a FPU. Especially embedded systems. Internally you would shift number to the left so they become real integers, but at some moment you have to go back to real world and shift back to world values. If you don't have the FP libraries either, you would do it like this. Maybe nowadays embedded systems come with an FPU or speed is not an issue anymore... I am from the era that programs were fed into a 6800 system using a paper tape reader. jlinkels Last edited by jlinkels; 12-16-2006 at 03:27 AM.
12-16-2006, 12:01 PM   #12
Dan04
Member

Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep:
Quote:
 Originally Posted by jabka im trieing to convert 101011.01 to octal dec and hex : so i took 101011.01 and to dec : 43 + 0 * 2^{-1} + 1* 2^{-2}= 43.25 101011.01 to oct. 53+ 0*2^-1 + 2^{-2} = 53.25 and to hex : 2b.25 but it was wrong (i try to go back by same option and i get other nombers)
You've converted the integer parts correctly, and recognized that binary 0.01 = decimal 0.25, but you need to convert that to octal and hex.

One algorithm for converting a fraction to another base is:

Code:
```while (fraction != 0) and (you haven't found as many digits as you want):
fraction *= base;
digit = int(fraction)
fraction -= digit```
For example, to convert 0.2 to base 12, you'd do:

0.2 * 12 = 2.4 -> digit = 2, fraction = 0.4
0.4 * 12 = 4.8 -> digit = 4, fraction = 0.8
0.8 * 12 = 9.6 -> digit = 9, fraction = 0.6
0.6 * 12 = 7.2 -> digit = 7, fraction = 0.2

at which point we're back were we started, so the same pattern repeats. Therefore, 0.2 decimal = 0.2497 2497 2497... dozenal.

But for converting between binary, octal, and hex, it's easiest to do it dogpatch's way.

12-16-2006, 06:05 PM   #13
dogpatch
Member

Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 451
Blog Entries: 4

Rep:
Quote:
 Originally Posted by jlinkels "Binary fractions" make sense because you can stick with integer math instead of floating point, which is (was?) an order of magnitude fasters on processors which do not (did not?) have a FPU. Especially embedded systems.
Not only that, but by using binary fractions you can avoid those nasty rounding errors associated with floating point numbers. Native Cobol programs store all numbers internally as integers, the compiler keeping track of the implied decimal point for each variable. As a result, these applications never drop or add pennies here and there. (COBOL = COmmon Business-Oriented Language)
Quote:
 Originally Posted by Dan04 But for converting between binary, octal, and hex, it's easiest to do it dogpatch's way.
Thanks for the compliment.

Last edited by dogpatch; 12-16-2006 at 06:08 PM.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post linuxi Programming 12 11-10-2006 05:21 PM Suinatsa Programming 2 10-24-2006 08:42 AM maldini1010 Programming 6 02-01-2005 04:03 PM miguetoo Programming 13 04-24-2003 09:49 PM MasterC General 8 01-21-2003 10:50 PM

LinuxQuestions.org

All times are GMT -5. The time now is 09:09 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -