MathCore includes a built-in ASCII plotter that renders any single-variable mathematical expression as a text-based chart, directly in your terminal. This is useful for quick visual sanity-checks during development, exploring function behavior, and displaying results in environments where a graphical UI is unavailable — such as CI pipelines, SSH sessions, or embedded debug consoles.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
MathCore::plot_ascii
Parameters
| Parameter | Type | Description |
|---|---|---|
expression | &str | The mathematical expression to plot, e.g. "sin(x)". |
var | &str | The name of the x-axis variable used in the expression. |
x_min | f64 | The left bound of the x range. |
x_max | f64 | The right bound of the x range. |
width | usize | Number of character columns in the output grid. Controls x-axis resolution. |
height | usize | Number of character rows in the output grid. Controls y-axis resolution. |
Result<String, MathError> — the complete ASCII plot as a multi-line string, ready to be passed to println!. Returns Err if the expression cannot be parsed or if no finite points can be computed in the given range.
Basic Example
Output Format
The returned string has the following structure:- Header line — shows the expression and the x range:
- y-range line — the automatically-determined minimum and maximum y values:
- Blank line — separates the header from the grid.
- The plot grid — a rectangular character grid where:
|is drawn along the leftmost column as the y-axis.-is drawn along the bottom row as the x-axis.+marks the bottom-left corner where the axes meet.*marks each evaluated point of the function.
The y-axis range is computed automatically from the actual evaluated points — you do not need to specify it. The grid maps the minimum y value to the bottom row and the maximum y value to the top row.
More Examples
Parabola
x=0, y=0) appears at the bottom of the grid, and the two arms curve upward toward the corners.
Cosine Wave
x_min=0 and x_max=6.28 (approximately tau), this plots a full period of the cosine function, starting at its maximum value of 1.
Combining with the Demo
Theexamples/demo.rs file uses plot_ascii with a 40-column, 15-row grid:
Resolution Tips
A reasonable starting point for most terminal windows iswidth=60, height=20. For detailed inspection of a narrow feature (like a sharp peak), zoom in by tightening x_min and x_max and increasing width.
Handling Non-Finite Points
plot_ascii evaluates the expression for each x sample and silently skips any point that produces a non-finite result — that is, f64::INFINITY, f64::NEG_INFINITY, or NaN. This means functions with poles or discontinuities plot cleanly without panicking:
