Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Is it possible to produce a fraction with bc?
 User Name Remember Me? Password
 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.

Notices

 10-01-2013, 09:11 AM #1 Predatorian Member   Registered: Mar 2008 Location: currently, where ever the army takes me Distribution: Debian Lenny/Ubuntu or Arch Linux Posts: 138 Rep: Is it possible to produce a fraction with bc? I know bc is an arbitrary calculator, but I don't know what that means other than it produces decimals. I can hand jam a decimal until is a fraction, but is there a way to make bc convert the decimal to a fraction, or should I look else where?
 10-01-2013, 09:17 AM #2 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 1,599 Rep: Try this: Code: `\$ echo 'scale=20; print 355/113, "\n"' | bc`
 10-01-2013, 09:25 AM #3 Predatorian Member   Registered: Mar 2008 Location: currently, where ever the army takes me Distribution: Debian Lenny/Ubuntu or Arch Linux Posts: 138 Original Poster Rep: That works, however, it's still in the form of a decimal. Maybe I'm expecting something that bc can't do? Code: ```\$ echo 'scale=20; print 355/113, "\n"' | bc 3.14159292035398230088``` is there anyway to get it to format it so that it displays as Code: `22/7` Am I asking this the right way?
 10-01-2013, 09:51 AM #4 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 1,599 Rep: I don't think that is possible. Try wolframalfa. http://www.wolframalpha.com/input/?i=3%2F7%2B7%2F3
 10-01-2013, 09:51 AM #5 colucix Moderator   Registered: Sep 2003 Location: Bologna Distribution: CentOS 6.5 OpenSuSE 12.3 Posts: 10,458 Rep: I don't think there is a function to easily convert a decimal to a fraction in bc. Here is an algorithm written in awk: Code: ```BEGIN { eps = 1.0E-10 } { x = sprintf("%d",\$0) + 0 N1 = 1 D1 = 0 N = x D = 1 while ( \$0-x > eps*D*D ) { \$0 = 1/(\$0-x) x = sprintf("%d",\$0) + 0 N2 = N1 N1 = N D2 = D1 D1 = D N = N2 + x*N1 D = D2 + x*D1 } print N "/" D }``` where the decimal to convert is \$0. I'm not an expert of bc programming language, but since it has while loops, it should be easy to convert this code in bc or eventually you can use it directly with awk! Hope this helps. 1 members found this post helpful.
 10-01-2013, 02:22 PM #6 firstfire Member   Registered: Mar 2006 Location: Ekaterinburg, Russia Distribution: Debian, Ubuntu Posts: 615 Rep: Hi. There is another arbitrary precision calculator called calc (in Debian-like distros the package is called apcalc). Rational numbers is a built in type in calc (all real numbers are represented by numerator and denominator internally). Here is a sample session: Code: ```\$ calc ; x=1.5 ; x 1.5 ; num(x) 3 ; den(x) 2 ; exp(x) 4.4816890703380648226 ; num(exp(x)) 22408445351690324113 ; den(exp(x)) 5000000000000000000 ; 22408445351690324113/5000000000000000000 4.4816890703380648226``` 1 members found this post helpful.
10-01-2013, 08:06 PM   #7
psionl0
Member

Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 510
Blog Entries: 2

Rep:
Quote:
 Originally Posted by Predatorian Code: ```\$ echo 'scale=20; print 355/113, "\n"' | bc 3.14159292035398230088``` is there anyway to get it to format it so that it displays as Code: `22/7`
I hope not!

1 members found this post helpful.
 10-01-2013, 08:54 PM #8 AnanthaP Member   Registered: Jul 2004 Location: Chennai, India Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC Posts: 607 Rep: If you want 7 in the denominator for a result of 3.14159292035398230088, then 21.99115044247787610616 and not 22 gazinta the numerator. Why 22/7? Why not 44/14? OK
 10-02-2013, 06:01 PM #9 bill_from_tampa LQ Newbie   Registered: Jan 2005 Location: Tampa Distribution: Debian testing Posts: 22 Rep: emacs calc mode can do fractions (M-x calc). Per the manual: "The c F (calc-fraction) [pfrac] command converts a floating-point number into a fractional approximation. By default, it produces a fraction whose decimal representation is the same as the input number, to within the current precision. You can also give a numeric prefix argument to specify a tolerance, either directly, or, if the prefix argument is zero, by using the number on top of the stack as the tolerance. If the tolerance is a positive integer, the fraction is correct to within that many significant figures. If the tolerance is a non-positive integer, it specifies how many digits fewer than the current precision to use. If the tolerance is a floating-point number, the fraction is correct to within that absolute amount."

 Thread Tools Search this Thread Search this Thread: Advanced Search

 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 vdx Programming 4 06-10-2011 09:36 AM mahi04 Ubuntu 2 01-06-2010 12:39 AM smitty654 Linux - Software 3 01-19-2008 09:02 AM alpha_gamma Programming 7 06-04-2007 07:15 PM quixotic2020 Linux - Hardware 10 08-04-2004 11:27 AM

All times are GMT -5. The time now is 08:01 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -
 My LQ
 Write for LQ LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
 Syndicate Latest Threads   LQ News Twitter: @linuxquestions identi.ca: @linuxquestions Facebook: linuxquestions Google+: linuxquestions