summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-24 10:34:05 +0300
committerPaul Buetow <paul@buetow.org>2026-05-24 10:34:05 +0300
commitd1a11d36a25c79384d57e23962f56652bbad1dd1 (patch)
treefcd7d33101b37ffe3532dd8e32a111b33dafd6e6
parentfc071785f821395cf213d760654abb4f115f119c (diff)
rpn: derive builtin set from loadBuiltInConstants, eliminating duplicate list
loadBuiltInConstants now returns a map[string]struct{} of keys it set. ReloadBuiltInConstants uses that returned set to filter user-defined constants, so adding a new builtin only requires one edit instead of keeping two hardcoded lists in sync.
-rw-r--r--internal/rpn/constants.go56
1 files changed, 35 insertions, 21 deletions
diff --git a/internal/rpn/constants.go b/internal/rpn/constants.go
index 18d9ff3..fd15c2a 100644
--- a/internal/rpn/constants.go
+++ b/internal/rpn/constants.go
@@ -44,66 +44,98 @@ func NewConstants() *Constants {
return c
}
-// loadBuiltInConstants loads the standard mathematical constants.
-func (c *Constants) loadBuiltInConstants() {
+// loadBuiltInConstants loads the standard mathematical constants and returns the set of keys it set.
+func (c *Constants) loadBuiltInConstants() map[string]struct{} {
+ builtIns := make(map[string]struct{})
// Pi (π) - ratio of a circle's circumference to its diameter
c.constants["pi"] = math.Pi
+ builtIns["pi"] = struct{}{}
c.constants["π"] = math.Pi
+ builtIns["π"] = struct{}{}
// Euler's number (e) - base of natural logarithm
c.constants["e"] = math.E
+ builtIns["e"] = struct{}{}
c.constants["euler"] = math.E
+ builtIns["euler"] = struct{}{}
// Golden ratio (φ)
c.constants["phi"] = 1.618033988749895
+ builtIns["phi"] = struct{}{}
c.constants["φ"] = 1.618033988749895
+ builtIns["φ"] = struct{}{}
// Square root of 2
c.constants["sqrt2"] = 1.414213562373095
+ builtIns["sqrt2"] = struct{}{}
c.constants["√2"] = 1.414213562373095
+ builtIns["√2"] = struct{}{}
// Square root of 3
c.constants["sqrt3"] = 1.732050807568877
+ builtIns["sqrt3"] = struct{}{}
c.constants["√3"] = 1.732050807568877
+ builtIns["√3"] = struct{}{}
// Square root of 5
c.constants["sqrt5"] = 2.23606797749979
+ builtIns["sqrt5"] = struct{}{}
c.constants["√5"] = 2.23606797749979
+ builtIns["√5"] = struct{}{}
// Natural logarithm of 2
c.constants["ln2"] = 0.693147180559945
+ builtIns["ln2"] = struct{}{}
c.constants["log2"] = 0.693147180559945
+ builtIns["log2"] = struct{}{}
// Natural logarithm of 10
c.constants["ln10"] = 2.302585092994046
+ builtIns["ln10"] = struct{}{}
c.constants["log10"] = 2.302585092994046
+ builtIns["log10"] = struct{}{}
// Logarithm of e base 10
c.constants["log_e"] = 0.434294481903252
+ builtIns["log_e"] = struct{}{}
c.constants["log_e10"] = 0.434294481903252
+ builtIns["log_e10"] = struct{}{}
// Tau (2π) - circle constant
c.constants["tau"] = 2 * math.Pi
+ builtIns["tau"] = struct{}{}
c.constants["τ"] = 2 * math.Pi
+ builtIns["τ"] = struct{}{}
// Fraction 1/π
c.constants["1/π"] = 1 / math.Pi
+ builtIns["1/π"] = struct{}{}
c.constants["inv_pi"] = 1 / math.Pi
+ builtIns["inv_pi"] = struct{}{}
// Fraction 1/e
c.constants["1/e"] = 1 / math.E
+ builtIns["1/e"] = struct{}{}
c.constants["inv_e"] = 1 / math.E
+ builtIns["inv_e"] = struct{}{}
// Infinity
c.constants["inf"] = math.Inf(1)
+ builtIns["inf"] = struct{}{}
c.constants["infinity"] = math.Inf(1)
+ builtIns["infinity"] = struct{}{}
// Negative infinity
c.constants["-inf"] = math.Inf(-1)
+ builtIns["-inf"] = struct{}{}
c.constants["-infinity"] = math.Inf(-1)
+ builtIns["-infinity"] = struct{}{}
// NaN (Not a Number)
c.constants["nan"] = math.NaN()
+ builtIns["nan"] = struct{}{}
+
+ return builtIns
}
// SetConstant assigns a value to a constant name.
@@ -159,25 +191,7 @@ func (c *Constants) ReloadBuiltInConstants() {
c.mu.Lock()
defer c.mu.Unlock()
- c.loadBuiltInConstants()
-
- builtIns := map[string]struct{}{
- "pi": {}, "π": {},
- "e": {}, "euler": {},
- "phi": {}, "φ": {},
- "sqrt2": {}, "√2": {},
- "sqrt3": {}, "√3": {},
- "sqrt5": {}, "√5": {},
- "ln2": {}, "log2": {},
- "ln10": {}, "log10": {},
- "log_e": {}, "log_e10": {},
- "tau": {}, "τ": {},
- "1/π": {}, "inv_pi": {},
- "1/e": {}, "inv_e": {},
- "inf": {}, "infinity": {},
- "-inf": {}, "-infinity": {},
- "nan": {},
- }
+ builtIns := c.loadBuiltInConstants()
maps.DeleteFunc(c.constants, func(k string, _ float64) bool {
_, ok := builtIns[k]