LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
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

Reply
 
Search this Thread
Old 10-01-2013, 10:11 AM   #1
Predatorian
Member
 
Registered: Mar 2008
Location: currently, where ever the army takes me
Distribution: Debian Lenny/Ubuntu or Arch Linux
Posts: 143

Rep: Reputation: 26
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?
 
Old 10-01-2013, 10:17 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,883

Rep: Reputation: 520Reputation: 520Reputation: 520Reputation: 520Reputation: 520Reputation: 520
Try this:

Code:
$ echo 'scale=20; print 355/113, "\n"' | bc
 
Old 10-01-2013, 10:25 AM   #3
Predatorian
Member
 
Registered: Mar 2008
Location: currently, where ever the army takes me
Distribution: Debian Lenny/Ubuntu or Arch Linux
Posts: 143

Original Poster
Rep: Reputation: 26
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?
 
Old 10-01-2013, 10:51 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,883

Rep: Reputation: 520Reputation: 520Reputation: 520Reputation: 520Reputation: 520Reputation: 520
I don't think that is possible. Try wolframalfa.
http://www.wolframalpha.com/input/?i=3%2F7%2B7%2F3
 
Old 10-01-2013, 10:51 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
Old 10-01-2013, 03:22 PM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
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.
Old 10-01-2013, 09:06 PM   #7
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 606
Blog Entries: 2

Rep: Reputation: 92
Quote:
Originally Posted by Predatorian View Post
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.
Old 10-01-2013, 09:54 PM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 650

Rep: Reputation: 160Reputation: 160
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
 
Old 10-02-2013, 07:01 PM   #9
bill_from_tampa
LQ Newbie
 
Registered: Jan 2005
Location: Tampa
Distribution: Debian testing
Posts: 23

Rep: Reputation: 3
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."
 
  


Reply


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to get epoch time in fraction vdx Programming 4 06-10-2011 10:36 AM
display images with refreshing in fraction of second mahi04 Ubuntu 2 01-06-2010 01:39 AM
Is there any way to produce screenshots? smitty654 Linux - Software 3 01-19-2008 10:02 AM
C++, issues converting decimal to fraction alpha_gamma Programming 7 06-04-2007 08:15 PM
Why is only a fraction of my new second hardrive detected? quixotic2020 Linux - Hardware 10 08-04-2004 12:27 PM


All times are GMT -5. The time now is 05:02 AM.

Main Menu
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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration