標籤:

在c語言中寫出變數=1./0.是啥意思?


在編譯期生成表示 +inf (正無窮)的 double 類型值。

http://en.cppreference.com/w/c/language/operator_arithmetic

If the second operand is zero, the behavior is undefined, except that if the IEEE floating-point arithmetic is supported, and the floating-point division is taking place, then

  • Dividing a non-zero number by ±0.0 gives the correctly-signed infinity and FE_DIVBYZERO is raised

但由於這是在編譯期計算,不會產生異常標記。


意思就是 double 1 除以 double 0。具體會造成什麼結果,得看標準文檔,看彙編沒用。

例如在 C99:

§6.5.5/5

The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

也就是說,只要除數的值為 0,行為就未定義。

但是,C99 在附件 F 中引入了對 IEC 60559 浮點數標準(即常說的 IEEE 754)的可選支持:只要編譯器定義了 __STDC_IEC_559__ 宏,就意味著會優先遵守 IEC 60559 中的相關標準。除以浮點數 0 的操作就不是未定義行為。

根據 IEC 60559 中的定義,1.0 / 0.0 應該是正無窮(即 math.h 中的 INFINITY)。


吃個 Positive Infinity。這個是794的標準。


我來說說這個1./0.的語法吧,題主是不是沒看懂啊

C語言裡面,表示一個浮點數,後面必須加上f,比如1.0f。如果不加f,默認是double型,比如,double d = 1.0;而如果寫float f = 1.0;那麼有些編譯器會提出警告,說將double型賦值給float會降低精度。1.0後面加f, 改成float f = 1.0f;即可消除警告。類似還有int,long等。

當doule型的小數位全是0時,即1.0, 1.000, 2.0, 2.000可以省略後面的0,簡寫為1. 1.,2., 2.

所以1./0.是簡寫, 意思就是 1.0 / 0.0


根據浮點數標準 結果的二進位中 標誌位為零 指數區全為 1 尾數區全為 0 表示正無窮


推薦閱讀:

有哪些比譚浩強主編的《C 語言程序設計(第四版)》能讓人更快學會 C 語言,且適合新手的書籍?
有C語言基礎,想把C語言學到極致,有什麼比較好的書籍推薦么?
老師讓我學習C,彙編,看c編譯成的彙編代碼,說以後幹什麼都難不倒。?
多線程編程的時候,使用無鎖結構會不會比有鎖結構更加快?
怎麼把彙編代碼自動轉換成C語言內聯彙編?

TAG:C編程語言 | CC |