Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java
In short, that's the way it's specified in the IEEE-754 standard, which is what Java's Floating-Point Operations are based on.
Why doesn't division by zero (or overflow, or underflow) stop the program or trigger an error? Why does a standard on numbers include "not-a-number" (NaN)?
The 754 model encourages robust programs. It is intended not only for numerical analysts but also for spreadsheet users, database systems, or even coffee pots. The propagation rules for NaNs and infinities allow inconsequential exceptions to vanish. Similarly, gradual underflow maintains error properties over a precision's range.
When exceptional situations need attention, they can be examined immediately via traps or at a convenient time via status flags. Traps can be used to stop a program, but unrecoverable situations are extremely rare. Simply stopping a program is not an option for embedded systems or network agents. More often, traps log diagnostic information or substitute valid results.
Flags offer both predictable control flow and speed. Their use requires the programmer be aware of exceptional conditions, but flag stickiness allows programmers to delay handling exceptional conditions until necessary.
Java division by zero doesnt throw an ArithmeticException - why?
Why can't you just check it yourself and throw an exception if that is what you want.
try {
for (int i = 0; i < tab.length; i++) {
tab[i] = 1.0 / tab[i];
if (tab[i] == Double.POSITIVE_INFINITY ||
tab[i] == Double.NEGATIVE_INFINITY)
throw new ArithmeticException();
}
} catch (ArithmeticException ae) {
System.out.println("ArithmeticException occured!");
}
Why doesn't Java throw an Exception when dividing by 0.0?
The result of division by zero is, mathematically speaking, undefined, which can be expressed with a float/double (as NaN
- not a number), it isn't, however, wrong in any fundamental sense.
As an integer must hold a specific numerical value, an error must be thrown on division by zero when dealing with them.
why there is no ArithmeticException( divide by zero) when both values are double?
The "magic" is that Java floating point representations are based on the IEE 754 floating point standard. This has a special value (NaN) that denotes the "indefinite value" that you get when zero is divided by zero. (There are also values that represent positive and negative infinity; e.g. 1.0 / 0.0
gives INF
- positive infinity.)
This is covered in the Java Language Specification; see sections §4.2.3 which discusses the representations and §4.2.4 which discusses how arithmetic works.
Note that the same "magic" applies to float
, double
, Float
and Double
.
Division by zero doesn't always throw java.lang.ArithmeticException
To understand the difference between your two cases, note that
(double) this.A / diviseur.A
is equivalent to
((double)this.A) / diviseur.A
since casting takes precedence over division.
So although A
is an int
you are doing a floating-point division that allows division by zero with a result of plus/minus infinity.
To the contrary 1/0
is a pure integer-division that should give an integer-result, so infinity would not be valid and the ArithmeticException
is thrown.
Why does integer division by zero 1/0 give error but floating point 1/0.0 returns Inf?
That's because integers don't have values for +/-Inf, NaN, and don't allow division by 0, while floats do have those special values.
LibGDX - Sprite from custom class is not showing up
your problem is in setSize() method in UiButton class:
public void setSize( float toWidth, float toHeight )
{
//System.out.println("super.width = " + super.getWidth() + ", super.height = " + super.getHeight() ); - you can uncomment it to see the super width/height value in the console
float scaleX = toWidth / super.getWidth( );
float scaleY = toHeight / super.getHeight( );
setScale( scaleX, scaleY );
}
Both super.getWidth() and super.getHeight() returns ZERO (because you haven't set any width/height to the super class object). Then in scaleX/scaleY variables you have infinity value - you can see it by uncommenting marked line in my code.
The funny thing is that Java allows you to divide by floating zero - returning mentioned infinity value (read more here) and that is why you don't have an Exception there.
The solution is to fix this method by calling super.setSize(width,height) or just to delete it at all and lean on default Sprite class setSize() method implementation.
One thing more - after fixing the issue you will observe problems with rotation - it is about origin. You can just change you setSize method to
@Override
public void setSize(float w, float h)
{
super.setSize(w,h);
setOrigin(w/2f, h/2f);
}
and it will do the thing
Related Topics
How to Flatten 2D Array to 1D Array
Pdfbox 2.0.2 > Calling of Pagedrawer.Processpage Method Caught Exceptions
In Log4J, Does Checking Isdebugenabled Before Logging Improve Performance
How to Solve Circular Reference in JSON Serializer Caused by Hibernate Bidirectional Mapping
What Is @Joincolumn and How It Is Used in Hibernate
JPA and Hibernate - Criteria VS. Jpql or Hql
How to Combine Two Lists into a Map (Java)
Trying to Read from the Console in Java
Why F Is Placed After Float Values
Use Custom Fonts When Creating PDF Using Ireport
Pass String as Params from One Java App to Another
How to Get the Threadpoolexecutor to Increase Threads to Max Before Queueing
Java "Lambda Expressions Not Supported at This Language Level"
Java: Random Long Number in 0 <= X < N Range
Hibernate - @Elementcollection - Strange Delete/Insert Behavior
Static Allocation in Java - Heap, Stack and Permanent Generation