FACTOID # 77: Moldova has one of the smallest artillery forces in Europe, and the highest rate in the world of death by powered lawnmower. Coincidence? Surely not.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Dc (Unix)

From the GNU Project dc man page: 'Dc is a reverse-polish desk calculator which supports unlimited precision arithmetic'. It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax. Traditionally, the more user-friendly (with its infix notation) bc calculator program was implemented on top of dc, although more modern implementations are no longer related. It has been suggested that this article or section be merged with Unix-like. ... The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a standardized imperative computer programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the... Infix notation is the arithmetic formula notation known to most people, in which operators are written infix-style between the operands they act on. ... bc is an arbitrary precision calculator language with syntax similar to the C programming language. ...


This article provides some examples in an attempt to give a flavour of the language; for a complete list of commands and syntax you should consult a manual page for your implementation.


Unlike bc, dc is based on reverse-Polish notation, which means it interprets mathematical statements in a stack-based fashion. bc is an arbitrary precision calculator language with syntax similar to the C programming language. ... Reverse Polish notation (RPN) , also known as postfix notation, is an arithmetic formula notation, derived from the Polish notation introduced in 1920 by the Polish mathematician Jan Łukasiewicz. ... It has been suggested that Stack-Based Memory Allocation be merged into this article or section. ...


For example, to multiply four and five in dc (note that most of the whitespace is optional):

 4 5 * p 

This translates into "push four and five onto the stack, then, with the multiplication operator, pop two elements from the stack, multiply them and push the result back on the stack." Then the 'p' command is used to examine (print out to the screen) the first element on the stack.


To evaluate (12 + 3^4)/11-22:

 12 3 4 ^ + 11 / 22 - p 

In addition to these basic arithmetic and stack operations, dc includes support for macros, conditionals and storing of results for later retrieval; unfortunately the syntax is terse and complex programs in dc tend to be very hard to read. A macro in computer science is an abstraction, whereby a certain textual pattern is replaced according to a defined set of rules. ...


The mechanism underlying macros and conditionals is the register, which in dc is a storage location with a single character name which can be stored to and retrieved from: 'sc' pops the top of the stack and stores it in register c, and 'lc' pushes the value of register c onto the stack. For example:

 3 sc 4 lc * p 

Registers can also be treated as secondary stacks, so values can be pushed and popped between them and the main stack.


Macros are then implemented by allowing registers and stack entries to be strings as well as numbers. A string can be printed, but it can also be executed (ie processed as a sequence of dc commands). So for instance we can store a macro to add one and then multiply by 2 into register m:

 [1 + 2 *] sm 

and then (using the 'x' command which executes the top of the stack) we can use it like this:

 3 lm x p 

Finally, we can use this macro mechanism to provide conditionals. The command '=r' will pop two values from the stack, and execute the macro stored in register r only if they are equal. So this will print the string 'equal' only if the top of the stack is equal to 5:

 [[equal]p] sm 5 =m 

Looping is then possible by defining a macro which (conditionally) reinvokes itself.


As an example of a relatively simple program in dc, this command:

 dc -e '[[Enter a number (metres), or 0 to exit]psj]sh[q]sz[lhx?d0=z10k39.370079*.5+0k12~1/rn[ feet ]Pn[ inches]P10Pdx]dx' 

will convert distances from metres to feet and inches; the bulk of it is concerned with prompting for input, printing output in a suitable format and looping round to convert another number.


As an example of implementation Euclidean algorithm for search GCD: The Euclidean algorithm (also called Euclids algorithm) is an algorithm to determine the greatest common divisor (gcd) of two integers. ... In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf) of two integers which are not both zero is the largest integer that divides both numbers. ...

 dc -e '?[dSarLa%d0<a]dsax+p' # shortest dc -e '[a=]P?[b=]P?[dSarLa%d0<a]dsax+[GCD:]Pp' # more confort version 

References


  Results from FactBites:
 
Dc (Unix) - Wikipedia, the free encyclopedia (607 words)
It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax.
In addition to these basic arithmetic and stack operations, dc includes support for macros, conditionals and storing of results for later retrieval; unfortunately the syntax is terse and complex programs in dc tend to be very hard to read.
The mechanism underlying macros and conditionals is the register, which in dc is a storage location with a single character name which can be stored to and retrieved from: 'sc' pops the top of the stack and stores it in register c, and 'lc' pushes the value of register c onto the stack.
BIGpedia - Unix - Encyclopedia and Dictionary Online (2756 words)
UNIX is a portable, multi-tasking and multi-user computer operating system originally developed by a group of AT&T Bell Labs employees including Ken Thompson, Dennis Ritchie and Douglas McIlroy.
By 1993 most of the commercial vendors of UNIX had changed their commercial variants of UNIX to be based upon SVR4, and many BSD features were added on top.
The UNIX trademark and the certification rights were sold to the X/Open Consortium, which was an industry group to define a "UNIX Standard".
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.