在C / C ++做数学运算时,我应该强制转换的变量? [英] Which variables should I typecast when doing math operations in C/C++?

查看:97
本文介绍了在C / C ++做数学运算时,我应该强制转换的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,当我分两个整数并希望浮动回来后,我写的迷信是这样的:

  int类型的= 2,B = 3;
浮C =(浮点)A /(浮点)B:

如果我不投 A B 来花车,它会做整数除法,并返回一个int

同样,如果我要乘以符号的8位数字,一个无符号的8位数字,我将它们转换成16位有符号数,生怕溢出乘以前:

  U8一= 255;
S8 B = -127;
S16 =(S16)一个*(S16)B:

为什么说编译器的行为在这些情况下,当完全不投或当只铸造一个变量?我真的需要显式转换所有的变量,或只是一个在左边或右边的一?


解决方案

问题1:浮法事业部

  int类型的= 2,B = 3;
浮C =的static_cast&所述;浮&GT(A)/ B; //需要1操作数转换为浮动

问题2:编译器的工作原理

拇指五个规则要记住:


  • 算术操作是在相同类型的值始终执行。

  • 结果类型是相同的操作数(升级后)

  • 最小的类型算术运算上执行的是int型。

  • ANSCI C(因而C ++)利用价值preserving整数推广。

  • 每个操作在隔离完成的。

ANSI C的规则如下:结果
大多数这些规则也适用于C ++,虽然不是所有的类型都正式支持(还)。


  • 如果一个操作数为A 长双的其他转换为A 长双

  • 如果一个操作数为A 双击的其他转换为A 双击

  • 如果一个操作数为A 浮动的其他转换为A 浮动

  • 如果一个操作数为A 无符号长长的另一个被转换为无符号长长

  • 如果一个操作数为A 长长的另一个被转换为长长

  • 如果一个操作数为A 无符号长的另一个被转换为无符号长

  • 如果一个操作数为A 的另一个被转换为

  • 如果一个操作数为A unsigned int类型的另一个被转换为 unsigned int类型

  • 否则,两个的操作数转换为 INT

溢出

溢出始终是一个问题。注意。结果类型是一样的输入操作数,这样所有的操作都​​可以溢出,所以是你需要担心它(虽然语言没有提供任何明确的办法赶上这种情况发生。

作为一个方面说明:结果
无符号除法不能溢出,但签署师可以。结果

 的std :: numeric_limits< INT> :: MAX()/ -1 //无溢出
的std :: numeric_limits< INT> :: MIN()/ -1 //就会溢出

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 a and b to 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

这篇关于在C / C ++做数学运算时,我应该强制转换的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆