float / int隐式转换 [英] float/int implicit conversion
问题描述
我正在做乘法和除法 float
s和 int
s,我忘记了隐式转换规则而且问题中的单词对谷歌来说似乎太过于模糊了,而不是在这里问这个问题。)
如果我有两个 int
s,但我想做浮点除法,我是否只需要施放一个或两个操作数?如果乘以一个 float
和一个 int
,那么答案是a 你不能分配给
int $ <$ c $> code>从
float
除以 int
的结果,反之亦然。
所以答案是:
lockquote
如果我有两个 int
s,但我想做浮点除法...?
一次投射就足够了。
lockquote>
如果我乘以一个 float
和一个 int
,答案是 float
?
是的。
float f = 1000f;
int i = 3;
f = i; // Ok
i = f; //错误
f = i / f; // Ok 0.003
f = f / i; //好的333.3333(3)
i = i / f; //错误
i = f / i; //错误
I'm doing multiplication and division of float
s and int
s and I forget the implicit conversion rules (and the words in the question seem too vague to google more quickly than asking here).
If I have two int
s, but I want to do floating-point division, do I need only to cast one or both of the operands? How about for multiplication — if I multiply a float
and an int
, is the answer a float
?
You can’t assign to an int
result from division of a float
by an int
or vice-versa.
So the answers are:
If I have two
int
s, but I want to do floating point division…?
One cast is enough.
If I multiply a
float
and anint
, is the answer afloat
?
Yes it is.
float f = 1000f;
int i = 3;
f = i; // Ok
i = f; // Error
f = i/f; //Ok 0.003
f = f/i; //Ok 333.3333(3)
i = i/f; //Error
i = f/i; //Error
这篇关于float / int隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!