[英]I can't figure out how to calculate the maximum and minimum signed number without using climits in C++
我目前正在為我的CS課程做作業。 我正在嘗試為int,short和long數據類型打印最大和最小符號數。
我不太確定該怎么做。 我一直在使用sizeof()函數來確定每種數據類型攜帶多少字節,然后從cmath庫中使用pow()。
這是我用來計算帶符號的short的最大數量的數學/代碼:(int)pow(2,sizeof(short)* 8)-1;
這將輸出最大無符號數,而不是最大有符號數。 我可以將其除以2,但不能完全確定如何計算負數,除了在前面打印“-”以外。
我現在擁有的更詳細的代碼:
42 int shortmax_calculate;
43 shortmax_calculate = (pow(2, sizeof(short)*8)-1)/2;
44
45 float shortmin_calculate;
46 shortmin_calculate = (int)pow(2, sizeof(short)*8)/2;
47
48 short unsigned shortunsigned_calculate;
49 shortunsigned_calculate = pow(2, sizeof(short)*8)-1;
50
51 int intmax_calculate;
52 intmax_calculate = (int)pow(2, sizeof(int)*8)-1;
53
54
55 cout << "Maxmimum short (signed): "<< shortmax_calculate << endl;
56 cout << "Minimum short (signed): " << "-" << shortmin_calculate << endl;
57 cout << "Maximum short (unsigned): " << shortunsigned_calculate << endl;
58 cout << "Maximum int (signed): " << intmax_calculate << endl;
任何幫助表示贊賞,謝謝。
假設您可以使用C ++ 11,請使用limits標准標頭:
#include <iostream>
#include <limits>
...
{
std::cout << "Maxmimum short (signed): " <<
std::numeric_limits<short>::max() << std::endl;
// repeat replacing with 'unsigned short', 'int', 'unsigned int'
// in template class function: std::numeric_limits<type>, using
// max() or min() as required.
}
看一下clang的實現-搜索__libcpp_numeric_limits
看看它如何進行評估。 如果您不能使用C ++ 11,它可能會給您一些模板的想法。
一點模板代碼就可以完成這項工作。
#include <iostream>
#include <cstdint>
template <size_t S> struct Max
{
static intmax_t get()
{
return ((Max<S-1>::get() << 8) + 0xFF);
}
};
template <> struct Max<1>
{
static intmax_t get()
{
return 0x7F;
}
};
template <size_t S> struct Min
{
static intmax_t get()
{
return ((Min<S-1>::get() << 8));
}
};
template <> struct Min<1>
{
static intmax_t get()
{
return -0x80;
}
};
int main()
{
std::cout << "Max for int: " << Max<sizeof(int)>::get() << std::endl;
std::cout << "Min for int: " << Min<sizeof(int)>::get() << std::endl;
std::cout << "Max for short: " << Max<sizeof(short)>::get() << std::endl;
std::cout << "Min for short: " << Min<sizeof(short)>::get() << std::endl;
std::cout << "Max for signed char: " << Max<sizeof(signed char)>::get() << std::endl;
std::cout << "Min for signed char: " << Min<sizeof(signed char)>::get() << std::endl;
}
這是我在機器上得到的:
Max for int: 2147483647 Min for int: -2147483648 Max for short: 32767 Min for short: -32768 Max for signed char: 127 Min for signed char: -128
因此,我認為您正在尋找的只是在計算的最大值中添加一個。 我在下面修改了您的代碼(對不起,此致)。
short shortmax_calculate;
shortmax_calculate = (pow(2, sizeof(short)*8)-1)/2;
short shortmin_calculate;
shortmin_calculate = shortmax_calculate + 1;
short unsigned shortunsigned_calculate;
shortunsigned_calculate = pow(2, sizeof(short)*8)-1;
int intmax_calculate;
intmax_calculate = (int)pow(2, sizeof(int)*8)-1;
int intmin_calculate = intmax_calculate + 1;
printf("Maximum short (signed): %i\n",shortmax_calculate);
printf("Minimum short (signed): %i\n",shortmin_calculate);
printf("Maximum short (unsigned): %i\n", shortunsigned_calculate);
printf("Maximum int (signed): %i\n", intmax_calculate);
printf("Minimum int (signed): %i\n", intmin_calculate);
這是代碼的輸出:
Maximum short (signed): 32767
Minimum short (signed): -32768
Maximum short (unsigned): 65535
Maximum int (signed): 2147483647
Minimum int (signed): -2147483648
這是因為計算機如何存儲負數。 例如:max short(有符號)為01111111,即127。最小有符號存儲為10000000(−128)。 有關更多信息,請參見此鏈接 。
在二進制補碼中,-1將設置所有位。 如果將其轉換為無符號並向右移動1,則它將是最大的有符號數,因為符號位將被替換為零。 如果采用按位的逆,則它將是最小的負數。
short shortmax_calculate = static_cast<short>(static_cast<unsigned short>(-1) >> 1);
short shortmin_calculate = ~shortmax_calculate;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.