您好,登錄后才能下訂單哦!
C++中怎么避免被0除,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
Reason(原因)
The result is undefined and probably a crash.
結(jié)果無定義,很可能會導致程序崩潰。
Note(注意)
This also applies to %.
本規(guī)則也適用于取余運算。
Example, bad(反面示例)
double divide(int a, int b)
{
// BAD, should be checked (e.g., in a precondition)
return a / b;
}
double divide(int a, int b)
{
// good, address via precondition (and replace with contracts once C++ gets them)
Expects(b != 0);
return a / b;
}
double divide(int a, int b)
{
// good, address via check
return b ? a / b : quiet_NaN<double>();
}
Alternative: For critical applications that can afford some overhead, use a range-checked integer and/or floating-point type.
可選項:對于能夠承受一定代價的要求嚴格的應用,可以考慮使用帶有范圍檢查的整數(shù)或者浮點數(shù)。
Enforcement(實施建議)
Flag division by an integral value that could be zero
標記可能為零的整數(shù)除數(shù)。
關(guān)于C++中怎么避免被0除問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。