diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-24 14:11:06 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-24 14:11:06 +0300 |
| commit | 0710597ccd639c37f4afe6aa3c9081b7229b6066 (patch) | |
| tree | b6008fafe92f69bbebfe0bc3200540a893a390ad | |
| parent | c95b15346def9fe1b34615b464f6a86046c0819b (diff) | |
fix(rpn): extract builtInConstants map, eliminate loadBuiltInConstants dual responsibility (sj)
Move built-in constants into a package-level builtInConstants map so
loadBuiltInConstants() only copies from it (no return value). Reload
and Clear now simply clear and copy from the source map, removing the
maps.DeleteFunc dance and the unused maps import.
| -rw-r--r-- | internal/rpn/constants.go | 141 |
1 files changed, 49 insertions, 92 deletions
diff --git a/internal/rpn/constants.go b/internal/rpn/constants.go index d497076..71899de 100644 --- a/internal/rpn/constants.go +++ b/internal/rpn/constants.go @@ -5,7 +5,7 @@ package rpn import ( "cmp" - "maps" + "math" "slices" "sync" @@ -59,107 +59,68 @@ type Constants struct { constants map[string]float64 } -// NewConstants creates and initializes a new Constants instance with built-in constants. -func NewConstants() *Constants { - c := &Constants{ - constants: make(map[string]float64), - } - c.loadBuiltInConstants() - return c -} - -// 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{}) +// builtInConstants is the immutable set of standard mathematical constants. +var builtInConstants = map[string]float64{ // Pi (π) - ratio of a circle's circumference to its diameter - c.constants["pi"] = math.Pi - builtIns["pi"] = struct{}{} - c.constants["π"] = math.Pi - builtIns["π"] = struct{}{} - + "pi": math.Pi, + "π": math.Pi, // Euler's number (e) - base of natural logarithm - c.constants["e"] = math.E - builtIns["e"] = struct{}{} - c.constants["euler"] = math.E - builtIns["euler"] = struct{}{} - + "e": math.E, + "euler": math.E, // Golden ratio (φ) - c.constants["phi"] = 1.618033988749895 - builtIns["phi"] = struct{}{} - c.constants["φ"] = 1.618033988749895 - builtIns["φ"] = struct{}{} - + "phi": 1.618033988749895, + "φ": 1.618033988749895, // Square root of 2 - c.constants["sqrt2"] = 1.414213562373095 - builtIns["sqrt2"] = struct{}{} - c.constants["√2"] = 1.414213562373095 - builtIns["√2"] = struct{}{} - + "sqrt2": 1.414213562373095, + "√2": 1.414213562373095, // Square root of 3 - c.constants["sqrt3"] = 1.732050807568877 - builtIns["sqrt3"] = struct{}{} - c.constants["√3"] = 1.732050807568877 - builtIns["√3"] = struct{}{} - + "sqrt3": 1.732050807568877, + "√3": 1.732050807568877, // Square root of 5 - c.constants["sqrt5"] = 2.23606797749979 - builtIns["sqrt5"] = struct{}{} - c.constants["√5"] = 2.23606797749979 - builtIns["√5"] = struct{}{} - + "sqrt5": 2.23606797749979, + "√5": 2.23606797749979, // Natural logarithm of 2 - c.constants["ln2"] = 0.693147180559945 - builtIns["ln2"] = struct{}{} - c.constants["log2"] = 0.693147180559945 - builtIns["log2"] = struct{}{} - + "ln2": 0.693147180559945, + "log2": 0.693147180559945, // Natural logarithm of 10 - c.constants["ln10"] = 2.302585092994046 - builtIns["ln10"] = struct{}{} - c.constants["log10"] = 2.302585092994046 - builtIns["log10"] = struct{}{} - + "ln10": 2.302585092994046, + "log10": 2.302585092994046, // 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{}{} - + "log_e": 0.434294481903252, + "log_e10": 0.434294481903252, // Tau (2π) - circle constant - c.constants["tau"] = 2 * math.Pi - builtIns["tau"] = struct{}{} - c.constants["τ"] = 2 * math.Pi - builtIns["τ"] = struct{}{} - + "tau": 2 * math.Pi, + "τ": 2 * math.Pi, // Fraction 1/π - c.constants["1/π"] = 1 / math.Pi - builtIns["1/π"] = struct{}{} - c.constants["inv_pi"] = 1 / math.Pi - builtIns["inv_pi"] = struct{}{} - + "1/π": 1 / math.Pi, + "inv_pi": 1 / math.Pi, // 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{}{} - + "1/e": 1 / math.E, + "inv_e": 1 / math.E, // Infinity - c.constants["inf"] = math.Inf(1) - builtIns["inf"] = struct{}{} - c.constants["infinity"] = math.Inf(1) - builtIns["infinity"] = struct{}{} - + "inf": math.Inf(1), + "infinity": math.Inf(1), // Negative infinity - c.constants["-inf"] = math.Inf(-1) - builtIns["-inf"] = struct{}{} - c.constants["-infinity"] = math.Inf(-1) - builtIns["-infinity"] = struct{}{} - + "-inf": math.Inf(-1), + "-infinity": math.Inf(-1), // NaN (Not a Number) - c.constants["nan"] = math.NaN() - builtIns["nan"] = struct{}{} + "nan": math.NaN(), +} - return builtIns +// NewConstants creates and initializes a new Constants instance with built-in constants. +func NewConstants() *Constants { + c := &Constants{ + constants: make(map[string]float64), + } + c.loadBuiltInConstants() + return c +} + +// loadBuiltInConstants copies the built-in constants into the constants map. +func (c *Constants) loadBuiltInConstants() { + for name, value := range builtInConstants { + c.constants[name] = value + } } // SetConstant assigns a value to a constant name. @@ -215,12 +176,8 @@ func (c *Constants) ReloadBuiltInConstants() { c.mu.Lock() defer c.mu.Unlock() - builtIns := c.loadBuiltInConstants() - - maps.DeleteFunc(c.constants, func(k string, _ float64) bool { - _, ok := builtIns[k] - return !ok - }) + clear(c.constants) + c.loadBuiltInConstants() } // Count returns the number of defined constants. |
