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

Re: Floating point imprecision in activerecord-jdbc-adapter?

by Charles Oliver Nutter-4 :: Rate this Message:

Reply to Author | View in Thread

On Tue, Jul 7, 2009 at 2:26 PM, Justin Coyne<digger250@...> wrote:
> Okay, here's the real bug. It's not in jdbc at all. It appears to be deeper
> in the guts of jruby.
>
> jruby -e "require 'java';  puts
> JRuby.runtime.newFloat(java.lang.Float.new(5.8)).to_s()"
>
> 5.80000019073486

It looks like the conversion from single-precision float to
double-precision produces this effect.
java.lang.Float.new(5.8)doubleValue produces the same result.

Here's a longer demonstration:

import java.lang.reflect.*;

public class Floaty {
  public static void main(String[] args) {
    Float f = 5.8f;
    System.out.println(f);
    System.out.println(f.floatValue());
    System.out.println(f.doubleValue());
    System.out.println((double)f.floatValue());
    System.out.println(new Double(f.floatValue()).doubleValue());
    System.out.println((double)5.8f);
    System.out.println(new Double(5.8f));
    System.out.println(5.8);
    System.out.println(new Double(5.8));
    System.out.println(new Double("5.8"));
  }
}

And the output:

5.8
5.8
5.800000190734863
5.800000190734863
5.800000190734863
5.800000190734863
5.800000190734863
5.8
5.8
5.8

This starts to look like a "WONTFIX" or maybe a "CANTFIX" since it's
Java/JDK/JVM float-conversion behavior at play here. But if we're
getting floats out of the database somewhere in the JDBC adapters,
perhaps we should ask for Double or double instead?

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


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