neo4j / neo4j-java-driver

Neo4j Bolt driver for Java
Apache License 2.0
330 stars 155 forks source link

Float to Double Conversion Issue in Values class #1561

Open garvit-joshi opened 4 months ago

garvit-joshi commented 4 months ago

Neo4j Version: 5.21.2 Neo4j Mode Single instance Driver Version: 5.22.0 Operating System Apple M2 Pro [Version 14.5 Sonoma]

Steps to reproduce

Create a float variable with a value that can't be precisely represented in binary (e.g., 17.3f) Pass this value to a method that expects a double Observe the value inside the method

Expected behavior

The float value should be converted to its exact double representation without loss of precision. Actual behavior The float value, when converted to double, shows more decimal places and a slightly different value. For example, 17.3f becomes 17.299999237060547. Code Example:

public class Example {
    public static void main(String[] args) {
        float b = 17.3f;
        hello((float) b);
    }

    public static void hello(double a) {
        System.out.println("A: " + a);
    }
}

Output: A: 17.299999237060547

Additional Context

This issue occurs in the Values.value(Object value) method:

public static Value value(Object value) {
    // ...
    if (value instanceof Double) {
        return value((double) value);
    }
    if (value instanceof Float) {
        return value((float) value);
    }
    // ...
}

The conversion from float to double may lead to a change in the visible decimal representation of the number.

Impact:

This issue affects the accuracy of data stored in Neo4j when working with float values, potentially leading to unexpected results in queries.

garvit-joshi commented 4 months ago

Hi @injectives, Would this be considered an issue or not? If It is, could I work on it?

garvit-joshi commented 1 month ago

Hi @injectives , Any update?