programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Floating point divide by zero

Ranch Hand
Posts: 52
• Number of slices to send:
Optional 'thank-you' note:
Can anybody explain whether the following statement is true or not.

Floating point modulo (%) by zero produces NaN , but floating point divide by zero does not.

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
Hi
Its true
Floating point divide by zero produces Infinity & Floating point modulo by zero produces NaN(not a number)

Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
does anyone know the reason behind this ?

Why arithmetic exception happens for int and not for float ?
[ March 28, 2007: Message edited by: JB Ramesh ]

author
Posts: 23928
142
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by JB Ramesh:
does anyone know the reason behind this ?

Why arithmetic exception happens for int and not for float ?

The behavior of float is actually defined by the IEEE standard. Java follows the same standard used by most floating point processors in use today.

As for int, it throws an exception, because there is no representation for infinity or NaN.

Henry

Srinivas Kumar
Ranch Hand
Posts: 52
• Number of slices to send:
Optional 'thank-you' note:
My actual doubt is that floating point module zero also does divide by zero internally. So why Nan is getting printed instead of Infinity?

 Police line, do not cross. Well, this tiny ad can go through: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising