How can I multiply by decimal numbers and get the result in decimal?? How can the code be modified to meet these two conditions?
Asked
Active
Viewed 130 times
-1

Amany ahmad
- 25
1 Answers
0
You could call the desk-calculator program to do the arithmetic:
v1 = 3
pi = 3.14
$ echo "Area of Circle =" $(dc --expression="$pi $v1 * $v1 * p")
Area of Circle = 28.26
In case you aren't familiar with it, dc uses reverse-polish syntax:
- stack $pi
- stack $v1
- replace the top two numbers with their product
- stack $v1
- replace the top two numbers with their product
- print the top of the stack
These are equivalent expressions:
$ echo "Area of Circle =" $(dc --expression="$pi $v1 * $v1 * p")
Area of Circle = 28.26
$ echo "Area of Circle =" $(dc --expression="$pi $v1 2 ^ * p")
Area of Circle = 28.26
$ echo "Area of Circle =" $(dc --expression="$v1 2 ^ $pi * p")
Area of Circle = 28.26

Ray Butterworth
- 1,393
-
when use this command>>> linux write: dc stack empty & pi: not found command – Amany ahmad Nov 02 '22 at 06:04
-
@Amanyahmad, did you first set the values of pi and v1? Before doing the
echo …
line, tryecho $pi
andecho $v1
to verify that they are set correctly. ¶ Also, make sure you are using the bash shell:echo $SHELL
should print "/bin/bash". – Ray Butterworth Nov 02 '22 at 13:09
man bc
. – Jos Oct 24 '22 at 11:09{}
icon (or the Ctrl+K shortcut). – Dan Oct 24 '22 at 11:28