tools4j / decimal4j

Java library for fast fixed-point arithmetic based on longs with support for up to 18 decimal places.
decimal4j.org
MIT License
156 stars 17 forks source link

Overflow for conversion from double to decimal with scale x: yEz #15

Closed oliviersinnaeve closed 5 years ago

oliviersinnaeve commented 5 years ago

Trying to do the following simple multiplication gave next exception :

500000000 * 0,00000010

java.lang.IllegalArgumentException: Overflow for conversion from double to decimal with scale 12: 5.0E8\n\tat org.decimal4j.arithmetic.DoubleConversion.newOverflowException(DoubleConversion.java:538)\n\tat org.decimal4j.arithmetic.DoubleConversion.doubleToUnscaledShiftRight(DoubleConversion.java:287)\n\tat org.decimal4j.arithmetic.DoubleConversion.doubleToUnscaledShift(DoubleConversion.java:260)\n\tat org.decimal4j.arithmetic.DoubleConversion.doubleToUnscaled(DoubleConversion.java:235)\n\tat org.decimal4j.arithmetic.CheckedScaleNfRoundingArithmetic.fromDouble(CheckedScaleNfRoundingArithmetic.java:176)\n\tat org.decimal4j.immutable.Decimal12f.valueOf(Decimal12f.java:321)\n\tat com.b2beyond.util.PriceUtil.multiply(PriceUtil.java:36)\n\tat com.b2beyond.trade.dispatch.websocket.model.TradeRequest.getCalculatedFee(TradeRequest.java:166)

terzerm commented 5 years ago

Hi

It is much simpler to give some accurate feedback to your problem if you state the actual code that you are trying to run.

Given the information you provided, yes it is correct that 500 million does not fit in a 12 scale decimal as can be seen in the MAX_VALUE constant of Decimal12f: http://decimal4j.org/javadoc/1.0.3/org/decimal4j/immutable/Decimal12f.html#MAX_VALUE

The way to perform your calculation depends a bit on the problem, but in your case it would certainly be possible to use the multiply by long method:

Decimal12f.valueOf(0.00000010).multiply(500000000)
oliviersinnaeve commented 5 years ago

Anyways, imho your library sucks, therefore you don't got a great community, the amounts I provided you were simple numbers, not even about decimals, just whole numbers and your library failed !! My own implementation is much easier, just needed some tests !!

Good luck in your career ...

On Sun, Apr 28, 2019 at 4:45 PM Marco Terzer notifications@github.com wrote:

Closed #15 https://github.com/tools4j/decimal4j/issues/15.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tools4j/decimal4j/issues/15#event-2305292544, or mute the thread https://github.com/notifications/unsubscribe-auth/AAK7K5S2RPAJKWVLXR5ORITPSW2BVANCNFSM4HG7JOSQ .

-- Olivier Sinnaeve Zaakvoerder Java Developer BVBA +32 472 56 17 69

terzerm commented 5 years ago
dcullender-cb commented 5 years ago

@terzerm I have used this library for 2 big projects and think it is great! Big Thanks for all the hard work you have put into it.

terzerm commented 5 years ago

@cullender-skew thank you I appreciate that!