Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
howto:calculate-dc [2015/08/09 03:50] bill_thomson |
howto:calculate-dc [2018/06/21 23:36] izxle added missing <nowiki> </nowiki> |
||
---|---|---|---|
Line 59: | Line 59: | ||
''dc'' is a calulator with abitrary precision, by default this precision is 0. | ''dc'' is a calulator with abitrary precision, by default this precision is 0. | ||
- | thus ''dc <<< "5 4/p"'' prints "1". | + | thus ''<nowiki>dc <<< "5 4/p"</nowiki>'' prints "1". |
We can increase the precision using the ''k'' command. It pops the value at the top of the stack | We can increase the precision using the ''k'' command. It pops the value at the top of the stack | ||
Line 160: | Line 160: | ||
</code> | </code> | ||
- | The above snippet use newlines to embed comments, but it doesn't | + | The above snippet uses newlines to embed comments, but it doesn't |
really matter, you can use ''echo '12sa10la+p'| dc'', with the same results. | really matter, you can use ''echo '12sa10la+p'| dc'', with the same results. | ||
- | The register can contain more that just a value, **each register is a | + | The register can contain more than just a value, **each register is a |
stack on its own**. | stack on its own**. | ||
Line 181: | Line 181: | ||
===== Macros ===== | ===== Macros ===== | ||
- | ''dc'' lets you push aribtrary strings on the stack when the strings are enclosed in ''[]''. | + | ''dc'' lets you push arbitrary strings on the stack when the strings are enclosed in ''[]''. |
You can print it with ''p'': ''<nowiki>dc <<< '[Hello World!]p'</nowiki>'' and you can | You can print it with ''p'': ''<nowiki>dc <<< '[Hello World!]p'</nowiki>'' and you can | ||
evalute it with x: ''<nowiki>dc <<< '[1 2+]xp'</nowiki>''. | evalute it with x: ''<nowiki>dc <<< '[1 2+]xp'</nowiki>''. | ||
This is not that interesting until combined with registers. | This is not that interesting until combined with registers. | ||
- | First let's say we want the to calculate the square of a number | + | First, let's say we want to calculate the square of a number |
(don't forget to include ''f'' if you get lost!): | (don't forget to include ''f'' if you get lost!): | ||
<code> | <code> | ||
Line 196: | Line 196: | ||
</code> | </code> | ||
- | Now we have several cube to calculate, we could use ''<nowiki>dd**</nowiki>'' several times, or | + | Now we have several cubes to calculate, we could use ''<nowiki>dd**</nowiki>'' several times, or |
use a macro. | use a macro. | ||
Line 226: | Line 226: | ||
</code> | </code> | ||
- | Some ''dc'' have ''>R <R =R'', GNU ''dc'' had some more, check your manual. Not | + | Some ''dc'' have ''>R <R =R'', GNU ''dc'' had some more, check your manual. Note |
- | that the test "consumes" its operands the 2 first elements are popped | + | that the test "consumes" its operands: the 2 first elements are popped |
- | off the stack (you can verify that ''dc <<< "[f]sR 2 1 >R 1 2 >R f"'' | + | off the stack (you can verify that ''<nowiki>dc <<< "[f]sR 2 1 >R 1 2 >R f"</nowiki>'' |
doesn't print anything) | doesn't print anything) | ||
Have you noticed how we can //include// a macro (string) in a macro? and as ''dc'' | Have you noticed how we can //include// a macro (string) in a macro? and as ''dc'' | ||
- | relies on a stack we can in fact use the macro recursively (have your | + | relies on a stack we can, in fact, use the macro recursively (have your |
favorite control-c key combo ready ;)) : | favorite control-c key combo ready ;)) : | ||
Line 252: | Line 252: | ||
1 - # we decrement the index by one | 1 - # we decrement the index by one | ||
si # store decremented index (i=i-1) | si # store decremented index (i=i-1) | ||
- | 0 li >L # if i > 0 the execute L | + | 0 li >L # if i > 0 then execute L |
] sL # store our macro with the name L | ] sL # store our macro with the name L | ||