C++/C++11中std numeric limits的使用

有點模糊發表於2019-02-14
               

 在C/C++11中,std::numeric_limits為模板類,在庫編譯平臺提供基礎算術型別的極值等屬性資訊,取代傳統C語言,所採用的預處理常數。比較常用的使用是對於給定的基礎型別用來判斷在當前系統上的最大值、最小值。若使用此類,需包含<limits>標頭檔案。它支援的基礎算術型別包括如下:


 min、max與C庫巨集常量的關係如下:


 測試程式碼如下:

#include "numeric_limits.hpp"#include <limits>#include <iostream>///* reference: http://www.cplusplus.com/reference/limits/numeric_limits/ https://msdn.microsoft.com/en-us/library/c707ct0t.aspx*/int test_numeric_limits_1()std::cout << std::boolalpha; std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << std::endlstd::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << std::endlstd::cout << "int is signed: " << std::numeric_limits<int>::is_signed << std::endlstd::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << std::endlstd::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << std::endlstd::cout << "Minimum value for float: " << std::numeric_limits<float>::min() << std::endl; // min returns the smallest positive value the type can encode, not the lowest std::cout << "Lowest value for float: " << std::numeric_limits<float>::lowest() << std::endl; // the lowest value std::cout << "Maximum value for float: " << std::numeric_limits<float>::max() << std::endlstd::cout << "float is signed: " << std::numeric_limits<float>::is_signed << std::endlstd::cout << "Non-sign bits in float: " << std::numeric_limits<float>::digits << std::endlstd::cout << "float has infinity: " << std::numeric_limits<float>::has_infinity << std::endlstd::cout << "Minimum value for unsigned short: " << std::numeric_limits<unsigned short>::min() << std::endlstd::cout << "Maximum value for unsigned short: " << std::numeric_limits<unsigned short>::max() << std::endlstd::cout << "is_specialized(float): " << std::numeric_limits<float>::is_specialized << std::endlstd::cout << "is_integer(float): " << std::numeric_limits<float>::is_integer << std::endlstd::cout << "is_exact(float): " << std::numeric_limits<float>::is_exact << std::endlstd::cout << "is_bounded(float): " << std::numeric_limits<float>::is_bounded << std::endlstd::cout << "is_modulo(float): " << std::numeric_limits<float>::is_modulo << std::endlstd::cout << "is_iec559(float): " << std::numeric_limits<float>::is_iec559 << std::endlstd::cout << "digits10(float): " << std::numeric_limits<float>::digits10 << std::endlstd::cout << "radix(float): " << std::numeric_limits<float>::radix << std::endlstd::cout << "min_exponent(float): " << std::numeric_limits<float>::min_exponent << std::endlstd::cout << "max_exponent(float): " << std::numeric_limits<float>::max_exponent << std::endlstd::cout << "min_exponent10(float): " << std::numeric_limits<float>::min_exponent10 << std::endlstd::cout << "max_exponent10(float): " << std::numeric_limits<float>::max_exponent10 << std::endlstd::cout << "epsilon(float): " << std::numeric_limits<float>::epsilon() << std::endlstd::cout << "round_style(float): " << std::numeric_limits<float>::round_style << std::endlstd::cout << "The smallest nonzero denormalized value for float: "  << std::numeric_limits<float>::denorm_min()<< std::endlstd::cout << "The difference between 1 and the smallest value greater than 1 for float: "  << std::numeric_limits<float>::epsilon()<< std::endlstd::cout << "Whether float objects allow denormalized values: "  << std::numeric_limits<float>::has_denorm << std::endlstd::cout << "Whether float objects can detect denormalized loss: "  << std::numeric_limits<float>::has_denorm_loss << std::endlstd::cout << "Whether float objects have quiet_NaN: "  << std::numeric_limits<float>::has_quiet_NaN << std::endlstd::cout << "Whether float objects have a signaling_NaN: "  << std::numeric_limits<float>::has_signaling_NaN << std::endlstd::cout << "The base for type float is:  "  << std::numeric_limits<float>::radix << std::endlstd::cout << "The maximum rounding error for type float is:  "  << std::numeric_limits<float>::round_error() << std::endlstd::cout << "The rounding style for a double type is: "  << std::numeric_limits<double>::round_style << std::endlstd::cout << "The signaling NaN for type float is:  "  << std::numeric_limits<float>::signaling_NaN() << std::endlstd::cout << "Whether float types can detect tinyness before rounding: "  << std::numeric_limits<float>::tinyness_before << std::endlstd::cout << "Whether float types have implemented trapping: "  << std::numeric_limits<float>::traps << std::endlreturn 0;}
 在windows10 64位上執行結果如下:
 

           

再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://blog.csdn.net/jiangjunshow

相關文章