DEV Community

ember_seed
ember_seed

Posted on

Why I Built a Scientific Calculator in Pure C for Terminal Environments

For most people, opening a calculator means launching a GUI application or typing a quick expression into Python.

For me, neither felt ideal.

A large part of my work happens inside terminals: remote servers, SSH sessions, minimal Linux installations, recovery environments, virtual machines, and development systems where a terminal is always available but a graphical environment is not guaranteed.

I wanted a scientific calculator that felt native to that environment.

Not a Python script.

Not a desktop application.

Not something that required package installation, runtime environments, or external dependencies.

Just a single executable that could be compiled anywhere and run immediately.

So I built one.

Why Not Python?

Before someone asks: Python is excellent.

I use Python regularly and have no problem with it.

The goal of this project was never to replace Python.

The goal was to build a tool with different trade-offs.

With Python, even a simple calculation requires starting an interpreter:

python -c "print(2+2)"

That is perfectly reasonable.

But in terminal-centric workflows, I wanted something that behaves more like a traditional Unix utility:

calculator

Instant startup.

No runtime.

No dependency management.

No interpreter.

No virtual environments.

Just a native executable.

Why Terminal First?

Modern software increasingly assumes graphical environments.

Yet terminals remain one of the most important interfaces in computing.

System administrators live in terminals.

Embedded developers use terminals.

Linux users spend hours inside terminals.

SSH sessions are terminals.

Containers are terminals.

Rescue environments are terminals.

Many scientific calculators either require a GUI or offer only basic arithmetic.

I wanted something that remained entirely inside the command line while still providing advanced mathematical functionality.

Features Beyond Basic Arithmetic

The project started as a simple calculator.

It gradually evolved into something closer to a lightweight mathematical toolkit.

Expression Parsing

The calculator supports full mathematical expressions:

2+3*4
sqrt(16)
sin(pi/2)

It also supports implicit multiplication:

2pi
3(2+4)
(x+1)(x-1)

which feels natural when entering mathematical expressions.

Scientific Functions

Built-in support includes:

Trigonometric functions
Inverse trigonometric functions
Degree-based variants
Logarithms
Exponentials
Roots
Factorials
Rounding functions

Examples:

sin(pi/2)
sind(90)
sqrt(25)
log2(1024)
fact(5)
Number Base Support

Working close to hardware often means switching between number systems.

The calculator supports:

0b1010
0o77
0xFF

as inputs and can convert values between bases.

Equation Solving

Instead of only evaluating expressions, the calculator can solve equations.

Supported modes include:

Quadratic equations
Cubic equations
General numerical root finding

Example:

x^2 - 2 = 0

which converges to:

1.414213562...

using Newton-Raphson iteration.

Numerical Differentiation

Given any expression containing x, the calculator can estimate its derivative numerically.

Example:

f(x) = x^2

at:

x = 2

returns:

f'(2) = 4
ASCII Graph Plotting

This is one of my favorite features.

Graphing normally requires graphical libraries.

Instead, this calculator renders graphs directly in the terminal using ASCII characters.

No GUI.

No plotting framework.

Just text.

Example:

y = x^2

can be visualized directly inside a terminal window.

Matrix Operations

The calculator also includes support for 2ร—2 matrix operations:

Addition
Multiplication
Determinant
Inverse

which makes it useful for basic linear algebra tasks.

Why a Single File?

One criticism I expect is:

"Why is everything in one file?"

Because simplicity was a design goal.

This project is intentionally small enough that opening a single source file provides a complete view of the entire program.

There is no build system.

No project hierarchy.

No dependency graph.

No package manager.

No generated code.

A developer can open the source, scroll through it, understand it, modify it, and compile it.

That simplicity has value.

Especially for educational projects.

Especially for C.

What I Learned

The most interesting part of this project wasn't writing mathematical functions.

It was building the expression parser.

Supporting features like:

2pi
3(2+4)
root(8,3)
logbase(100,10)

required much more thought than I initially expected.

The project also reinforced something I appreciate about C:

For small system-oriented utilities, C remains remarkably effective.

The language gives direct control, fast execution, minimal overhead, and broad portability.

Future Plans

There are still many things I'd like to improve:

Better matrix support
More graphing capabilities
Additional numerical methods
Persistent history
Improved expression parsing

But even in its current form, the calculator already serves the purpose that motivated the project:

Providing a capable scientific calculator that feels at home in terminal environments.

Repository

If you're interested in terminal tools, C programming, parsers, or numerical methods, I'd love to hear your feedback and ideas for future improvements.

GitHub logo EmberSeed / ADVANCED-TERMINAL-CALCULATOR

**Advanced Terminal Calculator** โ€“ A powerful CLI scientific calculator with trig, logs, roots, base conversion, equation solver, differentiation, matrix ops, ASCII plotting, and history. No GUI, just pure math in your terminal.

๐Ÿงฎ Advanced Terminal Calculator

A zero-dependency scientific calculator written in C.

This project combines an expression parser, scientific functions, numerical methods, equation solving, graph plotting, and basic matrix operations into a single self-contained terminal application.

The entire implementation is contained in a single source file and depends only on the C standard library and libm.


โœจ Features

1. Arithmetic & Expressions

Supported operators:

  • +
  • -
  • *
  • /
  • %
  • ^

Features:

  • Full operator precedence
  • Parentheses
  • Implicit multiplication
  • Right-associative exponentiation

Examples:

2+3*4          -> 14
(2+3)*4        -> 20
2pi            -> 6.28318...
3(2+4)         -> 18
2^3^2          -> 512
10%3           -> 1

2. Trigonometric Functions

Radians:

  • sin
  • cos
  • tan
  • asin
  • acos
  • atan

Degrees:

  • sind
  • cosd
  • tand
  • asind
  • acosd
  • atand

Examples:

sin(pi/2)      -> 1
cos(0)         -> 1
tan(pi/4)      -> 1

sind(90)       -> 1
cosd(180)      -> -1
tand(45)       -> 1

3. Logarithmic & Exponential Functions

Supported:

  • ln
  • log
  • log10
  • log2
  • logbase(x,base)
  • exp

Examples:

ln(e)              -> 1
โ€ฆ

Top comments (0)