Skip to content

Add math built-ins (log, trig, constants) #467

@aallan

Description

@aallan

Summary

Vera has basic math functions (abs, min, max, floor, ceil, round, sqrt, pow) but is missing logarithmic functions, trigonometric functions, mathematical constants, and common numeric utilities.

Proposed API

Logarithmic

  • log(@Float64) -> @Float64 — natural logarithm
  • log2(@Float64) -> @Float64 — base-2 logarithm
  • log10(@Float64) -> @Float64 — base-10 logarithm

Trigonometric

  • sin(@Float64) -> @Float64
  • cos(@Float64) -> @Float64
  • tan(@Float64) -> @Float64
  • asin(@Float64) -> @Float64
  • acos(@Float64) -> @Float64
  • atan(@Float64) -> @Float64
  • atan2(@Float64, @Float64) -> @Float64

Constants

  • pi() -> @Float64 — 3.14159...
  • e() -> @Float64 — 2.71828...

Numeric utilities

  • sign(@Int) -> @Int — returns -1, 0, or 1
  • clamp(@Int, @Int, @Int) -> @Int — restrict value to [min, max] range
  • clamp_float(@Float64, @Float64, @Float64) -> @Float64 — float variant

Implementation

  • environment.py: Register as pure functions (no effects)
  • codegen/api.py: Host imports delegating to Python math module
  • WASM: Most map directly to f64 operations or can use math.log, math.sin, etc.
  • Browser runtime: Math.log(), Math.sin(), Math.PI, etc.
  • Verification: Standard Z3 real arithmetic axioms where available; trig functions are generally uninterpreted in Z3

Priority

Lower priority than array utilities and sleep/random, but these are standard library functions that every language provides. Useful for scientific computation, graphics, audio, and any problem involving geometry or exponential growth.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions