- C/C++的设计准则之一:尽可能地接近硬件。
- float:一个字(32bit)double:2个字(64bit)long long:3或4个字(96/128bit)
- 类型int,short,long 和 long long均为带符号的。
- 如何选择类型:①不可能为负,选用无符号;(In a word: no negative, unsigned.)②使用int执行整数运算,超过int选用long long;(In a word: short < **int** < long < long long)③浮点数运算选用double,long double提供的精度没必要(In a word: float < **double** < long double)
E2.1:
- short 和 int 至少16bits,long至少32bits,long long至少64bits
- 有符号类型可以代表正数、负数和0,无符号类型只能代表不小于0的正数
- C/C++标准不区分float、double、long double
E2.2:
- 用double,或者float
The rate most like that: 4.50 % per year.
The principal most like that: $854.36
The payment most like that: $1,142.36
- 赋给无符号类型一个超出它表示范围的值时,结果是初始值对无符号类型表示数值总数取模后的余数。
- 控制变量递减的方式把10到0的数字降序输出
unsigned u = 11;
while(u > 0){
- - u; //先减1,这样最大一次迭代时就会输出0
std::cout << u << std::endl;
}
E2.3:
- 32
- (int 32bits = 4294967296)4294967234
- 32
- -32
- 0
- 0
#include <iostream>
int main( )
{
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl;
std::cout << u - u2 << std::endl;
int i = 10, i2 = 42;
std::cout << i2 - i <<