Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
7
votes
3
answers
1366
views
dc unix : preserving initial value even after storing new value at the same array index
I'm unable to understand this example given in manpage of `dc`: $ dc 1 0:a 0Sa 2 0:a La 0;ap 1 To me answer should be 2 because: 1. `1 0:a` Here we store 1 at 0th position of array `a`. 2. `0Sa` Now we push 0 to the stack of register `a`. 3. `2 0:a` Now here again we store 2 at 0th position of array...
I'm unable to understand this example given in manpage of
dc
:
$ dc
1 0:a 0Sa 2 0:a La 0;ap
1
To me answer should be 2 because:
1. 1 0:a
Here we store 1 at 0th position of array a
.
2. 0Sa
Now we push 0 to the stack of register a
.
3. 2 0:a
Now here again we store 2 at 0th position of array a
thereby overwriting the previous 1 stored at that location.
4. La
Now we pop the 0 stored on stack of register a
and push it to main stack.
5. 0;a
Now we again push 0 to the main stack and then pop it to use as an array index and so we push the 2 stored at 0th location of array a
to the main stack.
6. p
Now we print the top of main stack which is 2. So answer should be 2.
What am I missing?
PS- I wanted to use dc
as a tag but looks like it doesn't exist and it's compulsory to use at least one tag so used debian
(my workstation OS).
rootkea
(196 rep)
Jan 5, 2016, 11:59 PM
• Last activity: Oct 17, 2024, 03:26 PM
0
votes
1
answers
100
views
Fractional exponents in dc
Is there a way to use exponents with fractional components in `dc`? Example: ``` user@box:~$ dc 9k 2 2 ^ p 4 2 2.5 ^ p Runtime warning: non-zero scale in exponent 4 ``` The man page states: ``` Arithmetic ... ^ Pops two values and exponentiates, using the first value popped as the exponent and the s...
Is there a way to use exponents with fractional components in
dc
?
Example:
user@box:~$ dc
9k
2 2 ^ p
4
2 2.5 ^ p
Runtime warning: non-zero scale in exponent
4
The man page states:
Arithmetic
...
^ Pops two values and exponentiates, using the first value popped
as the exponent and the second popped as the base. The fraction
part of the exponent is ignored. The precision value specifies
the number of fraction digits in the result.
*The fraction part of the exponent is ignored.*
It seems plain, but is there a simple way around this?
WhiteHotLoveTiger
(590 rep)
Apr 29, 2024, 01:09 PM
• Last activity: Apr 29, 2024, 06:33 PM
2
votes
1
answers
409
views
How to get the difference between numbers using dc?
Input: > printf 1\n\2\n3\n 1 2 3 Desired output: 0 1 1 Typing `printf 1\n\2\n3\n | dc -e '-p'` the output is: dc: stack empty dc: stack empty
Input:
> printf 1\n\2\n3\n
1
2
3
Desired output:
0
1
1
Typing
printf 1\n\2\n3\n | dc -e '-p'
the output is:
dc: stack empty
dc: stack empty
ncomputers
(1572 rep)
Jan 28, 2016, 09:51 PM
• Last activity: Feb 17, 2023, 05:15 PM
11
votes
1
answers
1029
views
Why does the "dc" command exit(1) when using q(uit)?
$ echo "$?" 0 $ echo 'q' | dc || echo "$?" 1 Why is using `q` to quit `dc` considered an error? (The test was run on Ubuntu 20.4.)
$ echo "$?"
0
$ echo 'q' | dc || echo "$?"
1
Why is using
q
to quit dc
considered an error?
(The test was run on Ubuntu 20.4.)
Ray Butterworth
(456 rep)
Jan 27, 2023, 12:14 AM
• Last activity: Jan 30, 2023, 12:49 PM
4
votes
1
answers
443
views
dc and standard input or heredoc with string command?
`dc` can read command from a file or/and from standard input. If I want to print user input : cat essai_dc [enter a number : ] n ? p dc essai_dc enter a number : 4 4 Now, if I try with a `heredoc` : dc [enter a number : ] > n > ? > p > EOF enter a number : dc: stack empty I get the same with standar...
dc
can read command from a file or/and from standard input.
If I want to print user input :
cat essai_dc
[enter a number : ]
n
?
p
dc essai_dc
enter a number : 4
4
Now, if I try with a heredoc
:
dc [enter a number : ]
> n
> ?
> p
> EOF
enter a number : dc: stack empty
I get the same with standard input :
cat essai_dc | dc
enter a number : dc: stack empty
The command ? get the p
and execute it but the stack is empty.
Is it possible to get it to work (tell dc
to wait for the input)
ctac_
(1968 rep)
Apr 6, 2021, 01:45 PM
• Last activity: Apr 6, 2021, 03:59 PM
0
votes
1
answers
59
views
executing 2 scripts with connection
I've been given some challenge: there is an application that provides me some postfix expression and expects to get a solution within 5 seconds. If there is no solution, it quits, otherwise, it provides additional expression. The interface is like follow: etc. If there was only single expression I'v...
I've been given some challenge: there is an application that provides me some postfix expression and expects to get a solution within 5 seconds.
If there is no solution, it quits, otherwise, it provides additional expression.
The interface is like follow:
etc.
If there was only single expression I've thought to use
tail -1
command to get the last line to get the expression and use the dc
command to evaluate it.
However, as there are unknown number of expressions (so far), I can't use it and have to do some loop till the string which is read is not starting with a number (i.e expression).
Therefore I've though to use 2 scripts:
1. start the application always send text output to file1 and execute it in the background (script 1)
2. read the last row of a script the file1 (script 2), evaluate a solution and sent it to file2 (script 2)
3. read solution from file2, delete file2 (for next solutions), bring the application to foreground, send a solution and then send the application to background (script 1)
However, it seems to be little bit complicate for me.
Any suggestions how to simplify the procedure to solve it?
user1977050
(409 rep)
Jun 21, 2017, 08:33 AM
• Last activity: Dec 30, 2018, 04:35 AM
13
votes
3
answers
2257
views
Losing precision with dc
I want to use `dc` to handle some base 16 numbers with hexadecimal points but I'm running into precision problems. For example, below I'm multiplying `F423F.FD` by `100`, both hex. The expected answer is `F423FFD`, instead it is giving `F423FFA.E1`, close but not accurate enough even after rounding....
I want to use
dc
to handle some base 16 numbers with hexadecimal points but I'm running into precision problems. For example, below I'm multiplying F423F.FD
by 100
, both hex. The expected answer is F423FFD
, instead it is giving F423FFA.E1
, close but not accurate enough even after rounding.
$ dc
16 d i o F423F.FD 100 * p
F423FFA.E1
I read that dc
was an unlimited precision calculator, and this is not a large number by any means. Is there something I'm doing wrong?
Thank for your answers. Given the issues with dc
, I bit the bullet and wrote my own parser for real numbers in other bases. If anyone is interested in the code, I can post it here.
Yimin Rong
(953 rep)
Jul 17, 2018, 04:26 PM
• Last activity: Jul 19, 2018, 07:35 PM
2
votes
2
answers
316
views
How come dc print splits up long number
When running the `dc` utility, if I input a very large number, say: 999999999999999999999999999999999999999999999999999999999999999999999999 When I print it out using `p`, I get: 999999999999999999999999999999999999999999999999999999999999999999999\ 999 Why does it split the output after 69 digits?
When running the
dc
utility, if I input a very large number, say:
999999999999999999999999999999999999999999999999999999999999999999999999
When I print it out using p
, I get:
999999999999999999999999999999999999999999999999999999999999999999999\
999
Why does it split the output after 69 digits?
MarksCode
(135 rep)
Feb 7, 2018, 11:19 PM
• Last activity: Feb 8, 2018, 12:14 AM
1
votes
1
answers
266
views
How to capture user input in dc?
I'd like to handle user input in `dc`. For `?`, `info dc` indicates: '?' Reads a line from the terminal and executes it. This command allows a macro to request input from the user. However, if I type this command sequence to square a number, followed by my input `9`: ?d*p 9 I get: dc: stack empty dc...
I'd like to handle user input in
dc
.
For ?
, info dc
indicates:
'?'
Reads a line from the terminal and executes it. This command
allows a macro to request input from the user.
However, if I type this command sequence to square a number, followed by my input 9
:
?d*p
9
I get:
dc: stack empty
dc: stack empty
dc: stack empty
Am I misinterpreting what ?
does?
Yimin Rong
(953 rep)
Jul 29, 2017, 12:42 PM
• Last activity: Jul 29, 2017, 02:07 PM
0
votes
1
answers
140
views
How can I supress GNU dc warnings?
When some invalid or undefined commands are executed in dc, it usually just prints a warning. Example: cp dc: stack empty This is nice most of the time, however, there are cases where this is annoying as the desired action actually works. Consider this example: 5 6+sИlИp dc: 0230 unimplemented dc: 0...
When some invalid or undefined commands are executed in dc, it usually just prints a warning. Example:
cp
dc: stack empty
This is nice most of the time, however, there are cases where this is annoying as the desired action actually works. Consider this example:
5 6+sИlИp
dc: 0230 unimplemented
dc: 0230 unimplemented
11
See? Even if I try to use another register than those in the manual it still works. ("officially" 256, but how are you even supposed to use the NUL byte?)
Even if perhaps not always a good idea, I would like dc to now show me such warnings in some scripts.
How can I make dc not print warnings?
SE - stop firing the good guys
(245 rep)
Apr 15, 2017, 04:57 PM
• Last activity: Apr 15, 2017, 06:19 PM
Showing page 1 of 10 total questions