本文介紹了如何檢查零是正數還是負數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
是否可以檢查 float
是正零 (0.0) 還是負零 (-0.0)?
Is it possible to check if a float
is a positive zero (0.0) or a negative zero (-0.0)?
我已將 float
轉換為 String
并檢查第一個 char
是否為 '-'
,但是還有其他方法嗎?
I've converted the float
to a String
and checked if the first char
is a '-'
, but are there any other ways?
推薦答案
是的,除以它.1/+0.0f
是 +Infinity
,但 1/-0.0f
是 -Infinity
.通過簡單的比較很容易找出是哪一個,所以你得到:
Yes, divide by it. 1 / +0.0f
is +Infinity
, but 1 / -0.0f
is -Infinity
. It's easy to find out which one it is with a simple comparison, so you get:
if (1 / x > 0)
// +0 here
else
// -0 here
(這里假設 x
只能是兩個零之一)
(this assumes that x
can only be one of the two zeroes)
這篇關于如何檢查零是正數還是負數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!