tally/engine/src/evaluator.zig

1664 lines
66 KiB
Zig

//! AST evaluator for Tally.
//!
//! Walks an AST and produces a Value. In standard mode, all computations
//! use f64 floating-point arithmetic. In programmer mode, integer operations
//! are exact (masked to bit width). The evaluator uses an Environment for
//! variable storage, history, and configuration.
const std = @import("std");
const math = std.math;
const Allocator = std.mem.Allocator;
const ast = @import("ast.zig");
const Expr = ast.Expr;
const BinaryOp = ast.BinaryOp;
const Integer = @import("Integer.zig");
/// What standard-mode evaluation can fail with.
///
/// Its own name and range errors, plus everything its dependencies can raise. The
/// `||` chain is the honest signature: financial functions are callable from an
/// expression, so `ConvergenceFailure` really can come out of `evalString`, while a
/// bare `parser.parse` cannot produce it and no longer claims to.
pub const Error = error{
UnknownFunction,
UnknownVariable,
DomainError,
Overflow,
} || parser_mod.Error || number_mod.Error || bitwise.Error || financial.Error;
const parser_mod = @import("parser.zig");
const Parser = parser_mod.Parser;
const number_mod = @import("number.zig");
const Number = number_mod.Number;
const bitwise = @import("bitwise.zig");
const financial = @import("financial.zig");
/// Evaluation environment holding variables and the last answer.
///
/// Variables and `Ans` are stored as `Number`, so an assignment keeps whatever
/// exactness its expression had: `X = 0.1` stores exactly one tenth rather than
/// a binary approximation of it.
///
/// It holds no mode and no programmer configuration. It used to hold both and read
/// neither: the caller chooses between `evalString` and `evalProgrammerString`, and
/// standard mode's integer type is fixed (see `standardInt`). The TUI was writing
/// a mode into this on every mode change, into a field nothing consulted.
pub const Environment = struct {
allocator: Allocator,
variables: std.StringHashMap(Number),
ans: Number,
pub fn init(allocator: Allocator) Environment {
return .{
.allocator = allocator,
.variables = std.StringHashMap(Number).init(allocator),
// Starts inexact so that `init` cannot fail; the first evaluation
// replaces it.
.ans = Number.fromFloat(0),
};
}
pub fn deinit(self: *Environment) void {
var it = self.variables.iterator();
while (it.next()) |entry| {
self.allocator.free(entry.key_ptr.*);
entry.value_ptr.deinit();
}
self.variables.deinit();
self.ans.deinit();
}
/// Store a variable. Both the name and the value are copied, so neither has
/// to outlive this call.
///
/// The name is duplicated because it points into the expression source,
/// which callers are free to release: the TUI frees history entries on
/// Ctrl-L, which previously left dangling keys in this map.
pub fn setVar(self: *Environment, name: []const u8, value: Number) !void {
var copy = try value.cloneWith(self.allocator);
errdefer copy.deinit();
const gop = try self.variables.getOrPut(name);
if (gop.found_existing) {
gop.value_ptr.deinit();
} else {
const owned_name = self.allocator.dupe(u8, name) catch |err| {
// Remove the entry keyed by the borrowed name so the map never
// retains a key it does not own.
_ = self.variables.remove(name);
return err;
};
gop.key_ptr.* = owned_name;
}
gop.value_ptr.* = copy;
}
/// Replace the last answer, taking a copy.
pub fn setAns(self: *Environment, value: Number) !void {
const copy = try value.cloneWith(self.allocator);
self.ans.deinit();
self.ans = copy;
}
/// Borrowed view of a variable or built-in constant.
///
/// The result is owned by the environment (or is a freshly built constant),
/// so callers that need it to outlive the environment, or that will free it
/// separately, must `cloneWith` first.
///
/// The constants are inexact by nature: pi, e and tau are irrational and
/// have no rational representation.
pub fn getVar(self: *const Environment, name: []const u8) ?Number {
if (std.mem.eql(u8, name, "pi")) return Number.fromFloat(math.pi);
if (std.mem.eql(u8, name, "e")) return Number.fromFloat(math.e);
if (std.mem.eql(u8, name, "tau")) return Number.fromFloat(math.tau);
if (std.mem.eql(u8, name, "Ans") or std.mem.eql(u8, name, "ans")) return self.ans;
return self.variables.get(name);
}
/// The last answer collapsed to f64, for frontends that only need a float.
pub fn ansFloat(self: *const Environment) f64 {
return self.ans.toFloat(self.allocator);
}
};
/// Evaluate a parsed expression in the given environment.
/// Returns the computed value as f64 for standard mode.
///
/// Internally the computation runs on `Number`, so exact arithmetic is used
/// wherever possible and only collapses to f64 here, at the boundary. That
/// single final rounding is what fixes the accumulated-error class of bug:
/// `0.1 + 0.2` is computed as exactly `3/10` and rounds to the f64 nearest
/// `0.3`, rather than adding two separately-rounded operands.
///
/// Task 2.0c replaces this boundary with a `Number`-returning API, which is what
/// the remaining integer-precision cases need.
pub fn evaluate(env: *Environment, expr: *const Expr) Error!f64 {
// A scratch arena keeps Number lifetimes trivial: nothing in the recursive
// evaluator has to free intermediates, and the caller's allocator is never
// left holding them regardless of whether it is an arena itself.
var arena = std.heap.ArenaAllocator.init(env.allocator);
defer arena.deinit();
const scratch = arena.allocator();
const result = try evalExact(env, scratch, expr);
return result.toFloat(scratch);
}
/// The exact evaluation core. Produces a `Number`, staying exact until an
/// operation forces the float fallback (see design.md 2.7.4).
fn evalExact(env: *Environment, scratch: Allocator, expr: *const Expr) Error!Number {
switch (expr.*) {
.number => |n| return literalToNumber(scratch, n),
.string_literal => |text| {
// Pack ASCII bytes into an integer (BE packing, as programmer mode).
var packed_value: u128 = 0;
for (text) |byte| {
if (byte > 0x7F) return Error.InvalidNumber;
packed_value = (packed_value << 8) | byte;
}
return try Number.fromInt(scratch, packed_value);
},
.variable => |name| {
// getVar hands back a borrowed value owned by the environment, so
// copy it into the evaluation arena before it takes part in
// arithmetic that the arena will later free.
const value = env.getVar(name) orelse return Error.UnknownVariable;
return try value.cloneWith(scratch);
},
.assignment => |a| {
const val = try evalExact(env, scratch, a.value);
// setVar copies, so storing an arena-allocated value is safe.
env.setVar(a.name, val) catch return Error.OutOfMemory;
return val;
},
.unary => |u| {
const operand = try evalExact(env, scratch, u.operand);
return switch (u.op) {
.negate => try Number.negate(scratch, operand),
// Bitwise NOT is a fixed-width integer operation, not rational
// arithmetic, so it drops to the float/integer path. The width is
// the fixed standard-mode one: this used to read
// `env.programmer_config.bit_width`, so `tally --bits 8 '~0'` gave
// 255 in standard mode while the shifts alongside it ignored the
// setting entirely.
.bitwise_not => blk: {
break :blk fromStandardInt(bitwise.not(try standardInt(operand.toFloat(scratch))));
},
};
},
.binary => |b| {
const left = try evalExact(env, scratch, b.left);
const right = try evalExact(env, scratch, b.right);
return evalBinaryOp(scratch, b.op, left, right);
},
.call => |c| {
return evalFunction(env, scratch, c.name, c.args);
},
}
}
/// Turn a literal into a Number, exactly where possible.
///
/// Decimal literals are re-parsed from their source text rather than taken from
/// `float_value`, because `float_value` has already rounded: `0.1` cannot be
/// recovered from its binary approximation.
fn literalToNumber(scratch: Allocator, n: ast.Expr.Number) Error!Number {
if (n.base == .decimal and n.text.len > 0) {
if (Number.parse(scratch, n.text)) |value| return value else |_| {
// Fall through to the approximations below rather than failing: the
// tokenizer already accepted this text, so a parse mismatch here
// should degrade, not error.
}
}
// Non-decimal literals are integers; use the exact integer the tokenizer
// recovered when it fits, otherwise accept the float approximation.
if (n.int_value) |int_val| {
return try Number.fromInt(scratch, int_val);
}
return Number.fromFloat(n.float_value);
}
/// Evaluate a binary operation.
fn evalBinaryOp(scratch: Allocator, op: BinaryOp, left: Number, right: Number) Error!Number {
return switch (op) {
.add => try Number.add(scratch, left, right),
.sub => try Number.sub(scratch, left, right),
.mul => try Number.mul(scratch, left, right),
.div => try Number.div(scratch, left, right),
.mod => try Number.mod(scratch, left, right),
.pow => try Number.pow(scratch, left, right),
// The remaining operators are fixed-width integer operations rather than
// rational arithmetic, so they work on the 64-bit projection, in the shared
// implementation programmer mode also uses (FR-2.12). `inline else`
// resolves the operator at comptime, so an operator added to `BinaryOp`
// that `bitwise.fromBinaryOp` does not know is a compile error here.
inline else => |fixed_op| blk: {
const l = try standardInt(left.toFloat(scratch));
const r = try standardInt(right.toFloat(scratch));
const result = try bitwise.apply(comptime bitwise.fromBinaryOp(fixed_op).?, l, r);
break :blk fromStandardInt(result);
},
};
}
/// Project a float onto standard mode's integer type: 64-bit two's complement,
/// fixed (FR-2.3), which is also `Integer`'s default.
///
/// Standard mode does not consult the programmer-mode width. A width other than 64
/// is what programmer mode is for, and pretending otherwise is how `~` came to
/// honour the setting while the shifts beside it did not.
///
/// Every one of these operators used to do `@intFromFloat` straight onto the
/// unchecked value, which is illegal behaviour out of range and aborted the
/// process: `2^64 and 1` and `~1e30` both killed it, and a NaN operand produced a
/// garbage answer instead. An operand that does not fit the width is a reportable
/// error, not a crash.
fn standardInt(value: f64) Error!Integer {
if (!math.isFinite(value)) return Error.DomainError;
// i64 covers [-2^63, 2^63); 2^63 itself is the first excluded value and is
// exactly representable, so these bounds are exact.
if (value >= 9223372036854775808.0 or value < -9223372036854775808.0) {
return Error.Overflow;
}
const bits: u64 = @bitCast(@as(i64, @intFromFloat(value)));
return .{ .raw = @as(u128, bits) };
}
/// Read a result back as a number, signed, since standard mode is signed.
fn fromStandardInt(value: Integer) Number {
return Number.fromFloat(@floatFromInt(value.signedValue()));
}
/// Evaluate a built-in function call.
fn evalFunction(env: *Environment, scratch: Allocator, name: []const u8, args: []const *Expr) Error!Number {
// Single-argument functions
if (args.len == 1) {
const x = try evalExact(env, scratch, args[0]);
// Functions with an exact implementation.
if (std.mem.eql(u8, name, "abs")) {
return try Number.abs(scratch, x);
}
if (std.mem.eql(u8, name, "floor")) {
return try Number.floor(scratch, x);
}
if (std.mem.eql(u8, name, "ceil")) {
return try Number.ceil(scratch, x);
}
if (std.mem.eql(u8, name, "round")) {
return try Number.round(scratch, x);
}
if (std.mem.eql(u8, name, "sqrt")) {
// The negative-input rule lives in Number.sqrt, which raises
// NegativeRoot. That name now reaches the user instead of being
// flattened into "domain error".
return try Number.sqrt(scratch, x);
}
if (std.mem.eql(u8, name, "factorial")) {
const result = try Number.factorial(scratch, x);
// Null means the argument was negative or fractional, which is a domain
// error, not an unknown function.
return result orelse Error.DomainError;
}
// Everything else escapes the rationals, so it falls back to f64.
const f = try evalSingleArgFn(name, x.toFloat(scratch)) orelse
return Error.UnknownFunction;
return Number.fromFloat(f);
}
// Multi-argument functions
if (args.len == 2) {
const a = try evalExact(env, scratch, args[0]);
const b = try evalExact(env, scratch, args[1]);
if (std.mem.eql(u8, name, "max")) {
return try Number.max(scratch, a, b);
}
if (std.mem.eql(u8, name, "min")) {
return try Number.min(scratch, a, b);
}
const x = a.toFloat(scratch);
const y = b.toFloat(scratch);
if (std.mem.eql(u8, name, "atan2")) return Number.fromFloat(math.atan2(x, y));
// apy(nominal_rate, compounds_per_year): the effective annual rate, so a
// nominal rate can be compared against one.
if (std.mem.eql(u8, name, "apy")) {
return Number.fromFloat(try financial.effectiveAnnualRate(x, y));
}
if (std.mem.eql(u8, name, "log")) {
// log(value, base)
if (y <= 0 or y == 1 or x <= 0) return Error.DomainError;
return Number.fromFloat(@log(x) / @log(y));
}
}
// Financial functions take three or four arguments.
//
// These are inexact by construction: every financial formula needs a
// non-integer power or a logarithm, so the exact tier has nothing to
// preserve (see the header of financial.zig).
if (args.len == 3 or args.len == 4) {
var values: [4]f64 = undefined;
for (args, 0..) |arg, i| {
const value = try evalExact(env, scratch, arg);
values[i] = value.toFloat(scratch);
}
if (try evalFinancialFn(name, values[0..args.len])) |result| {
return Number.fromFloat(result);
}
}
// Zero-argument functions
if (args.len == 0) {
if (std.mem.eql(u8, name, "rand")) {
// Not truly random in a pure engine, but useful as placeholder
return Number.fromFloat(0.0);
}
}
return Error.UnknownFunction;
}
/// A whole period count or 1-based period index, validated.
fn periodCount(value: f64) Error!usize {
if (!math.isFinite(value)) return Error.DomainError;
if (@floor(value) != value) return Error.DomainError;
if (value < 1 or value > @as(f64, @floatFromInt(financial.max_schedule_periods))) {
return Error.DomainError;
}
return @intFromFloat(value);
}
/// Financial functions callable from a standard-mode expression.
///
/// Exposed as functions rather than only as a separate mode so that they compose
/// with the rest of the language: `cagr(10000, 25000, 5) * 100` and
/// `amort_interest(200000, 0.5, 360, 1) + 50` both work, the same way unit
/// conversion is reachable from a bare expression.
///
/// Returns null when `name` is not a financial function, so the caller can carry
/// on to report an unknown function.
fn evalFinancialFn(name: []const u8, a: []const f64) Error!?f64 {
if (a.len == 3) {
// cagr(start, end, periods) -> growth rate as a fraction.
if (std.mem.eql(u8, name, "cagr")) return try financial.cagr(a[0], a[1], a[2]);
// fv(pv, annual_rate_percent, years) compounded annually.
if (std.mem.eql(u8, name, "fv")) return try financial.compoundFutureValue(a[0], a[1], a[2], 1);
// pv(fv, annual_rate_percent, years) compounded annually.
if (std.mem.eql(u8, name, "pv")) return try financial.compoundPresentValue(a[0], a[1], a[2], 1);
// The same relationship solved for its other two variables. The rate is
// NOMINAL; use apy() to convert.
if (std.mem.eql(u8, name, "compound_rate")) return try financial.compoundRate(a[0], a[1], a[2], 1);
if (std.mem.eql(u8, name, "compound_years")) return try financial.compoundPeriods(a[0], a[1], a[2], 1);
// amort_payment(principal, rate_per_period_percent, periods)
if (std.mem.eql(u8, name, "amort_payment")) {
return try financial.amortizationPayment(.{
.principal = a[0],
.rate = a[1],
.periods = try periodCount(a[2]),
});
}
if (std.mem.eql(u8, name, "amort_total_interest")) {
const totals = try financial.amortizationTotals(.{
.principal = a[0],
.rate = a[1],
.periods = try periodCount(a[2]),
});
return totals.interest;
}
if (std.mem.eql(u8, name, "amort_total_paid")) {
const totals = try financial.amortizationTotals(.{
.principal = a[0],
.rate = a[1],
.periods = try periodCount(a[2]),
});
return totals.paid;
}
return null;
}
// fv(pv, rate, years, compounds_per_year) and its inverse.
if (std.mem.eql(u8, name, "fv")) return try financial.compoundFutureValue(a[0], a[1], a[2], a[3]);
if (std.mem.eql(u8, name, "pv")) return try financial.compoundPresentValue(a[0], a[1], a[2], a[3]);
// compound_rate(pv, fv, years, compounds_per_year) -> nominal annual rate.
if (std.mem.eql(u8, name, "compound_rate")) return try financial.compoundRate(a[0], a[1], a[2], a[3]);
// compound_years(pv, fv, rate, compounds_per_year)
if (std.mem.eql(u8, name, "compound_years")) return try financial.compoundPeriods(a[0], a[1], a[2], a[3]);
// TVM: each function names the variable it solves for, and takes the other
// four in the calculator's N, I/Y, PV, PMT, FV order.
if (std.mem.eql(u8, name, "tvm_fv")) {
const s = try financial.solveTvm(.{ .periods = a[0], .rate = a[1], .present_value = a[2], .payment = a[3] });
return s.value;
}
if (std.mem.eql(u8, name, "tvm_pv")) {
const s = try financial.solveTvm(.{ .periods = a[0], .rate = a[1], .payment = a[2], .future_value = a[3] });
return s.value;
}
if (std.mem.eql(u8, name, "tvm_pmt")) {
const s = try financial.solveTvm(.{ .periods = a[0], .rate = a[1], .present_value = a[2], .future_value = a[3] });
return s.value;
}
if (std.mem.eql(u8, name, "tvm_n")) {
const s = try financial.solveTvm(.{ .rate = a[0], .present_value = a[1], .payment = a[2], .future_value = a[3] });
return s.value;
}
if (std.mem.eql(u8, name, "tvm_rate")) {
const s = try financial.solveTvm(.{ .periods = a[0], .present_value = a[1], .payment = a[2], .future_value = a[3] });
return s.value;
}
// Amortization rows: (principal, rate_per_period_percent, periods, period)
const is_interest = std.mem.eql(u8, name, "amort_interest");
const is_principal = std.mem.eql(u8, name, "amort_principal");
const is_balance = std.mem.eql(u8, name, "amort_balance");
if (is_interest or is_principal or is_balance) {
const entry = try financial.amortizationEntry(.{
.principal = a[0],
.rate = a[1],
.periods = try periodCount(a[2]),
}, try periodCount(a[3]));
if (is_interest) return entry.interest;
if (is_principal) return entry.principal;
return entry.balance;
}
return null;
}
/// Evaluate a single-argument built-in function that has no exact form.
/// Evaluate a single-argument built-in that has no exact form.
///
/// Returns null when `name` is not one of these functions, and an error when the
/// name is known but the argument is outside its domain. The two used to be the
/// same answer (null), so the caller reported `asin(2)` as "unknown function".
fn evalSingleArgFn(name: []const u8, x: f64) Error!?f64 {
if (std.mem.eql(u8, name, "sin")) return @sin(x);
if (std.mem.eql(u8, name, "cos")) return @cos(x);
if (std.mem.eql(u8, name, "tan")) return @tan(x);
if (std.mem.eql(u8, name, "asin")) {
if (x < -1 or x > 1) return Error.DomainError;
return math.asin(x);
}
if (std.mem.eql(u8, name, "acos")) {
if (x < -1 or x > 1) return Error.DomainError;
return math.acos(x);
}
if (std.mem.eql(u8, name, "atan")) return math.atan(x);
// log/log10/ln/log2 of a non-positive value has no real result. The two-argument
// log already reported this as a domain error; the one-argument forms returned
// -inf or NaN.
if (std.mem.eql(u8, name, "log") or std.mem.eql(u8, name, "log10")) {
if (x <= 0) return Error.DomainError;
return @log10(x);
}
if (std.mem.eql(u8, name, "ln")) {
if (x <= 0) return Error.DomainError;
return @log(x);
}
if (std.mem.eql(u8, name, "log2")) {
if (x <= 0) return Error.DomainError;
return @log2(x);
}
if (std.mem.eql(u8, name, "cbrt")) return math.cbrt(x);
if (std.mem.eql(u8, name, "exp")) return @exp(x);
return null;
}
/// Result of evaluation with metadata for display decisions.
pub const EvalInfo = struct {
/// The computed value. Allocated with the allocator passed to
/// `evalStringInfo`; the caller owns it and should `deinit` when done.
value: Number,
/// True if the expression contained any non-decimal literal (hex/oct/bin).
has_nondecimal_literal: bool,
};
/// High-level evaluate: parse a string and evaluate it.
/// Updates env.ans on success. The caller owns the returned value.
pub fn evalString(env: *Environment, allocator: Allocator, source: []const u8) Error!Number {
const info = try evalStringInfo(env, allocator, source);
return info.value;
}
/// Like evalString but returns metadata (whether the expression used
/// non-decimal literals) so frontends can decide to show a multi-base view.
pub fn evalStringInfo(env: *Environment, allocator: Allocator, source: []const u8) Error!EvalInfo {
var p = Parser.init(allocator, source);
const expr = try p.parse();
// The parser hands over ownership. Nothing in the result borrows from the
// tree (literal text points into `source`, and the value is cloned out of the
// scratch arena), so it can be released as soon as evaluation is done.
// Without this every evaluated expression leaked its whole AST, which only
// went unnoticed because the CLI hands in an arena.
defer parser_mod.freeExpr(allocator, expr);
// Intermediates live in a scratch arena; only the final value is copied out
// into the caller's allocator.
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const scratch = arena.allocator();
const raw = try evalExact(env, scratch, expr);
const result = try raw.cloneWith(allocator);
env.setAns(result) catch return Error.OutOfMemory;
return .{
.value = result,
.has_nondecimal_literal = hasNonDecimalLiteral(expr),
};
}
/// Walk an AST and report whether any number literal is non-decimal.
fn hasNonDecimalLiteral(expr: *const Expr) bool {
return switch (expr.*) {
.number => |n| n.base != .decimal,
.string_literal => false,
.variable => false,
.unary => |u| hasNonDecimalLiteral(u.operand),
.binary => |b| hasNonDecimalLiteral(b.left) or hasNonDecimalLiteral(b.right),
.call => |c| blk: {
for (c.args) |arg| {
if (hasNonDecimalLiteral(arg)) break :blk true;
}
break :blk false;
},
.assignment => |a| hasNonDecimalLiteral(a.value),
};
}
// -- Tests --
const testing = std.testing;
/// Evaluate and collapse to f64.
///
/// The exact result is converted here rather than at every call site, which is
/// what lets the ~90 pre-existing f64 assertions in this file stay untouched
/// while the engine itself moved to `Number`.
fn testEval(source: []const u8) !f64 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const alloc = arena.allocator();
var env = Environment.init(alloc);
defer env.deinit();
const result = try evalString(&env, alloc, source);
return result.toFloat(alloc);
}
fn testEvalProgrammer(source: []const u8) !f64 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const alloc = arena.allocator();
var env = Environment.init(alloc);
defer env.deinit();
const result = try evalString(&env, alloc, source);
return result.toFloat(alloc);
}
test "eval simple number" {
const result = try testEval("42");
try testing.expectEqual(@as(f64, 42.0), result);
}
test "eval addition" {
const result = try testEval("2 + 3");
try testing.expectEqual(@as(f64, 5.0), result);
}
test "eval subtraction" {
const result = try testEval("10 - 7");
try testing.expectEqual(@as(f64, 3.0), result);
}
test "eval multiplication" {
const result = try testEval("6 * 7");
try testing.expectEqual(@as(f64, 42.0), result);
}
test "eval division" {
const result = try testEval("10 / 4");
try testing.expectEqual(@as(f64, 2.5), result);
}
test "eval division by zero" {
const result = testEval("1 / 0");
try testing.expectError(Error.DivisionByZero, result);
}
test "eval modulo" {
const result = try testEval("10 % 3");
try testing.expectApproxEqAbs(@as(f64, 1.0), result, 1e-10);
}
test "eval power" {
const result = try testEval("2^10");
try testing.expectEqual(@as(f64, 1024.0), result);
}
test "eval precedence" {
const result = try testEval("2 + 3 * 4");
try testing.expectEqual(@as(f64, 14.0), result);
}
test "eval parentheses" {
const result = try testEval("(2 + 3) * 4");
try testing.expectEqual(@as(f64, 20.0), result);
}
test "eval unary negation" {
const result = try testEval("-5 + 3");
try testing.expectEqual(@as(f64, -2.0), result);
}
test "eval nested parens" {
const result = try testEval("((2 + 3) * (4 - 1))");
try testing.expectEqual(@as(f64, 15.0), result);
}
test "eval pi constant" {
const result = try testEval("pi");
try testing.expectApproxEqAbs(math.pi, result, 1e-10);
}
test "eval e constant" {
const result = try testEval("e");
try testing.expectApproxEqAbs(math.e, result, 1e-10);
}
test "eval tau constant" {
const result = try testEval("tau");
try testing.expectApproxEqAbs(math.tau, result, 1e-10);
}
test "eval 2*pi" {
const result = try testEval("2*pi");
try testing.expectApproxEqAbs(2.0 * math.pi, result, 1e-10);
}
test "eval 3*(4+5)" {
const result = try testEval("3*(4+5)");
try testing.expectEqual(@as(f64, 27.0), result);
}
test "eval sin" {
const result = try testEval("sin(0)");
try testing.expectApproxEqAbs(@as(f64, 0.0), result, 1e-10);
}
test "eval cos" {
const result = try testEval("cos(0)");
try testing.expectApproxEqAbs(@as(f64, 1.0), result, 1e-10);
}
test "eval sqrt" {
const result = try testEval("sqrt(144)");
try testing.expectEqual(@as(f64, 12.0), result);
}
test "eval abs" {
const result = try testEval("abs(-42)");
try testing.expectEqual(@as(f64, 42.0), result);
}
test "eval floor" {
const result = try testEval("floor(3.7)");
try testing.expectEqual(@as(f64, 3.0), result);
}
test "eval ceil" {
const result = try testEval("ceil(3.2)");
try testing.expectEqual(@as(f64, 4.0), result);
}
test "eval round" {
const result = try testEval("round(3.5)");
try testing.expectEqual(@as(f64, 4.0), result);
}
test "eval factorial" {
const result = try testEval("factorial(5)");
try testing.expectEqual(@as(f64, 120.0), result);
}
test "eval factorial 0" {
const result = try testEval("factorial(0)");
try testing.expectEqual(@as(f64, 1.0), result);
}
test "eval ln" {
const result = try testEval("ln(1)");
try testing.expectApproxEqAbs(@as(f64, 0.0), result, 1e-10);
}
test "eval exp" {
const result = try testEval("exp(0)");
try testing.expectEqual(@as(f64, 1.0), result);
}
test "eval max" {
const result = try testEval("max(3, 7)");
try testing.expectEqual(@as(f64, 7.0), result);
}
test "eval min" {
const result = try testEval("min(3, 7)");
try testing.expectEqual(@as(f64, 3.0), result);
}
test "eval unknown function" {
const result = testEval("bogus(1)");
try testing.expectError(Error.UnknownFunction, result);
}
test "eval unknown variable" {
const result = testEval("xyz");
try testing.expectError(Error.UnknownVariable, result);
}
test "eval variable assignment and use" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const alloc = arena.allocator();
var env = Environment.init(alloc);
defer env.deinit();
const assign_result = try evalString(&env, alloc, "X = 42");
try testing.expectEqual(@as(f64, 42.0), assign_result.toFloat(alloc));
const use_result = try evalString(&env, alloc, "X + 8");
try testing.expectEqual(@as(f64, 50.0), use_result.toFloat(alloc));
}
test "eval Ans" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const alloc = arena.allocator();
var env = Environment.init(alloc);
defer env.deinit();
_ = try evalString(&env, alloc, "7 * 6");
const result = try evalString(&env, alloc, "Ans + 1");
try testing.expectEqual(@as(f64, 43.0), result.toFloat(alloc));
}
test "eval complex expression" {
const result = try testEval("sin(pi/2) + cos(0)");
try testing.expectApproxEqAbs(@as(f64, 2.0), result, 1e-10);
}
test "eval 2^32 - 1" {
const result = try testEval("2^32 - 1");
try testing.expectEqual(@as(f64, 4294967295.0), result);
}
test "eval programmer XOR" {
const result = try testEvalProgrammer("0xF xor 0x3");
try testing.expectEqual(@as(f64, 12.0), result);
}
test "eval programmer AND" {
const result = try testEvalProgrammer("0xFF & 0x0F");
try testing.expectEqual(@as(f64, 15.0), result);
}
test "eval programmer OR" {
const result = try testEvalProgrammer("0xF0 | 0x0F");
try testing.expectEqual(@as(f64, 255.0), result);
}
test "eval programmer shift left" {
const result = try testEvalProgrammer("1 << 8");
try testing.expectEqual(@as(f64, 256.0), result);
}
test "eval programmer shift right" {
const result = try testEvalProgrammer("256 >> 4");
try testing.expectEqual(@as(f64, 16.0), result);
}
test "eval number with underscores" {
const result = try testEval("1_000_000 + 1");
try testing.expectEqual(@as(f64, 1_000_001.0), result);
}
test "eval number with commas" {
const result = try testEval("1,000 * 2.3");
try testing.expectApproxEqAbs(@as(f64, 2300.0), result, 1e-10);
}
test "eval commas not confused with function args" {
const result = try testEval("max(1,000, 500)");
try testing.expectEqual(@as(f64, 1000.0), result);
}
test "eval bitwise not in standard mode" {
const result = try testEval("~0");
// ~0 as i64 = -1
try testing.expectEqual(@as(f64, -1.0), result);
}
test "standard mode: the fixed-width operators use one implementation with programmer mode" {
// FR-2.12: an operator means the same thing in both modes. Standard mode is
// 64-bit signed (FR-2.3), so the same expression evaluated through the
// evaluator and through programmer.zig has to agree. Note that this compares
// the two real paths: `testEvalProgrammer` only sets the mode flag and still
// runs the evaluator, so it would have compared one implementation with itself.
const programmer_mod = @import("programmer.zig");
const shared = [_][]const u8{
"0xF0 and 0x0F",
"0xF0 or 0x0F",
"5 xor 3",
"1 << 10",
"1 << 63",
"1 << 64",
"1024 >> 4",
"1024 >>> 4",
"0 - 8 >> 1",
"1 rol 4",
"1 rol 65",
"16 ror 4",
"~0",
"~5",
};
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
for (shared) |source| {
const standard = try testEval(source);
const prog = try programmer_mod.evalProgrammerString(alloc, source, .{
.width = .bits64,
.signedness = .signed,
});
try testing.expectEqual(@as(i128, @intFromFloat(standard)), prog.signedValue());
}
}
test "standard mode: >> is arithmetic and >>> is logical" {
// This is the case that used to differ: standard mode had only a logical shift,
// so `-8 >> 1` was 9223372036854775804 rather than -4.
try testing.expectEqual(@as(f64, -4.0), try testEval("0 - 8 >> 1"));
try testing.expectEqual(@as(f64, -1.0), try testEval("0 - 1 >> 1"));
try testing.expectEqual(@as(f64, -64.0), try testEval("0 - 128 >> 1"));
// The zero-filling variant is still available and still huge.
try testing.expect(try testEval("0 - 8 >>> 1") > 9.0e18);
// Non-negative values shift identically either way.
try testing.expectEqual(try testEval("1024 >> 4"), try testEval("1024 >>> 4"));
}
test "standard mode: a shift runs to completion instead of wrapping the distance" {
// The distance used to be reduced modulo 64, so `1 << 64` was `1 << 0`.
try testing.expectEqual(@as(f64, 0.0), try testEval("1 << 64"));
try testing.expectEqual(@as(f64, 0.0), try testEval("1 << 65"));
try testing.expectEqual(@as(f64, 0.0), try testEval("1 << 1000"));
try testing.expectEqual(@as(f64, 0.0), try testEval("1024 >>> 64"));
// A negative value shifted all the way out is all sign bits, which is -1.
try testing.expectEqual(@as(f64, -1.0), try testEval("0 - 8 >> 64"));
// One place short of the width still keeps a bit: 1 << 63 is the sign bit.
try testing.expect(try testEval("1 << 63") < 0.0);
}
test "standard mode: a negative shift distance is a domain error" {
// It used to be reduced modulo 64, so `8 >> -1` quietly became `8 >> 63`.
try testing.expectError(Error.DomainError, testEval("8 >> 0 - 1"));
try testing.expectError(Error.DomainError, testEval("8 << 0 - 1"));
try testing.expectError(Error.DomainError, testEval("8 >>> 0 - 1"));
try testing.expectError(Error.DomainError, testEval("8 rol 0 - 1"));
}
test "standard mode: rotation is cyclic, not clamped" {
try testing.expectEqual(@as(f64, 1.0), try testEval("1 rol 64"));
try testing.expectEqual(@as(f64, 2.0), try testEval("1 rol 65"));
try testing.expectEqual(@as(f64, 1.0), try testEval("1 ror 64"));
}
test "standard mode: its integer type is fixed at 64-bit signed" {
// `~` used to read `env.programmer_config.bit_width` while the shifts beside it
// ignored it, so `--bits 8` changed one operator and not the others. The
// environment no longer carries a width to disagree about: there is nothing to
// set here, which is the point.
var env = Environment.init(testing.allocator);
defer env.deinit();
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
var not_zero = try evalString(&env, alloc, "~0");
defer not_zero.deinit();
try testing.expectEqual(@as(f64, -1.0), not_zero.toFloat(alloc));
var shifted = try evalString(&env, alloc, "1 << 10");
defer shifted.deinit();
try testing.expectEqual(@as(f64, 1024.0), shifted.toFloat(alloc));
const projected = try standardInt(1);
try testing.expectEqual(@as(u8, 64), projected.bits());
try testing.expectEqual(Integer.Signedness.signed, projected.signedness);
}
test "eval rotate left in standard mode" {
// 1 rol 4 = 16 (for 64-bit)
const result = try testEvalProgrammer("1 rol 4");
try testing.expectEqual(@as(f64, 16.0), result);
}
test "eval rotate right in standard mode" {
const result = try testEvalProgrammer("16 ror 4");
try testing.expectEqual(@as(f64, 1.0), result);
}
test "eval atan2" {
const result = try testEval("atan2(1, 1)");
try testing.expectApproxEqAbs(math.pi / 4.0, result, 1e-10);
}
test "eval log with base" {
const result = try testEval("log(100, 10)");
try testing.expectApproxEqAbs(@as(f64, 2.0), result, 1e-10);
}
test "eval log domain error" {
const result = testEval("log(-1, 10)");
try testing.expectError(Error.DomainError, result);
}
test "domain errors are domain errors, not unknown functions" {
// These pinned the wrong contract: the name is known, the argument is not in
// its domain. Reporting "unknown function" sent the user looking for a typo.
try testing.expectError(Error.DomainError, testEval("asin(2)"));
try testing.expectError(Error.DomainError, testEval("asin(-2)"));
try testing.expectError(Error.DomainError, testEval("acos(2)"));
try testing.expectError(Error.DomainError, testEval("factorial(-1)"));
try testing.expectError(Error.DomainError, testEval("factorial(2.5)"));
// Logarithms of non-positive values, which used to return -inf or NaN. The
// two-argument form already reported this correctly.
try testing.expectError(Error.DomainError, testEval("ln(0)"));
try testing.expectError(Error.DomainError, testEval("ln(0 - 1)"));
try testing.expectError(Error.DomainError, testEval("log(0)"));
try testing.expectError(Error.DomainError, testEval("log10(0 - 5)"));
try testing.expectError(Error.DomainError, testEval("log2(0)"));
try testing.expectError(Error.DomainError, testEval("log(100, 1)"));
// sqrt says which domain rule was broken, because the numeric tier raises its
// own error and nothing flattens it on the way out.
try testing.expectError(Error.NegativeRoot, testEval("sqrt(-1)"));
try testing.expectError(Error.NegativeRoot, testEval("sqrt(0 - 4)"));
// A genuinely unknown name still reports one.
try testing.expectError(Error.UnknownFunction, testEval("nope(1)"));
try testing.expectError(Error.UnknownFunction, testEval("asin(1, 2)"));
}
test "the functions themselves still work inside their domains" {
try testing.expectApproxEqAbs(@as(f64, 0.0), try testEval("asin(0)"), 1e-15);
try testing.expectApproxEqAbs(math.pi / 2.0, try testEval("acos(0)"), 1e-15);
try testing.expectEqual(@as(f64, 2.0), try testEval("log10(100)"));
try testing.expectApproxEqAbs(@as(f64, 1.0), try testEval("ln(e)"), 1e-15);
try testing.expectEqual(@as(f64, 3.0), try testEval("log2(8)"));
try testing.expectEqual(@as(f64, 120.0), try testEval("factorial(5)"));
try testing.expectEqual(@as(f64, 12.0), try testEval("sqrt(144)"));
}
test "eval acos" {
const result = try testEval("acos(1)");
try testing.expectApproxEqAbs(@as(f64, 0.0), result, 1e-10);
}
test "evalStringInfo: detects hex literal" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const alloc = arena.allocator();
var env = Environment.init(alloc);
defer env.deinit();
const info = try evalStringInfo(&env, alloc, "0o777 - 0x0f");
try testing.expectEqual(@as(f64, 496.0), info.value.toFloat(alloc));
try testing.expect(info.has_nondecimal_literal);
}
test "evalStringInfo: pure decimal has no nondecimal literal" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const alloc = arena.allocator();
var env = Environment.init(alloc);
defer env.deinit();
const info = try evalStringInfo(&env, alloc, "2 + 2");
try testing.expectEqual(@as(f64, 4.0), info.value.toFloat(alloc));
try testing.expect(!info.has_nondecimal_literal);
}
test "evalStringInfo: binary literal detected in nested expr" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const alloc = arena.allocator();
var env = Environment.init(alloc);
defer env.deinit();
const info = try evalStringInfo(&env, alloc, "sqrt(0b100) + 1");
try testing.expect(info.has_nondecimal_literal);
}
test "eval string literal in standard mode packs ASCII" {
// 'A' -> 0x41 -> 65
const result = try testEval("'A'");
try testing.expectEqual(@as(f64, 65.0), result);
}
test "eval string literal multi-char in standard mode" {
// 'AB' -> 0x4142 -> 16706
const result = try testEval("'AB'");
try testing.expectEqual(@as(f64, 16706.0), result);
}
test "eval string literal with non-ASCII byte errors" {
// byte > 0x7F is rejected
const result = testEval("'\x80'");
try testing.expectError(Error.InvalidNumber, result);
}
test "eval rand zero-arg function returns 0" {
const result = try testEval("rand()");
try testing.expectEqual(@as(f64, 0.0), result);
}
test "eval unknown zero-arg function" {
const result = testEval("bogus()");
try testing.expectError(Error.UnknownFunction, result);
}
test "eval unknown three-arg function" {
const result = testEval("bogus(1, 2, 3)");
try testing.expectError(Error.UnknownFunction, result);
}
test "eval unknown two-arg function" {
const result = testEval("bogus(1, 2)");
try testing.expectError(Error.UnknownFunction, result);
}
// -- Exact arithmetic (Task 2.0b) --
//
// These verify the exact evaluation core through the unchanged f64 API. The
// payoff is visible here because today's errors are ACCUMULATED: f64 rounds
// each decimal literal before operating on it, whereas the exact core computes
// the true value and rounds once, at the boundary.
//
// Note the runtime-`var` dance in the comparisons against plain f64: Zig folds
// float literals at comptime as `comptime_float`, so `0.1 + 0.2 != 0.3` is
// false at comptime and would not exercise f64 at all.
test "exact: 0.1 + 0.2 is 0.3" {
try testing.expectEqual(@as(f64, 0.3), try testEval("0.1 + 0.2"));
var x: f64 = 0.1;
var y: f64 = 0.2;
_ = &x;
_ = &y;
try testing.expect(x + y != @as(f64, 0.3));
}
test "exact: 1.1 + 2.2 is 3.3" {
try testing.expectEqual(@as(f64, 3.3), try testEval("1.1 + 2.2"));
}
test "exact: 0.1 * 3 is 0.3" {
try testing.expectEqual(@as(f64, 0.3), try testEval("0.1 * 3"));
}
test "exact: chained decimal addition" {
try testing.expectEqual(@as(f64, 0.6), try testEval("0.1 + 0.2 + 0.3"));
try testing.expectEqual(@as(f64, 0.8), try testEval("0.7 + 0.1"));
try testing.expectEqual(@as(f64, 0.2), try testEval("0.3 - 0.1"));
try testing.expectEqual(@as(f64, 0.01), try testEval("0.1 * 0.1"));
}
test "exact: an expression that cancels reaches exactly zero" {
try testing.expectEqual(@as(f64, 0.0), try testEval("(0.1 + 0.2) * 10 - 3"));
var x: f64 = 0.1;
var y: f64 = 0.2;
_ = &x;
_ = &y;
try testing.expect((x + y) * 10.0 - 3.0 != 0.0);
}
test "exact: intermediates beyond f64 precision survive" {
// 1e20 + 1 is not representable in f64, so the f64 route loses the 1 and
// yields 0. Exact arithmetic keeps it and the final result fits.
try testing.expectEqual(@as(f64, 1.0), try testEval("1e20 + 1 - 1e20"));
var big: f64 = 1e20;
_ = &big;
try testing.expectEqual(@as(f64, 0.0), big + 1.0 - big);
}
test "exact: division round trip" {
try testing.expectEqual(@as(f64, 1.0), try testEval("1 / 3 * 3"));
try testing.expectEqual(@as(f64, 1.0), try testEval("1 / 7 * 7"));
try testing.expectEqual(@as(f64, 100.5), try testEval("1.005 * 100"));
}
test "exact: factorial is no longer capped at 170" {
// Previously `factorial(171)` reported "unknown function" because the f64
// implementation overflowed. It now computes exactly and only loses
// magnitude at the f64 boundary.
const result = try testEval("factorial(171)");
try testing.expect(math.isPositiveInf(result));
// And a value f64 can still hold comes back exact.
try testing.expectEqual(@as(f64, 120.0), try testEval("factorial(5)"));
}
test "exact: perfect square roots stay exact, irrational ones fall back" {
try testing.expectEqual(@as(f64, 12.0), try testEval("sqrt(144)"));
try testing.expectEqual(@as(f64, 0.5), try testEval("sqrt(0.25)"));
try testing.expectApproxEqAbs(math.sqrt2, try testEval("sqrt(2)"), 1e-15);
}
test "exact: floor, ceil and round match the float builtins" {
try testing.expectEqual(@as(f64, -4.0), try testEval("floor(-3.2)"));
try testing.expectEqual(@as(f64, -3.0), try testEval("ceil(-3.2)"));
try testing.expectEqual(@as(f64, 3.0), try testEval("round(2.5)"));
try testing.expectEqual(@as(f64, -3.0), try testEval("round(-2.5)"));
try testing.expectEqual(@as(f64, 0.1), try testEval("abs(-0.1)"));
}
test "exact: mod keeps the sign of the divisor" {
try testing.expectEqual(@as(f64, 1.0), try testEval("10 % 3"));
try testing.expectEqual(@as(f64, 2.0), try testEval("-10 % 3"));
try testing.expectEqual(@as(f64, 0.5), try testEval("7.5 % 1"));
}
test "exact: non-decimal literals are exact integers" {
try testing.expectEqual(@as(f64, 255.0), try testEval("0xFF"));
try testing.expectEqual(@as(f64, 496.0), try testEval("0o777 - 0x0f"));
try testing.expectEqual(@as(f64, 10.0), try testEval("0b1010"));
}
test "exact: transcendentals still fall back to floats" {
// These have no exact rational form, so they must go through f64 and are
// only expected to be approximately right.
try testing.expectApproxEqAbs(@as(f64, 0.0), try testEval("sin(0)"), 1e-15);
try testing.expectApproxEqAbs(math.pi, try testEval("pi"), 1e-15);
try testing.expectApproxEqAbs(@as(f64, 1.0), try testEval("ln(e)"), 1e-15);
try testing.expectApproxEqAbs(@as(f64, 2.0), try testEval("log10(100)"), 1e-15);
}
test "exact: a transcendental contaminates the rest of the expression" {
// Once sin() enters, the result is inexact; it must still be numerically
// right, just not exact.
const result = try testEval("sin(0) + 0.1 + 0.2");
try testing.expectApproxEqAbs(@as(f64, 0.3), result, 1e-15);
}
test "exact: an absurd exponent is reported as an exponent that is too large" {
// The rational layer's guard reaches the caller by its own name rather than as a
// generic Overflow, which is what the old single error set turned it into.
try testing.expectError(Error.ExponentTooLarge, testEval("2 ^ 3000000"));
}
// -- Exactness visible through the Number API (Task 2.0c) --
//
// These are the cases the f64 boundary could not express. `testEval` collapses
// to f64 and would lose exactly the information under test here.
/// Evaluate and keep the exact result. The arena owns everything.
fn testEvalNumber(arena: *std.heap.ArenaAllocator, source: []const u8) !Number {
const a = arena.allocator();
var env = Environment.init(a);
defer env.deinit();
return evalString(&env, a, source);
}
fn expectExactDecimal(expected: []const u8, source: []const u8) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const result = try testEvalNumber(&arena, source);
try testing.expect(result.isExact());
const shown = try result.toDecimalString(arena.allocator(), 20);
try testing.expectEqualStrings(expected, shown.text);
}
test "Number API: the integer f64 cannot hold round-trips" {
// The headline case. Through the f64 boundary this became
// 9.007199254740992e15, a DIFFERENT integer than the one typed.
try expectExactDecimal("9007199254740993", "9007199254740993");
try expectExactDecimal("9007199254740993", "2^53 + 1");
try expectExactDecimal("9007199254740992", "2^53");
}
test "Number API: exact integer arithmetic is unbounded" {
try expectExactDecimal("1267650600228229401496703205376", "2^100");
try expectExactDecimal("100000000000000000001", "1e20 + 1");
try expectExactDecimal("121932631112635269", "123456789 * 987654321");
}
test "Number API: one third is retained exactly, not as a decimal" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const result = try testEvalNumber(&arena, "1/3");
try testing.expect(result.isExact());
// The exact form is a fraction, which no float could express.
const frac = (try result.toFractionString(arena.allocator())).?;
try testing.expectEqualStrings("1/3", frac);
// And its decimal rendering is correctly reported as approximate.
const shown = try result.toDecimalString(arena.allocator(), 10);
try testing.expect(!shown.exact);
}
test "Number API: factorial is exact past the old 170 limit" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const result = try testEvalNumber(&arena, "factorial(171)");
try testing.expect(result.isExact());
const shown = try result.toDecimalString(arena.allocator(), 0);
// 171! has 310 digits; f64 could only report infinity.
try testing.expectEqual(@as(usize, 310), shown.text.len);
try testing.expect(shown.exact);
}
test "Number API: transcendentals are reported as inexact" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const s = try testEvalNumber(&arena, "sin(1)");
try testing.expect(!s.isExact());
const p = try testEvalNumber(&arena, "pi");
try testing.expect(!p.isExact());
const r = try testEvalNumber(&arena, "sqrt(2)");
try testing.expect(!r.isExact());
// But a perfect square stays exact.
const q = try testEvalNumber(&arena, "sqrt(144)");
try testing.expect(q.isExact());
}
test "Number API: inexactness is contagious across an expression" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const result = try testEvalNumber(&arena, "0.1 + 0.2 + sin(0)");
try testing.expect(!result.isExact());
}
test "Number API: variables keep the exactness of their expression" {
// This is what storing Number in the Environment buys: previously the
// assignment round-tripped through f64 and 0.1 came back approximated.
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const a = arena.allocator();
var env = Environment.init(a);
defer env.deinit();
const assigned = try evalString(&env, a, "X = 0.1");
try testing.expect(assigned.isExact());
const sum = try evalString(&env, a, "X + 0.2");
try testing.expect(sum.isExact());
const shown = try sum.toDecimalString(a, 20);
try testing.expectEqualStrings("0.3", shown.text);
try testing.expect(shown.exact);
}
test "Number API: Ans keeps exactness between evaluations" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const a = arena.allocator();
var env = Environment.init(a);
defer env.deinit();
_ = try evalString(&env, a, "1/3");
const doubled = try evalString(&env, a, "Ans * 3");
try testing.expect(doubled.isExact());
const shown = try doubled.toDecimalString(a, 20);
try testing.expectEqualStrings("1", shown.text);
}
test "Number API: reassigning a variable releases the old value" {
// Exercises the replace path in setVar, which must deinit the previous
// Number rather than leaking it.
var env = Environment.init(testing.allocator);
defer env.deinit();
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const a = arena.allocator();
_ = try evalString(&env, a, "X = 1/3");
_ = try evalString(&env, a, "X = 2/7");
_ = try evalString(&env, a, "X = 5");
const result = try evalString(&env, a, "X * 2");
try testing.expectEqual(@as(f64, 10.0), result.toFloat(a));
}
test "Number API: a variable name outliving its source text stays valid" {
// setVar duplicates the name because it points into the expression source,
// which the caller may free (the TUI frees history on Ctrl-L).
var env = Environment.init(testing.allocator);
defer env.deinit();
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const a = arena.allocator();
{
const source = try testing.allocator.dupe(u8, "myvar = 42");
defer testing.allocator.free(source);
_ = try evalString(&env, a, source);
}
// The source is gone; the stored name must still resolve.
const result = try evalString(&env, a, "myvar + 1");
try testing.expectEqual(@as(f64, 43.0), result.toFloat(a));
}
// -- Financial functions in standard mode --
//
// The point of these tests is reachability and composition: the financial math
// itself is covered in financial.zig. What matters here is that a plain
// expression can call them, with the argument counts the parser now allows.
test "financial: cagr is callable from a standard expression" {
try testing.expectApproxEqAbs(@as(f64, 0.2011244), try testEval("cagr(10000, 25000, 5)"), 1e-7);
}
test "financial: a financial result composes with ordinary arithmetic" {
// The fraction-to-percent conversion users will reach for immediately.
try testing.expectApproxEqAbs(@as(f64, 20.11244), try testEval("cagr(10000, 25000, 5) * 100"), 1e-5);
}
test "financial: arguments may themselves be expressions" {
try testing.expectApproxEqAbs(
@as(f64, 0.2011244),
try testEval("cagr(10000, 5 * 5000, 60 / 12)"),
1e-7,
);
}
test "financial: compound interest with and without a frequency argument" {
// Three arguments compounds annually.
try testing.expectApproxEqAbs(@as(f64, 1628.894627), try testEval("fv(1000, 5, 10)"), 1e-6);
// The fourth argument is the compounding frequency; monthly beats annual.
try testing.expectApproxEqAbs(@as(f64, 1647.009498), try testEval("fv(1000, 5, 10, 12)"), 1e-6);
try testing.expect(try testEval("fv(1000, 5, 10, 12)") > try testEval("fv(1000, 5, 10)"));
}
test "financial: present value inverts future value" {
const future = try testEval("fv(1000, 5, 10, 4)");
var buffer: [64]u8 = undefined;
const source = try std.fmt.bufPrint(&buffer, "pv({d}, 5, 10, 4)", .{future});
try testing.expectApproxEqAbs(@as(f64, 1000.0), try testEval(source), 1e-6);
}
test "financial: compound interest solves for its rate and its time" {
// The same relationship as fv(), read backwards.
try testing.expectApproxEqAbs(
@as(f64, 5.0),
try testEval("compound_rate(1000, 1628.894627, 10)"),
1e-6,
);
try testing.expectApproxEqAbs(
@as(f64, 10.0),
try testEval("compound_years(1000, 1628.894627, 5)"),
1e-6,
);
// With a compounding frequency the rate is nominal, so it is lower.
const monthly = try testEval("compound_rate(1000, 2000, 10, 12)");
const annual = try testEval("compound_rate(1000, 2000, 10)");
try testing.expect(monthly < annual);
try testing.expectApproxEqAbs(@as(f64, 6.95152928), monthly, 1e-8);
// At annual compounding, solving for the rate is CAGR.
try testing.expectApproxEqAbs(
try testEval("cagr(10000, 25000, 5) * 100"),
try testEval("compound_rate(10000, 25000, 5)"),
1e-9,
);
}
test "financial: apy converts a nominal rate to an effective one" {
try testing.expectApproxEqAbs(@as(f64, 19.5618), try testEval("apy(18, 12)"), 1e-4);
// Annual compounding is its own effective rate.
try testing.expectApproxEqAbs(@as(f64, 5.0), try testEval("apy(5, 1)"), 1e-12);
// And it composes, which is the point of exposing it as a function.
try testing.expectApproxEqAbs(
@as(f64, 19.5618),
try testEval("apy(compound_rate(1000, fv(1000, 18, 5, 12), 5, 12), 12)"),
1e-4,
);
try testing.expectError(Error.DomainError, testEval("apy(5, 0)"));
}
test "financial: the tvm solvers are reachable as four-argument functions" {
// 200,000 at 0.5% a period over 360 periods: the classic mortgage payment.
try testing.expectApproxEqAbs(
@as(f64, -1199.10105),
try testEval("tvm_pmt(360, 0.5, 200000, 0)"),
1e-5,
);
try testing.expectApproxEqAbs(@as(f64, 7.1773462), try testEval("tvm_rate(10, -1000, 0, 2000)"), 1e-6);
try testing.expectApproxEqAbs(@as(f64, 10.244768), try testEval("tvm_n(7, -1000, 0, 2000)"), 1e-6);
try testing.expectApproxEqAbs(@as(f64, 1257.789254), try testEval("tvm_fv(10, 5, 0, -100)"), 1e-6);
try testing.expectApproxEqAbs(@as(f64, -1016.698584), try testEval("tvm_pv(10, 7, 0, 2000)"), 1e-6);
}
test "financial: amortization rows are reachable from an expression" {
try testing.expectEqual(@as(f64, 1199.10), try testEval("amort_payment(200000, 0.5, 360)"));
// Month one of a 6% loan on 200,000 is exactly 1000 of interest.
try testing.expectEqual(@as(f64, 1000.0), try testEval("amort_interest(200000, 0.5, 360, 1)"));
try testing.expectApproxEqAbs(@as(f64, 199.10), try testEval("amort_principal(200000, 0.5, 360, 1)"), 1e-9);
try testing.expectApproxEqAbs(@as(f64, 199800.90), try testEval("amort_balance(200000, 0.5, 360, 1)"), 1e-9);
try testing.expectEqual(@as(f64, 0.0), try testEval("amort_balance(200000, 0.5, 360, 360)"));
}
test "financial: amortization totals" {
const interest = try testEval("amort_total_interest(200000, 0.5, 360)");
const paid = try testEval("amort_total_paid(200000, 0.5, 360)");
try testing.expect(interest > 231000 and interest < 232000);
try testing.expectApproxEqAbs(paid - interest, 200000, 0.05);
}
test "financial: results are inexact, so they do not claim exactness" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer _ = arena.deinit();
const alloc = arena.allocator();
var env = Environment.init(alloc);
defer env.deinit();
const result = try evalString(&env, alloc, "cagr(1000, 2000, 10)");
try testing.expect(!result.isExact());
}
test "financial: bad arguments are domain errors, not wrong answers" {
// Zero periods.
try testing.expectError(Error.DomainError, testEval("cagr(1000, 2000, 0)"));
// A fractional period count cannot index an amortization schedule.
try testing.expectError(Error.DomainError, testEval("amort_interest(200000, 0.5, 360.5, 1)"));
// Period past the end of the schedule.
try testing.expectError(Error.DomainError, testEval("amort_balance(200000, 0.5, 360, 361)"));
// Payments that never retire the loan.
try testing.expectError(Error.DomainError, testEval("amort_payment(0, 0.5, 360)"));
// Period counts outside the schedule bounds.
try testing.expectError(Error.DomainError, testEval("amort_payment(200000, 0.5, 0)"));
try testing.expectError(Error.DomainError, testEval("amort_payment(200000, 0.5, 20000)"));
try testing.expectError(Error.DomainError, testEval("amort_payment(200000, 0.5, 10^400)"));
}
test "financial: wrong argument counts are unknown functions, not silent defaults" {
try testing.expectError(Error.UnknownFunction, testEval("cagr(10000, 25000)"));
try testing.expectError(Error.UnknownFunction, testEval("tvm_pmt(360, 0.5, 200000)"));
try testing.expectError(Error.UnknownFunction, testEval("amort_payment(200000, 0.5, 360, 1)"));
// A three or four argument call to something that is not a function at all.
try testing.expectError(Error.UnknownFunction, testEval("nope(1, 2, 3)"));
try testing.expectError(Error.UnknownFunction, testEval("nope(1, 2, 3, 4)"));
}
// -- The AST is not the caller's problem --
//
// These use testing.allocator directly rather than testEval's arena, because an
// arena hides exactly the bug they guard against: evalStringInfo used to leak the
// whole parsed tree on every call, which an arena silently absorbs.
test "no leak: a successful evaluation releases the parsed tree" {
var env = Environment.init(testing.allocator);
defer env.deinit();
const sources = [_][]const u8{
"1 + 2 * 3",
"-(4 + 5)",
"max(1, min(2, 3))",
"sqrt(2) + factorial(10)",
"X = 7 * 6",
"X + 1",
"cagr(10000, 25000, 5) * 100",
"amort_interest(200000, 0.5, 360, 1)",
"0xFF and 0x0F",
};
for (sources) |source| {
var value = try evalString(&env, testing.allocator, source);
value.deinit();
}
}
test "no leak: a failed evaluation releases the parsed tree" {
var env = Environment.init(testing.allocator);
defer env.deinit();
const sources = [_][]const u8{
"2 +",
"(1 + 2",
"max(1, 2",
"1 / 0",
"unknownfn(1)",
"undefined_variable + 1",
"sqrt(-1)",
"cagr(1000, 2000, 0)",
};
for (sources) |source| {
if (evalString(&env, testing.allocator, source)) |value| {
var owned = value;
owned.deinit();
std.debug.print("expected an error for \"{s}\"\n", .{source});
return error.TestUnexpectedResult;
} else |_| {}
}
}
test "no leak: repeated evaluation does not accumulate" {
// A long interactive session is the case that made this visible: the TUI
// evaluates on every Enter and never frees anything itself.
var env = Environment.init(testing.allocator);
defer env.deinit();
var i: usize = 0;
while (i < 200) : (i += 1) {
var value = try evalString(&env, testing.allocator, "Ans + 1");
value.deinit();
}
}
// -- Function arguments versus grouped digits --
//
// The tokenizer used to treat any comma followed by a digit as a thousands
// separator, so a call written without spaces silently became a call with one
// merged argument. These are the end-to-end cases: what a user types, and what
// they get.
test "function arguments survive a comma with no space after it" {
// log(100, 10) is 2. The old rule lexed this as log(10010) and returned
// 4.0004, which is a wrong answer rather than an error.
try testing.expectApproxEqAbs(@as(f64, 2.0), try testEval("log(100,10)"), 1e-12);
try testing.expectEqual(@as(f64, 2.0), try testEval("max(1,2)"));
try testing.expectEqual(@as(f64, 1.0), try testEval("min(1,2)"));
try testing.expectApproxEqAbs(@as(f64, 0.2011244), try testEval("cagr(10000,25000,5)"), 1e-7);
try testing.expectApproxEqAbs(
@as(f64, -1199.10105),
try testEval("tvm_pmt(360,0.5,200000,0)"),
1e-5,
);
// Whitespace must not change the meaning.
try testing.expectEqual(try testEval("max(1, 2)"), try testEval("max(1,2)"));
try testing.expectEqual(try testEval("log(100, 10)"), try testEval("log(100,10)"));
}
test "grouped digits still work, inside and outside a call" {
try testing.expectEqual(@as(f64, 2000.0), try testEval("1,000 * 2"));
try testing.expectEqual(@as(f64, 1234568.0), try testEval("1,234,567 + 1"));
// A grouped argument is one argument.
try testing.expectEqual(@as(f64, 1000.0), try testEval("max(1,000, 500)"));
try testing.expectEqual(@as(f64, 1500.0), try testEval("min(1,500, 2,000)"));
}
test "a malformed group is an error, not a silently merged number" {
// Two digits after the comma is neither a group nor a valid argument list
// here, so it fails loudly instead of evaluating as 100.
try testing.expectError(Error.UnexpectedToken, testEval("1,00"));
try testing.expectError(Error.UnexpectedToken, testEval("1,0000"));
try testing.expectError(Error.UnexpectedToken, testEval("2+3,4"));
}
test "a grouped literal past 2^53 is still exact" {
// The separators have to reach the exact re-parse, not just the f64 channel.
var env = Environment.init(testing.allocator);
defer env.deinit();
var value = try evalString(&env, testing.allocator, "9,007,199,254,740,993");
defer value.deinit();
try testing.expect(value.isExact());
const shown = try @import("formatter.zig").formatNumber(testing.allocator, value);
defer shown.deinit(testing.allocator);
try testing.expectEqualStrings("9007199254740993", shown.raw);
}
// -- Operands that do not fit a machine word --
//
// The bitwise operators project through f64 and then convert to an integer. That
// conversion used to be unchecked, so ordinary input aborted the process:
// `2^64 and 1` and `~1e30` both died with "integer part of floating point value
// out of bounds", and a NaN operand produced a garbage number instead.
test "bitwise operands outside i64 report overflow instead of aborting" {
const cases = [_][]const u8{
"2^64 and 1",
"1e30 and 1",
"1e30 or 1",
"1e30 xor 1",
"~1e30",
"~(2^1000)",
"1e30 rol 1",
"1e30 ror 1",
"1e30 << 1",
"1e30 >> 1",
"1 << 1e30",
"1 rol 1e30",
"0 - 1e30 and 1",
};
for (cases) |source| {
const result = testEval(source);
try testing.expectError(Error.Overflow, result);
}
}
test "a non-finite bitwise operand is a domain error" {
// ln(-1) is NaN, and 1/0 raises before it can reach here, so NaN arrives via
// the transcendental fallback.
try testing.expectError(Error.DomainError, testEval("~ln(-1)"));
try testing.expectError(Error.DomainError, testEval("ln(-1) and 1"));
try testing.expectError(Error.DomainError, testEval("1 << ln(-1)"));
}
test "bitwise operators still work at the edges of the range" {
// Just inside i64: 2^63 - 1 as a float is 2^63, so use 2^62 for a clean case.
try testing.expectEqual(@as(f64, 0.0), try testEval("2^62 and 1"));
try testing.expectEqual(@as(f64, 15.0), try testEval("0xFF and 0x0F"));
try testing.expectEqual(@as(f64, 8.0), try testEval("1 << 3"));
try testing.expectEqual(@as(f64, -1.0), try testEval("~0"));
// Negative operands are fine; they are two's complement bit patterns.
try testing.expectEqual(@as(f64, -2.0), try testEval("~1"));
}