ProgrammingThis 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.

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 LQ-related 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.

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?

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.

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).

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."

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