topfunky/gruff — pull request #15 by vicvega (2011-11-09 11:48:57+00:00)
Title: Marker labels accuracy 

Maths with float numbers is imprecise. Look at
https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#wiki-floats_imprecise
or try yourself in irb

ruby-1.9.2-p290 :001 > 1 * 0.1
=> 0.1
ruby-1.9.2-p290 :002 > 2 * 0.1
=> 0.2
ruby-1.9.2-p290 :003 > 3 * 0.1
=> 0.30000000000000004 #WTF!!!
ruby-1.9.2-p290 :004 >

I used BigDecimal instead of Float to fixed accuracy while calculating marker labels.

ruby-1.9.2-p290 :004 > require 'bigdecimal'
=> true
ruby-1.9.2-p290 :005 > (BigDecimal(3.to_s) * BigDecimal(0.1.to_s)).to_f
=> 0.3

I added a simple test too.

The test code is here, just in case you want to to run it and have a quick look to the issue

g = Gruff::Line.new
g.title = "Marker label accuracy"
g.labels = { 0 => '1', 1 => '2', 2 => '3', 3 => '4', 4 => '5' }
g.data('first', [0.5, 0.51, 0.52, 0.53, 0.54])
g.data('second', [0.6, 0.61, 0.62, 0.63, 0.64])
g.data('third', [0.7, 0.71, 0.72, 0.73, 0.74])
g.write("test.png")