From b8c6bd8cace5d89062a57acb2012b0be08fb9525 Mon Sep 17 00:00:00 2001 From: Rohit Gupta Date: Sun, 1 Jul 2018 00:31:59 +0530 Subject: [PATCH] better precision for small negative numbers - also fix BADPREC(bad precision) issue when 0 is max in series. --- asciigraph.go | 2 +- asciigraph_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/asciigraph.go b/asciigraph.go index 135aaa5..63defb6 100644 --- a/asciigraph.go +++ b/asciigraph.go @@ -54,7 +54,7 @@ func Plot(series []float64, options ...Option) string { } precision := 2 - logMaximum := math.Log10(math.Abs(maximum)) //to find number of zeros after decimal + logMaximum := math.Log10(math.Max(math.Abs(maximum), math.Abs(minimum))) //to find number of zeros after decimal if logMaximum < 0 { // negative log diff --git a/asciigraph_test.go b/asciigraph_test.go index 5e530f6..48fb140 100644 --- a/asciigraph_test.go +++ b/asciigraph_test.go @@ -129,6 +129,28 @@ func TestPlot(t *testing.T) { -1.70 ┤ ││ ││ ││ -3.00 ┤ ╰╯ ╰╯ ╰╯ I'm a doctor, not an engineer.`}, + { + []float64{-5, -2, -3, -4, 0, -5, -6, -7, -8, 0, -9, -3, -5, -2, -9, -3, -1}, + nil, + ` 0.00 ┤ ╭╮ ╭╮ + -1.00 ┤ ││ ││ ╭ + -2.00 ┤╭╮ ││ ││ ╭╮ │ + -3.00 ┤│╰╮││ ││╭╮││╭╯ + -4.00 ┤│ ╰╯│ │││││││ + -5.00 ┼╯ ╰╮ │││╰╯││ + -6.00 ┤ ╰╮ │││ ││ + -7.00 ┤ ╰╮│││ ││ + -8.00 ┤ ╰╯││ ││ + -9.00 ┼ ╰╯ ╰╯ `}, + { + []float64{-0.000018527, -0.021, -.00123, .00000021312, -.0434321234, -.032413241234, .0000234234}, + []Option{Height(5),Width(45)}, + ` 0.000 ┼─╮ ╭────────╮ ╭ + -0.008 ┤ ╰──╮ ╭──╯ ╰─╮ ╭─╯ + -0.017 ┤ ╰─────╯ ╰╮ ╭─╯ + -0.025 ┤ ╰─╮ ╭─╯ + -0.034 ┤ ╰╮ ╭────╯ + -0.042 ┼ ╰───╯ `}, } for i := range cases {