You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using Indriya 2.0.1 and we have some troubles with multiply and divide methods when using decimal values.
For example : Quantities.getQuantity(0.6, Units.SQUARE_METRE).multiply(1.55)
=> Result: 0.9299999999999999 m² Expected: 0.93 m²
Since we are using two Doubles, we have a similar result than this test: assertThat(0.6 * 1.55).isEqualTo(0.93)
=> Expected :0.93 Actual :0.9299999999999999
Converting at least one of the number to BigDecimal seems to solve the issue Quantities.getQuantity(0.6, Units.SQUARE_METRE).multiply(BigDecimal.valueOf(1.55))
=> Result: 0.93 m²
But when we try to divide, converting to BigDecimal is not enough: Quantities.getQuantity(BigDecimal.valueOf(53.75), Units.SQUARE_METRE).divide(BigDecimal.valueOf(4.3))
=> Result: 12.500000000000000000000000000000000875 m² Expected: 12.5 m²
I found than converting to Indriya RationalNumber instead of BigDecimal solves the problem: Quantities.getQuantity(RationalNumber.of(53.75), Units.SQUARE_METRE).divide(RationalNumber.of(4.3))
=> Result: 12.5 m²
Is converting to RationalNumber before doing multiply/divide the right way to handle this problem?
For a little more context, we are encapsulating Indriya inside our own value object in a framework used by other colleagues.
We do not want them to have to manage and keep track of what object was created with what type of number or even force them to cast their number.
They give us any kind of number and we will do any necessary casting or converting needed.
As example of API we propose:
We can create an Area with SQUARE_METRE as unit
public static Area squareMeter(Number value){
return new Area(getQuantity(checkNotNull(value), SQUARE_METER.uomUnit));
}
with SQUARE_METER.uomUnit an enum which returns a javax.measure.Unit<javax.measure.quantity.Area> (here, tech.units.indriya.unit.Units.SQUARE_METRE).
And we expect these lines to be true:
This API is exposed to people who do not even know there is a difference between a Integer and a Double, when they know Double exists. So we need to keep the usage as simple as possible.
Hope that's clear enough, I'm at your disposal if you need clarification
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
We are using Indriya 2.0.1 and we have some troubles with multiply and divide methods when using decimal values.
For example :
Quantities.getQuantity(0.6, Units.SQUARE_METRE).multiply(1.55)
=>
Result: 0.9299999999999999 m² Expected: 0.93 m²
Since we are using two Doubles, we have a similar result than this test:
assertThat(0.6 * 1.55).isEqualTo(0.93)
=>
Expected :0.93 Actual :0.9299999999999999
Converting at least one of the number to BigDecimal seems to solve the issue
Quantities.getQuantity(0.6, Units.SQUARE_METRE).multiply(BigDecimal.valueOf(1.55))
=>
Result: 0.93 m²
But when we try to divide, converting to BigDecimal is not enough:
Quantities.getQuantity(BigDecimal.valueOf(53.75), Units.SQUARE_METRE).divide(BigDecimal.valueOf(4.3))
=>
Result: 12.500000000000000000000000000000000875 m² Expected: 12.5 m²
I found than converting to Indriya RationalNumber instead of BigDecimal solves the problem:
Quantities.getQuantity(RationalNumber.of(53.75), Units.SQUARE_METRE).divide(RationalNumber.of(4.3))
=>
Result: 12.5 m²
Is converting to RationalNumber before doing multiply/divide the right way to handle this problem?
For a little more context, we are encapsulating Indriya inside our own value object in a framework used by other colleagues.
We do not want them to have to manage and keep track of what object was created with what type of number or even force them to cast their number.
They give us any kind of number and we will do any necessary casting or converting needed.
As example of API we propose:
We can create an Area with SQUARE_METRE as unit
with SQUARE_METER.uomUnit an enum which returns a javax.measure.Unit<javax.measure.quantity.Area> (here, tech.units.indriya.unit.Units.SQUARE_METRE).
And we expect these lines to be true:
This API is exposed to people who do not even know there is a difference between a Integer and a Double, when they know Double exists. So we need to keep the usage as simple as possible.
Hope that's clear enough, I'm at your disposal if you need clarification
Beta Was this translation helpful? Give feedback.
All reactions