简单的铸造问题 [英] Simple Casting Question
问题描述
大家好,
我有PRECISION在预处理器代码中定义了
,它可以是int,float或double,但我不知道
代码是什么。
>
现在,如果我想将零分配给PRECISION变量,
以下哪一行是正确的:
PRECISION myVar =(PRECISION)0; / *一个* /
PRECISION myVar =(PRECISION)0.0; / *两个* /
PRECISION myVar = 0; / *三* /
PRECISION myVar = 0.0; / *四* /
提前谢谢,
Alex
< blockquote> 5月18日,下午7:35,alex.j ... @ gmail.com写道:
大家好,
我有PRECISION在预处理器代码中定义了
,它可以是int,float或double,但我不知道
代码是什么。
>
现在,如果我想将零分配给PRECISION变量,
以下哪一行是正确的:
PRECISION myVar =(PRECISION)0; / *一个* /
PRECISION myVar =(PRECISION)0.0; / *两个* /
PRECISION myVar = 0; / *三* /
PRECISION myVar = 0.0; / *四* /
提前谢谢,
Alex
你可以试试不同的组合,看看哪个给你一个
错误,哪个正常。例如,假设PRECISION是一个浮动:
1)浮动myVar =(float)0; / * cast不需要,编译器会自动执行
* /
2)float myVar =(float)0.0; / * 0.0默认为加倍,施放没有
受伤* /
3)浮动myVar = 0; / *编译器自己执行强制转换* /
4)float myVar = 0.0; / * 0.0默认为double,但这是
okay * /
现在,假设PRECISION是一个整数类型,例如int:
1)int myVar =(int)0; / *浪费打字时间* /
2)int myvar =(int)0.0; / *编译器可以在编译时执行此操作
时间,但不好主意* /
3)int myVar = 0; / *这就是我们通常的做法* /
4)int myVar = 0.0; / *那个是编译器错误* /
所以方法4有问题,方法3打字最少,方法1
和2也没关系。
自己尝试一下,我希望我没错。
- Marty Wolfe
非常简单。这是方法三。 - 阿尔弗雷德·希区柯克(Alfred Hitchcock),三个人的方式来抢劫银行
al ******* @ gmail.com 写道:
Hello all,
我有PRECISION在预处理器代码中定义了
,它可以是int,float或double,但我不知道
代码是什么。
>
现在,如果我想将零分配给PRECISION变量,
以下哪一行是正确的:
PRECISION myVar =(PRECISION)0; / *一个* /
PRECISION myVar =(PRECISION)0.0; / *两个* /
PRECISION myVar = 0; / *三* /
PRECISION myVar = 0.0; / *四* /
我喜欢/ *三* /。
-
pete
al*******@gmail.com 写道:
我有PRECISION在预处理器代码中定义了
,它可以是int,float或double,但我不知道
代码是什么。
>
现在,如果我想将零分配给PRECISION变量,
以下哪一行是正确的:
PRECISION myVar =(PRECISION)0; / *一个* /
PRECISION myVar =(PRECISION)0.0; / *两个* /
PRECISION myVar = 0; / *三* /
PRECISION myVar = 0.0; / *四* /
所有四个都是正确的。第三种是首选,恕我直言。
PRECISION可以是整数类型还是
浮点类型似乎很奇怪。整数和浮点类型的行为方式非常不同,有时甚至是完全不同的方式。我认为
编写的代码不知道它是否处理整数
或浮点值会很困难且容易出错。 br />
-
Keith Thompson(The_Other_Keith) ks *** @ mib。组织< http://www.ghoti.net/~kst>
诺基亚
我们必须做点什么。这是事情。因此,我们必须这样做。
- Antony Jay和Jonathan Lynn,是部长
Hello all,
I have "PRECISION" defined in the preprocessor code
and it could be int, float or double, but I do not know in the
code what it is.
Now if I want to assign zero to a "PRECISION" variable,
which of the following lines are correct:
PRECISION myVar = (PRECISION) 0; /* One */
PRECISION myVar = (PRECISION) 0.0; /* Two */
PRECISION myVar = 0; /* Three */
PRECISION myVar = 0.0; /* Four */
Thank you in advance,
Alex
On May 18, 7:35 pm, alex.j...@gmail.com wrote:Hello all,
I have "PRECISION" defined in the preprocessor code
and it could be int, float or double, but I do not know in the
code what it is.
Now if I want to assign zero to a "PRECISION" variable,
which of the following lines are correct:
PRECISION myVar = (PRECISION) 0; /* One */
PRECISION myVar = (PRECISION) 0.0; /* Two */
PRECISION myVar = 0; /* Three */
PRECISION myVar = 0.0; /* Four */
Thank you in advance,
AlexYou could try the different combinations and see which gives you an
error, and which works properly. For example, assuming PRECISION is a
float:
1) float myVar = (float) 0; /* cast not needed, the compiler does
it automatically */
2) float myVar = (float) 0.0; /* 0.0 defaults to double, cast doesn''t
hurt */
3) float myVar = 0; /* Compiler performs cast on its own */
4) float myVar = 0.0; /* 0.0 defaults to double, but this is
okay */
Now, assuming PRECISION is an integral type, such as int:
1) int myVar = (int) 0; /* A waste of typing time */
2) int myvar = (int) 0.0; /* The compiler can do this at compile
time, but bad idea */
3) int myVar = 0; /* That''s how we usually do it */
4) int myVar = 0.0; /* That one is a compiler error */
So method 4 is problematic, method 3 is the least typing, methods 1
and 2 are okay too.
Try this on your own, I hope I''m not wrong.
-- Marty Wolfe
"Very simple. This is method three." -- Alfred Hitchcock, "Three
Ways to Rob a Bank"
al*******@gmail.com wrote:Hello all,
I have "PRECISION" defined in the preprocessor code
and it could be int, float or double, but I do not know in the
code what it is.
Now if I want to assign zero to a "PRECISION" variable,
which of the following lines are correct:
PRECISION myVar = (PRECISION) 0; /* One */
PRECISION myVar = (PRECISION) 0.0; /* Two */
PRECISION myVar = 0; /* Three */
PRECISION myVar = 0.0; /* Four */I like /* Three */.
--
pete
al*******@gmail.com writes:I have "PRECISION" defined in the preprocessor code
and it could be int, float or double, but I do not know in the
code what it is.
Now if I want to assign zero to a "PRECISION" variable,
which of the following lines are correct:
PRECISION myVar = (PRECISION) 0; /* One */
PRECISION myVar = (PRECISION) 0.0; /* Two */
PRECISION myVar = 0; /* Three */
PRECISION myVar = 0.0; /* Four */All four are correct. The third is preferred, IMHO.
It does seem odd that PRECISION can be either an integer type or a
floating-point type. Integer and floating-point types behave in very
different, and sometimes quite subly different, ways. I would think
that writing code that doesn''t know whether it''s dealing with integers
or floating-point values would be difficult and error-prone.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
这篇关于简单的铸造问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!