Working with complex numbers

Working with complex numbers

How to:

Bash doesn’t support complex numbers natively. You’ll often use an external tool like bc with its -l option. Here’s how you crunch complex numbers in bash:

echo "sqrt(-1)" | bc -l




echo "(-1 + -1i) * (4 + 3i)" | bc -l



Deep Dive

Complex numbers have been around since the 16th century, but scripting languages like Bash are not primed for mathematical computations like complex numbers out of the box. That’s why bc or other tools like awk often come into play. Some alternative languages for working with complex numbers are Python with its cmath module and MATLAB, which are both built for more advanced mathematical functions. As for Bash, it’s all about leveraging tools - bc uses the lowercase ‘i’ to represent the imaginary unit and supports basic operations like addition, subtraction, multiplication, and division.

See Also