I'm having a difficulty with using negative exponents in the program bc
in Bash. If I execute echo "2*1.86929*10^05" | bc
, I get a result of 373858.00000
while if I execute echo "2*1.86929*10^-05" | bc
, I get a result only of 0
. How can I get better accuracy when using negative exponents?
Asked
Active
Viewed 3,414 times
4

user000001
- 32,226
- 12
- 81
- 108

d3pd
- 7,935
- 24
- 76
- 127
2 Answers
6
By default, the output of bc
is rounded to an integer. To keep the decimal part of the result, use bc -l
, like this:
$ echo "2*1.86929*10^-05" | bc -l
.00003738580000000000

user000001
- 32,226
- 12
- 81
- 108
-
This works well. Thank you very much for your assistance on this. – d3pd Mar 04 '13 at 15:09
-
1`bc -l` should be the default for `bc`, in my opinion. – Lonnie Best Nov 02 '21 at 19:03
3
You need to set the value of "scale" - so
scale=50
2*1.86929*10^-05
Gives .00003738580000000000000000000000000000000000000000

DrC
- 7,528
- 1
- 22
- 37
-
Thanks for your help on that. I got it working in the following way: ```echo "scale=50; 2*1.86929*10^-05" | bc```. – d3pd Oct 03 '14 at 11:31