Skip to content

Commit

Permalink
AVRO-2108: [Java] DoubleArray converts double to float (#3285)
Browse files Browse the repository at this point in the history
* fixing DoubleArray float cast

* add new line

* preventing boxing

---------

Co-authored-by: Martin Grigorov <[email protected]>
  • Loading branch information
sarthaksin1857 and martin-g authored Jan 21, 2025
1 parent d843fc5 commit fe0261d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Integer get(int i) {

/**
* Direct primitive int access.
*
*
* @param i : index.
* @return value at index.
*/
Expand Down Expand Up @@ -162,7 +162,7 @@ public Long get(int i) {

/**
* Direct primitive int access.
*
*
* @param i : index.
* @return value at index.
*/
Expand Down Expand Up @@ -273,7 +273,7 @@ public Boolean get(int i) {

/**
* Direct primitive int access.
*
*
* @param i : index.
* @return value at index.
*/
Expand Down Expand Up @@ -431,7 +431,7 @@ public Float get(int i) {

/**
* Direct primitive int access.
*
*
* @param i : index.
* @return value at index.
*/
Expand Down Expand Up @@ -535,7 +535,7 @@ public Double get(int i) {

/**
* Direct primitive int access.
*
*
* @param i : index.
* @return value at index.
*/
Expand All @@ -550,7 +550,7 @@ public void add(int location, Double o) {
if (o == null) {
return;
}
this.add(location, o.floatValue());
this.add(location, o.doubleValue());
}

public void add(int location, double o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,18 @@ void doubleArray() {
Assertions.assertEquals((101 - i) * 3.0d, doubleArray.get(i - 1));
}
}

@Test
void testDoubleArrayPreservesPrecisionForNonFloatRepresentableValues() {
final PrimitivesArrays.DoubleArray doubleArray = new PrimitivesArrays.DoubleArray(1,
Schema.createArray(Schema.create(Schema.Type.DOUBLE)));

// This value cannot be represented as a float
Double nonFloatDouble = .9;
Assertions.assertNotEquals(.9, nonFloatDouble.floatValue());

// Assert that the double array does not lose precision when adding
doubleArray.add(nonFloatDouble);
Assertions.assertEquals(nonFloatDouble, doubleArray.get(0));
}
}

0 comments on commit fe0261d

Please sign in to comment.