Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Division by zero: float vs. int (http://www.velocityreviews.com/forums/t137207-division-by-zero-float-vs-int.html)

Razvan 09-27-2004 11:43 AM

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

Michael Saunby 09-27-2004 11:55 AM

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



Tor Iver Wilhelmsen 09-27-2004 02:57 PM

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));
}
}

)

John C. Bollinger 09-27-2004 07:28 PM

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

Tor Iver Wilhelmsen 09-27-2004 08:35 PM

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?

Mark Thornton 09-27-2004 08:49 PM

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

=?ISO-8859-1?Q?Daniel_Sj=F6blom?= 09-27-2004 10:33 PM

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

Boudewijn Dijkstra 09-28-2004 10:41 AM

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.



Razvan 09-28-2004 11:58 AM

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

Stefan Schulz 09-28-2004 12:03 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57