關於在Java中 a!=a 值為真的解釋(摘抄)

來源:互聯網
上載者:User
關於在Java中 a!=a 值為真的解釋

    In java spec , the primitive type is implemented in a certain defined way. The floating-types are implemented in IEEE-754 standard.
    Ref from java spec: 
        The IEEE 754 standard includes not only positive and negative sign-magnitude numbers, but also positive and negative zeros, positive and negative infinities, and a special Not-a-Number value (hereafter abbreviated as "NaN"). The NaN value is used to represent the result of certain invalid operations such as dividing zero by zero. 
        So, in this case , java specified that a!=a is true if  the variable is NaN. So does the case i present yesterday (a<b)==(a>=b) if either a or b is NaN.
         Now I wil give you a example show it:
         .........
         double a=0;
         double b=0;
          a=a/b;
          if(a!=a) System.out.println("OK, a!=a is true!");
          if((a<b)==(a>=b)) System.out.println("........");

But I think the problem is a little tricky, If you have not yet read something from the java spec or something other, you will not  know that. But when the problem been proposed to us ,we should think about the problem in a logical way ,but not the way coding it to see if a=0, and then  a!=a is false. What i want to see is not the result of this ,but the thinking way of the job seeker. 

.net 2.0裡面也是這樣

Module Test
Sub Main()
Dim a as double=0
Dim b as double=0
a=a/b
console.writeline("a ={0}",a)
console.writeline("a<>a ={0}",a<>a)
console.writeline("a=a ={0}",a=a)
console.writeline("a<b ={0}",a<b)
console.writeline("a>b ={0}",a>b)
console.writeline("a=b ={0}",a=b)
End Sub
end module

結果:
a =非數字
a<>a =True
a=a =False
a<b =False
a>b =False
a=b =False

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.