Which variables should I typecast when doing math operations in C/C++?
2012-09-29 13:21
525 查看
原帖: http://stackoverflow.com/questions/245740/which-variables-should-i-typecast-when-doing-math-operations-in-c-c
===============================================================================================================================
For example, when I'm dividing two ints and want a float returned, I superstitiously write something like this:
If I do not cast
floats, it'll do integer division and return an int.
Similarly, if I want to multiply a signed 8-bit number with an unsigned 8-bit number, I will cast them to signed 16-bit numbers before multiplying for fear of overflow:
How exactly does the compiler behave in these situations when not casting at all or when only casting one of the variables? Do I really need to explicitly cast all of the variables, or just the one on the left, or the one on the right?
===============================================================================================================================
Five rules of thumb to remember:
Arithmetic operations are always performed on values of the same type.
The result type is the same as the operands (after promotion)
The smallest type arithmetic operations are performed on is int.
ANSCI C (and thus C++) use value preserving integer promotion.
Each operation is done in isolation.
The ANSI C rules are as follows:
Most of these rules also apply to C++ though not all types are officially supported (yet).
If either operand is a long double the other is converted to a long
double.
If either operand is a double the other is converted to a double.
If either operand is a float the other is converted to a float.
If either operand is a unsigned long long the other is converted to unsigned
long long.
If either operand is a long long the other is converted to long
long.
If either operand is a unsigned long the other is converted to unsigned
long.
If either operand is a long the other is converted to long.
If either operand is a unsigned int the other is converted to unsigned
int.
Otherwise both operands are converted to int.
Overflow is always a problem. Note. The type of the result is the same as the input operands so all the operations can overflow, so yes you do need to worry about it (though the language does not provide any explicit way to catch this happening.
As a side note:
Unsigned division can not overflow but signed division can.
==============================================================================================================================
In general, if operands are of different types, the compiler will promote all to the largest or most precise type:
Examples:
Beware, though, that promotion occurs only as required for each intermediate calculation, so:
This is because the integer division is performed first, then the result is promoted to float for the addition.
==============================================================================================================================
===============================================================================================================================
For example, when I'm dividing two ints and want a float returned, I superstitiously write something like this:
int a = 2, b = 3; float c = (float)a / (float)b;
If I do not cast
aand
bto
floats, it'll do integer division and return an int.
Similarly, if I want to multiply a signed 8-bit number with an unsigned 8-bit number, I will cast them to signed 16-bit numbers before multiplying for fear of overflow:
u8 a = 255; s8 b = -127; s16 = (s16)a * (s16)b;
How exactly does the compiler behave in these situations when not casting at all or when only casting one of the variables? Do I really need to explicitly cast all of the variables, or just the one on the left, or the one on the right?
===============================================================================================================================
Question 1: Float division
int a = 2, b = 3; float c = static_cast<float>(a) / b; // need to convert 1 operand to a float
Question 2: How the compiler works
Five rules of thumb to remember:Arithmetic operations are always performed on values of the same type.
The result type is the same as the operands (after promotion)
The smallest type arithmetic operations are performed on is int.
ANSCI C (and thus C++) use value preserving integer promotion.
Each operation is done in isolation.
The ANSI C rules are as follows:
Most of these rules also apply to C++ though not all types are officially supported (yet).
If either operand is a long double the other is converted to a long
double.
If either operand is a double the other is converted to a double.
If either operand is a float the other is converted to a float.
If either operand is a unsigned long long the other is converted to unsigned
long long.
If either operand is a long long the other is converted to long
long.
If either operand is a unsigned long the other is converted to unsigned
long.
If either operand is a long the other is converted to long.
If either operand is a unsigned int the other is converted to unsigned
int.
Otherwise both operands are converted to int.
Overflow
Overflow is always a problem. Note. The type of the result is the same as the input operands so all the operations can overflow, so yes you do need to worry about it (though the language does not provide any explicit way to catch this happening.As a side note:
Unsigned division can not overflow but signed division can.
std::numeric_limits<int>::max() / -1 // No Overflow std::numeric_limits<int>::min() / -1 // Will Overflow
==============================================================================================================================
In general, if operands are of different types, the compiler will promote all to the largest or most precise type:
If one number is... And the other is... The compiler will promote to... ------------------- ------------------- ------------------------------- char int int signed unsigned unsigned char or int float float float double double
Examples:
char + int ==> int signed int + unsigned char ==> unsigned int float + int ==> float
Beware, though, that promotion occurs only as required for each intermediate calculation, so:
4.0 + 5/3 = 4.0 + 1 = 5.0
This is because the integer division is performed first, then the result is promoted to float for the addition.
==============================================================================================================================
相关文章推荐
- Programming Basic--why we need to add ' extern "C" ' in C++ program when calling the function which is complied by C complier?
- Type cast in C++
- When should you use a class vs a struct in C++?
- When should we write our own assignment operator in C++?
- C++错误集锦之:error C2243: 'type cast' : conversion exists, but is inaccessible
- Question 35: Protected, or private, inheritance, as opposed to public inheritance, models which type of relationship in C++?
- How to get a type in C++ when its template argument is the argument
- 4 type cast operators in C++
- 4 type cast operators in C++
- what is the difference between static and normal variables in c++
- Question 16: Which of the following methods can a developer use to override the default terminate() function in C++?
- C++: typename in a template template parameter
- [转]PNG should be transparent perfectly in whichever browser
- Type Casting in C++
- Virtual method and base-type pointer make polymorphism in C++
- C++重载类型转换操作符(type cast operator
- xcode更新到5.1,"Cast from pointer to smaller type 'int' loses information” in EAGLView.mm
- request for member 'getid’ in ‘e1’, which is of non-class type ‘Employee()’
- 【转载】When should static_cast, dynamic_cast and reinterpret_cast be used?
- Custom Ribbon in SharePoint 2010 & which not wrok when migrate from 2010 to 2013