Skip to content

Commit

Permalink
better precision for small negative numbers
Browse files Browse the repository at this point in the history
- also fix BADPREC(bad precision) issue when 0 is max in series.
  • Loading branch information
guptarohit committed Jun 30, 2018
1 parent b8f636e commit b8c6bd8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions asciigraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b8c6bd8

Please sign in to comment.