Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ArithmeticException: Division is undefined

Tags:

java

I have a simple operation going on in my program:

exposureNoDecimals =
    BigDecimal.valueOf(curEffSpreadPremium).multiply(BigDecimal.valueOf(100)).divide(wsRate, 0,
        java.math.RoundingMode.HALF_UP).longValue();

exposureNoDecimals - long curEffSpreadPremium - long wsRate - BigDecimal

However I am getting

"java.lang.ArithmeticException: Division is undefined" 
  at java.math.BigDecimal.longScaledDivide(BigDecimal.java:3105)
  at java.math.BigDecimal.divide(BigDecimal.java:2409)
  at java.math.BigDecimal.divide(BigDecimal.java:2396)
  at java.math.BigDecimal.divide(BigDecimal.java:2361)

The problem is the issue is recreatable on production and not on my machine (cant debug, or cant see the inputs)

What can be the issue here? Any suggestions/ideas?

like image 930
CHS Avatar asked Dec 17 '15 07:12

CHS


People also ask

What Division is undefined?

We can say that zero over zero equals "undefined." And of course, last but not least, that we're a lot of times faced with, is 1 divided by zero, which is still undefined.

What does Java Lang ArithmeticException by zero mean?

ArithmeticException is an unchecked exception in Java. Usually, one would come across java. lang. ArithmeticException: / by zero which occurs when an attempt is made to divide two numbers and the number in the denominator is zero. ArithmeticException objects may be constructed by the JVM.


2 Answers

Take a look at the source code for BigDecimal (e.g. here).

An ArithmeticException is only thrown with the message "Division undefined" when you attempt to divide zero by zero.

I'm not going to suggest a fix, because the >>correct<< fix will depend on what this calculation is supposed to be doing, and why the divisor / dividend happen to be zero. Putting in some zero checks might be a solution, but it could also be a "band-aid solution" that hides the problem rather than fixing it. It could come back to bite you later on.


The problem is the issue is recreatable on production and not on my machine (cant debug, or cant see the inputs)

As noted in various comments, there are different versions of BigDecimal depending on the Java version and (apparently) vendor. One of the differences between (some) versions is that the exception messages differ.

If you really want to track this down this reproducibility issue, you are going to have to look at the source code for BigDecimal in production and on your machine. (Unfortunately, a stacktrace involving Java SE classes is often difficult to diagnose without precise Java vendor and version number information. It is not helpful in this case ... for that reason.)

like image 114
Stephen C Avatar answered Sep 24 '22 05:09

Stephen C


According to the source code of BigDecimal, java.lang.ArithmeticException: Division undefined (without the is) is only thrown when you divide zero by zero.

Looks like in your case curEffSpreadPremium and wsRate both are zero.

So you need to guard the line with zero-checks.

like image 41
Hendrik Avatar answered Sep 24 '22 05:09

Hendrik