Nan in Java

Source: Internet
Author: User

Nan is a constant value, indicating that an object is not a quantity. However, this value can only exist in values of double and float floating point variables.

The following is a reprinted Article, which is probably sufficient to illustrate this topic:

When I debugged the program today, I accidentally found Nan in the floating point number of Java, and kept a record.

Java code

  1. /**
  2. * Test the Nan in the Java floating point number.
  3. * @ Author ayis
  4. *
  5. * Feb 23,200 9
  6. */
  7. Public class testnan {
  8. Public static
    Void main (string ARGs []) {
  9. // There is a nan value in the double and float types
  10. Double A = 0, B =
    0, C;
  11. C = A/(A + B );
  12. System. Out. println (C );
  13. // In int and long types, there is no nan value. Release the annotation and an exception will be thrown here ---/by zero
  14. // Int A = 0, B = 0, C;
  15. // C = A/(A + B );
  16. // System. Out. println (C );
  17. // If Nan is compared with any number, false is returned.
  18. If (0> C) | (0 = C) | (0 <C )){
  19. System. Out. println ("Nan compared with 0 is not always false .");
  20. } Else {
  21. System. Out. println ("Nan compared with 0 is always false! ");
  22. }
  23. }
  24. }
/*** Test the Nan * @ author ayis ** Feb 23,200 9 */public class testnan {public static void main (string ARGs []) in the Java floating point number. {// In the double and float types, there are Nan values of Double A = 0, B = 0, C; C = A/(a + B); system. out. println (c); // In the int and long types, there is no nan value. Release the annotation. An exception will be thrown here ---/by zero // int A = 0, B = 0, c; // C = A/(a + B); // system. out. println (c); // returns falseif (0> C) when comparing nan with any number | (0 = C) | (0 <C) {system. out. println ("Nan compared With 0 is not always false. ");} else {system. Out. println (" Nan compared with 0 is always false! ");}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.