![]() |
Division by zero: float vs. int
Hi !
I have noticed that the division by zero is handled differently for integer and float types. Let me explain better: public class CDummy // this class prints "Infinity" (division by zero) { public static void main(String args[]) { System.out.println("CDummy."); float f1 = 1.41356f; float f2 = 0f; System.out.println("f1/f2 = " + (f1/f2)); } } public class CDummy // this class throws an exception (division by zero) { public static void main(String args[]) { System.out.println("CDummy."); int i1 = 1; int i2 = 0; System.out.println("i1/i2 = " + (i1/i2)); } } What is the reason for this behavior ? Why integers throw exceptions when dividing by zero while floats just assumes the number is Infinity (no exception is thrown) ? Regards, Razvan |
Re: Division by zero: float vs. int
"Razvan" <mihai11@mailcity.com> wrote in message news:15f19d61.0409270343.1f335ac8@posting.google.c om... [snip] > > What is the reason for this behavior ? Why integers throw exceptions > when dividing by zero while floats just assumes the number is Infinity > (no exception is thrown) ? > Floating point representations usually include +inf, -inf and even "Not a Number". Integer representations don't. The behaviour you're seeing isn't unique to Java, most programming languages will do something similar, because that's what the floating point hardware (or low level library) is doing. Michael Saunby |
Re: Division by zero: float vs. int
mihai11@mailcity.com (Razvan) writes:
> What is the reason for this behavior ? Why integers throw exceptions > when dividing by zero while floats just assumes the number is Infinity > (no exception is thrown) ? Because the IEEE standard for floating point numbers used has defined values for positive and negative infinity, and the special "not a number" case. See the contants in java.lang.Float and java.lang.Double for details. (NaN has some special properties the following class demonstrates: public class DoubleTest { public static void main(String[] _) { // Prints "false" System.out.println("Double.NaN == Double.NaN: " + (Double.NaN == Double.NaN)); // Prints NaN System.out.println("sqrt(-1.0) = "+Math.sqrt(-1.0)); } } ) |
Re: Division by zero: float vs. int
Michael Saunby wrote:
> "Razvan" <mihai11@mailcity.com> wrote in message > news:15f19d61.0409270343.1f335ac8@posting.google.c om... > > [snip] > > >>What is the reason for this behavior ? Why integers throw exceptions >>when dividing by zero while floats just assumes the number is Infinity >>(no exception is thrown) ? >> > > > Floating point representations usually include +inf, -inf and even "Not a > Number". Integer representations don't. The behaviour you're seeing isn't > unique to Java, most programming languages will do something similar, > because that's what the floating point hardware (or low level library) is > doing. And, lest there be any confusion, the Java specs mandate the observed behavior, regardless of what the underlying hardware may or may not do. John Bollinger jobollin@indiana.edu |
Re: Division by zero: float vs. int
"John C. Bollinger" <jobollin@indiana.edu> writes:
> And, lest there be any confusion, the Java specs mandate the observed > behavior, regardless of what the underlying hardware may or may not do. Isn't that what the "strictfp" keyword is for though? To protect yourself from any anomalies in the underlying hardware? |
Re: Division by zero: float vs. int
Tor Iver Wilhelmsen wrote:
> "John C. Bollinger" <jobollin@indiana.edu> writes: > > >>And, lest there be any confusion, the Java specs mandate the observed >>behavior, regardless of what the underlying hardware may or may not do. > > > Isn't that what the "strictfp" keyword is for though? To protect > yourself from any anomalies in the underlying hardware? No, even without strictfp, there remain quite tight rules on how Java is permitted to behave. Mark Thornton |
Re: Division by zero: float vs. int
Mark Thornton wrote:
> Tor Iver Wilhelmsen wrote: >> "John C. Bollinger" <jobollin@indiana.edu> writes: >> >>> And, lest there be any confusion, the Java specs mandate the observed >>> behavior, regardless of what the underlying hardware may or may not do. >> >> Isn't that what the "strictfp" keyword is for though? To protect >> yourself from any anomalies in the underlying hardware? > > No, even without strictfp, there remain quite tight rules on how Java is > permitted to behave. True. Strictfp only disallows using extended precision values for intermediate calculations. Non-strictfp code allows intermediate calculations in extended precision, but any assignments result in a conversion back to the standard precision value sets. Even without strictfp, the rules are quite strict, and are in conflict with the choices made in e.g. the x87 fpu (I seem to recall that the java floating point semantics are taken from some of Sun's fp implementations in software. Not surprising, really.). -- Daniel Sjöblom Remove _NOSPAM to reply by mail |
Re: Division by zero: float vs. int
"Razvan" <mihai11@mailcity.com> schreef in bericht
news:15f19d61.0409270343.1f335ac8@posting.google.c om... > Hi ! > > > > > I have noticed that the division by zero is handled differently for > integer and float types. > Let me explain better: > > > System.out.println("1f/0f = " + (1f/0f)); > System.out.println("1 /0 = " + (1 /0 )); > > What is the reason for this behavior ? Why integers throw exceptions > when dividing by zero while floats just assumes the number is Infinity > (no exception is thrown) ? From a mathematical point of view, this is because integers are always exact and floating point numbers are not. The idea is that you 'never' know what was truncated to fit it in a finite number of bits. |
Re: Division by zero: float vs. int
"Michael Saunby" <msaunby@despammed.com> wrote in message news:<cj8v46$og6$1$8300dec7@news.demon.co.uk>...
> "Razvan" <mihai11@mailcity.com> wrote in message > news:15f19d61.0409270343.1f335ac8@posting.google.c om... > > [snip] > > > > > What is the reason for this behavior ? Why integers throw exceptions > > when dividing by zero while floats just assumes the number is Infinity > > (no exception is thrown) ? > > > > Floating point representations usually include +inf, -inf and even "Not a > Number". Integer representations don't. I know that. My question was "why ?". Why we are treating integers and floats differenntly ? Why integers don't have NaN representions ? Certainly with integer operation you can get NaNs. Regards, Razvan |
Re: Division by zero: float vs. int
On 28 Sep 2004 04:58:30 -0700, Razvan <mihai11@mailcity.com> wrote:
> I know that. My question was "why ?". Why we are treating > integers and floats differenntly ? Why integers don't have NaN > representions ? Certainly with integer operation you can get NaNs. Simple reasoning. It is very simple to implement addition for integers. It is much harder for floating point values. Such "Special Cases" would destroy many, if not all sophisticated algorithms for integer addition. Since Floating Point arithmetic is a few leagues more complicated anyway, it is not such a big issue here to have special cases. -- Whom the gods wish to destroy they first call promising. |
| All times are GMT. The time now is 11:29 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.