Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Dc (computer program)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Basic operations== To multiply four and five in dc (note that most of the [[Whitespace character|whitespace]] is optional): <syntaxhighlight lang="console"> $ cat << EOF > cal.txt 4 5 * p EOF $ dc cal.txt 20 $ </syntaxhighlight> The results are also available from the commands: <syntaxhighlight lang="console"> $ echo "4 5 * p" | dc </syntaxhighlight> or <syntaxhighlight lang="console"> $ dc - 4 5*pq 20 $ dc 4 5 * p 20 q $ dc -e '4 5 * p' </syntaxhighlight> 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 onto the stack." Then the <code>p</code> command is used to examine (print out to the screen) the top element on the stack. The <code>q</code> command quits the invoked instance of dc. Note that numbers must be spaced from each other even as some operators need not be. The [[arithmetic precision]] is changed with the command <code>k</code>, which sets the number of fractional digits (the number of digits following the [[radix point|point]]) to be used for arithmetic operations. Since the default precision is zero, this sequence of commands produces <code>0</code> as a result: <pre> 2 3 / p </pre> By adjusting the precision with <code>k</code>, an arbitrary number of decimal places can be produced. This command sequence outputs <code>.66666</code>. <pre> 5 k 2 3 / p </pre> To evaluate <math>\sqrt{\left(12 + \left(-3\right)^4\right)\over11}-22</math>: (<code>v</code> computes the square root of the top of the stack and <code>_</code> is used to input a negative number): <pre> 12 _3 4 ^ + 11 / v 22 - p </pre> To swap the top two elements of the stack, use the <code>r</code> command. To duplicate the top element, use the <code>d</code> command.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)