Иллюстрированный самоучитель по Visual Studio.Net

         

Полезные константы


STL имеет много полезных констант. Проверьте свои знания основ информатики. Знаете ли вы смысл констант, приведенных ниже? Для их использования вам потребуется подключить такие файлы заголовков:

#include <limits>

#include <climits>

#finclude <cfloat>

#finclude <numeric>

Вот фрагмент, который выводит некоторые из констант и по-английски описывает их смысл. Русский язык отказывается работать на моем компьютере в окне консольного приложения. Думаю, что существуют программисты, которые зарабатывают свой хлеб, имея смутное представление о существовании этих констант:

//===== Сначала простые, которые знают все

cout « "\n Is a char signed? "

« numeric_limits<char>::is_signed;

cout « "\n The minimum value for char is: "

« (int)numeric_limits<char>::min();

cout « "\n The maximum value for char is: "

« (int)numeric_limits<char>::max();

cout « "\n The minimum value for int is: "

« numeric_limits<int>::min();



cout « "\n The maximum value for int is: "

« numeric_limits<int>::max();

cout « "\n Is a integer an integer? "

« numeric_limits<int>::is_integer;

cout « "\n Is a float an integer? "

« numeric_limits<float>::is_integer;

cout « "\n Is a integer exact? "

« numeric_limits<int>::is_exact;

cout « "\n Is a float exact? "

« numeric_limits<float>::is_exact;

//===== Теперь более сложные

cout « "\n Number of bits in mantissa (double) : "

« DBL_MANT_DIG; cout « "\n Number of bits in mantissa (float): "

« FLT_MANT_DIG;

cout <<"\n The number of digits representble " "in base 10 for float is "

« numeric_limits<float>::digitslO;

cout « "\n The radix for float is: "

« numeric_limits<float>::radix;

cout « "\n The epsilon for float is: "

« numeric_limits<float>::epsilon() ;

cout « "\n The round error for float is: "



« numeric_limits<float>::round_error();

cout « "\ n The minimum exponent for float is: "

« numeric_limits<float>::min_exponent;

cout « "\n The minimum exponent in base 10: "

« numeric_limits<float>::min_exponentlO;

cout « "\n The maximum exponent is: "

« numeric_limits<float>::max_exponent;

cout « "\n The maximum exponent in base 10: "

« numeric_limits<float>::max_exponentlO;

cout « "\n Can float represent positive infinity? "

« numeric_limits<float>::has_infinity;

cout « "\n Can double represent positive infinity? "

« numeric_limits<double>::has_infinity;

cout « "\n Can int represent positive infinity? "

« numeric_limits<int>::has_infinity;

cout « "\n Can float represent a NaN? "

« numeric_limits<float>::has_quiet_NaN;

cout « "\n Can float represent a signaling NaN? "

« numeric_limits<float>::has_signaling_NaN;

//===== Теперь еще более сложные

cout « "\n Does float allow denormalized values? "

« numeric_limits<float>::has_denorm;

cout « "\n Does float detect denormalization loss? "

« numeric_limits<float>::has_denorm_loss;

cout « "\n Representation of positive infinity for"

" float: "« numeric_limits<float>::infinity();

cout « "\n Representation of quiet NaN for float: "

« numeric_limits<float>::quiet_NaN();

cout « "\n Minimum denormalized number for float: "

« numeric_limits<float>::denorm_min();

cout « "\n Minimum positive denormalized value for"

" float " « numeric_limits<float>::denorm_min();

cout « "\n Does float adhere to IEC 559 standard? "

« numeric_limits<float>::is_iec559; cout « "\n Is float bounded? "

« numeric_limits<float>::is_bounded;

cout « "\n Is float modulo? "

« numeric_limits<float>::is_modulo;



cout « "\n is int modulo? "

« numeric_limits<float>::is_modulo;

cout « "\ n Is trapping implemented for float? "

« numeric_limits<float>::traps;

cout « "\n Is tinyness detected before rounding? "

« numeric_limits<float>::tinyness_before;

cout « "\n What is the rounding style for float? "

« (int)numeric_limits<float>::round_style;

cout « "\n What is the rounding style for int? "

« (int)numeric_limits<int>::round_style;

//===== Теперь из другой оперы

cout « "\n Floating digits " « FLT_DIG;

cout « "\n Smallest such that 1.0+DBL_EPSILON !=1.0: "

« DBL_EPSILON;

cout « "\n LDBL_MIN_EXP: " « LDBL_MIN_EXP;

cout « "\n LDBL_EPSILON: " « LDBL_EPSILON;

cout « "\n Exponent radix: " « _DBL_RADIX;

Незнание констант типа DBL_EPSILON или DBL_MANT_DIG довольно сильно ограничивает квалификацию программиста, поэтому советую внимательно исследовать вывод, производимый данным фрагментом, и, возможно, обратиться к специальным изданиям по архитектуре компьютера или учебникам с целью ликвидировать пробелы в знаниях в этой области.




Содержание раздела