0

I am trying to calculate percentages with large numbers using the exponential notation. However, when doing this on bash using bc, I get what I presume to be is overflow errors.

For example, in bash if I do the calculation:

$ echo "6139391.0 / 2.9960647E7" | bc -l
2049151.60918664203376373104

The calculation is off. It should be 0.20491516755.

How do I handle cases like this using bc or bash in general?

nobody
  • 19,814
  • 17
  • 56
  • 77
paulo
  • 141
  • 1
  • 1
  • 8
  • 1
    Yes, I inputted the two numbers into a calculator and that was the reason why I put it as why that should be the answer. – paulo Dec 17 '20 at 01:11
  • 1
    I've put it in multiple calculators and confirmed that is the number. The formula would indeed produce that output. – paulo Dec 17 '20 at 01:17
  • Apparently bc can't handle scientific notation: https://stackoverflow.com/a/12882612/4162356 – James Brown Dec 17 '20 at 01:21

2 Answers2

0

Overflow?  No; it's just ignoring the exponent; it's giving you 6139391.0 / 2.9960647.

Try

echo "6139391.0 / 2.9960647E7" | sed -E 's/([0-9.]*)E([+-]*[0-9]*)/(\1*10^\2)/g' | bc -l

to convert 2.9960647E7 to (2.9960647*10^7).

0

bc does not support scientific notation, see this answer for a workaround.

carrasco
  • 176
  • 11