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 
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto 
Site FAQ 
Sitemap 
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQrelated cookies.

Introduction to Linux  A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free. 


10012013, 09:11 AM

#1

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?



10012013, 09:17 AM

#2

Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,569

Try this:
Code:
$ echo 'scale=20; print 355/113, "\n"'  bc



10012013, 09:25 AM

#3

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
Am I asking this the right way?



10012013, 09:51 AM

#4

Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,569




10012013, 09:51 AM

#5

Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,452

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.0E10
}
{
x = sprintf("%d",$0) + 0
N1 = 1
D1 = 0
N = x
D = 1
while ( $0x > eps*D*D ) {
$0 = 1/($0x)
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.

10012013, 02:22 PM

#6

Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 615

Hi.
There is another arbitrary precision calculator called calc (in Debianlike 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.

10012013, 08:06 PM

#7

Member
Registered: Jan 2011
Distribution: slackware_64 14.0
Posts: 506
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

I hope not!


1 members found this post helpful.

10012013, 08:54 PM

#8

Member
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul18,2006 on Intel 820 DC
Posts: 600
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



10022013, 06:01 PM

#9

LQ Newbie
Registered: Jan 2005
Location: Tampa
Distribution: Debian testing
Posts: 21
Rep:

emacs calc mode can do fractions (Mx calc).
Per the manual: "The c F (calcfraction) [pfrac] command converts a floatingpoint 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 nonpositive integer, it specifies how many digits fewer than the current precision to use. If the tolerance is a floatingpoint number, the fraction is correct to within that absolute amount."



Thread Tools 
Search this Thread 


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off



All times are GMT 5. The time now is 08:51 PM.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.

Latest Threads
LQ News

