From 8922e7ea1995f755a18eb45a4d52f78718c5c57b Mon Sep 17 00:00:00 2001 From: Cloakd <2733949+cloakd@users.noreply.github.com> Date: Wed, 15 Oct 2025 13:18:37 +0100 Subject: [PATCH 1/2] Respect precision when set by caller for all numbers --- asciigraph.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/asciigraph.go b/asciigraph.go index 608eb6a..60417c7 100644 --- a/asciigraph.go +++ b/asciigraph.go @@ -104,21 +104,23 @@ func PlotMany(data [][]float64, options ...Option) string { } precision := config.Precision - logMaximum = math.Log10(math.Max(math.Abs(maximum), math.Abs(minimum))) //to find number of zeros after decimal - if minimum == float64(0) && maximum == float64(0) { - logMaximum = float64(-1) - } + if precision == 0 { + logMaximum = math.Log10(math.Max(math.Abs(maximum), math.Abs(minimum))) //to find number of zeros after decimal + if minimum == float64(0) && maximum == float64(0) { + logMaximum = float64(-1) + } - if logMaximum < 0 { - // negative log - if math.Mod(logMaximum, 1) != 0 { - // non-zero digits after decimal - precision += uint(math.Abs(logMaximum)) - } else { - precision += uint(math.Abs(logMaximum) - 1.0) + if logMaximum < 0 { + // negative log + if math.Mod(logMaximum, 1) != 0 { + // non-zero digits after decimal + precision += uint(math.Abs(logMaximum)) + } else { + precision += uint(math.Abs(logMaximum) - 1.0) + } + } else if logMaximum > 2 { + precision = 0 } - } else if logMaximum > 2 { - precision = 0 } maxNumLength := len(fmt.Sprintf("%0.*f", precision, maximum)) From e5b6833470c4df1d8a55d2a2482c5bfa942cb598 Mon Sep 17 00:00:00 2001 From: Cloakd Date: Wed, 15 Oct 2025 13:35:58 +0100 Subject: [PATCH 2/2] Fix tests vs option --- asciigraph.go | 36 +++++++++++++++++++----------------- options.go | 4 ++-- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/asciigraph.go b/asciigraph.go index 60417c7..d018ed1 100644 --- a/asciigraph.go +++ b/asciigraph.go @@ -17,7 +17,7 @@ func PlotMany(data [][]float64, options ...Option) string { var logMaximum float64 config := configure(config{ Offset: 3, - Precision: 2, + Precision: nil, }, options) // Create a deep copy of the input data @@ -103,24 +103,26 @@ func PlotMany(data [][]float64, options ...Option) string { plot[i] = line } - precision := config.Precision - if precision == 0 { - logMaximum = math.Log10(math.Max(math.Abs(maximum), math.Abs(minimum))) //to find number of zeros after decimal - if minimum == float64(0) && maximum == float64(0) { - logMaximum = float64(-1) - } + var precision uint = 2 //Default precision to maintain backwards compatibility + if config.Precision != nil { + precision = *config.Precision + } - if logMaximum < 0 { - // negative log - if math.Mod(logMaximum, 1) != 0 { - // non-zero digits after decimal - precision += uint(math.Abs(logMaximum)) - } else { - precision += uint(math.Abs(logMaximum) - 1.0) - } - } else if logMaximum > 2 { - precision = 0 + logMaximum = math.Log10(math.Max(math.Abs(maximum), math.Abs(minimum))) //to find number of zeros after decimal + if minimum == float64(0) && maximum == float64(0) { + logMaximum = float64(-1) + } + + if logMaximum < 0 { + // negative log + if math.Mod(logMaximum, 1) != 0 { + // non-zero digits after decimal + precision += uint(math.Abs(logMaximum)) + } else { + precision += uint(math.Abs(logMaximum) - 1.0) } + } else if logMaximum > 2 && config.Precision == nil { + precision = 0 } maxNumLength := len(fmt.Sprintf("%0.*f", precision, maximum)) diff --git a/options.go b/options.go index 17dd4d0..ae49ef1 100644 --- a/options.go +++ b/options.go @@ -15,7 +15,7 @@ type config struct { LowerBound, UpperBound *float64 Offset int Caption string - Precision uint + Precision *uint CaptionColor AnsiColor AxisColor AnsiColor LabelColor AnsiColor @@ -80,7 +80,7 @@ func Offset(o int) Option { // Precision sets the graphs precision. func Precision(p uint) Option { - return optionFunc(func(c *config) { c.Precision = p }) + return optionFunc(func(c *config) { c.Precision = &p }) } // Caption sets the graphs caption.