« Return to Thread: Floating point imprecision in activerecord-jdbc-adapter?

Floating point imprecision in activerecord-jdbc-adapter?

by Justin Coyne :: Rate this Message:

Reply to Author | View in Thread

I've got an application where I run a query and get the expected result of 5.8 from the database when I run the program under MRI.  When I switch to jruby (and jdbcmysql-adapter) I'm getting the result 5.80000019073486.

Here's the code for MRI:

SPEC = { :username => 'test', :password => 'test', :database=>'test', :host=>'localhost'}
require 'test/unit'
require 'rubygems'
require 'active_record'  ## For AR::ConnectionNotEstablished
require "active_record/connection_adapters/mysql_adapter"

class TestPrecision < Test::Unit::TestCase

  def test_correct_precision
    connection = ActiveRecord::Base.send("mysql_connection", SPEC)
    assert_equal "5.8", connection.select_rows("SELECT density from samples WHERE name = 'br1991a'")[0][0]
    assert_equal "float", connection.raw_connection.query("SHOW FIELDS FROM samples").all_hashes.select{|field| field["Field"] == 'density'}.first["Type"]
  end
end

And here's the same for jruby:

SPEC = { :username => 'test', :password => 'test', :database=>'test', :host=>'localhost'}
require 'test/unit'
require 'rubygems'
require 'active_record'  ## For AR::ConnectionNotEstablished

gem 'activerecord-jdbcmysql-adapter'
require 'active_record/connection_adapters/jdbcmysql_adapter.rb'

class TestPrecision < Test::Unit::TestCase

  def test_jdbc_precision
    connection = ActiveRecord::Base.send("mysql_connection", SPEC)
    assert_equal 5.8, connection.select_rows("SELECT density from samples WHERE name = 'br1991a'")[0][0]
  end
end


The MRI test passes but the output of the jruby test is:

  1) Failure:
test_jdbc_precision(TestPrecision) [problem_jruby.rb:16]:
<5.8> expected but was
<5.80000019073486>.

1 tests, 1 assertions, 1 failures, 0 errors


Does anybody know what's going on?

-Justin


 « Return to Thread: Floating point imprecision in activerecord-jdbc-adapter?