简体   繁体   中英

using negative exponents with bc in Bash

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 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 echo "2*1.86929*10^-05" | bc , I get a result only of 0 . How can I get better accuracy when using negative exponents?

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

You need to set the value of "scale" - so

scale=50
2*1.86929*10^-05

Gives .00003738580000000000000000000000000000000000000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM