LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 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: 145

Rep: Reputation: 28
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, 09:17 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try this:

Code:
$ echo 'scale=20; print 355/113, "\n"' | bc
 
Old 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: 145

Original Poster
Rep: Reputation: 28
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, 09:51 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

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

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 02:22 PM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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, 08:06 PM   #7
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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, 08:54 PM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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, 06:01 PM   #9
bill_from_tampa
LQ Newbie
 
Registered: Jan 2005
Location: Tampa
Distribution: Debian testing
Posts: 26

Rep: Reputation: 4
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



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 09:36 AM
display images with refreshing in fraction of second mahi04 Ubuntu 2 01-06-2010 12:39 AM
Is there any way to produce screenshots? smitty654 Linux - Software 3 01-19-2008 09:02 AM
C++, issues converting decimal to fraction alpha_gamma Programming 7 06-04-2007 07:15 PM
Why is only a fraction of my new second hardrive detected? quixotic2020 Linux - Hardware 10 08-04-2004 11:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration