• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArithmeticException & Infinity

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When int a = 1/0; is given ArithmeticException is thrown whereas when float a = 1.0f/0.0f it gives answer as Infinity. Why?

Bhalchandra.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhalchandra Kalloorkar wrote:Hi,
When int a = 1/0; is given ArithmeticException is thrown whereas when float a = 1.0f/0.0f it gives answer as Infinity. Why?

Bhalchandra.




Java ints follow the two's complement standard -- which most languages / processors follow. There is no definition of infinity in the standard.

Java floats follow the IEEE 754 standard -- which most languages / processors follow. And that standard defines the result of a positive number divided by positive zero as positive infinity.

Henry
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch

Remember there are three kinds of arithmetic: integer, floating‑point and decimal (as used by BigDecimal). They all have different rules. You can read about them in the BigDecimal documentation and the Java Language Specification.

That may not be easy to read, however.
 
reply
    Bookmark Topic Watch Topic
  • New Topic