将float(或double)赋值(或初始化)为零时,为什么常常用0.0而不是0?

test0231 2013-07-06 08:18:06
比如
float f1=0.0;    //常用
float f2=0; //不常用

这里面有什么讲究吗?
...全文
13383 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanruifeng010 2014-10-10
  • 打赏
  • 举报
回复
如果写成double a = 0;不鲁棒,实际执行结果可能是a=0xffffffff80000000; 因为0占四字节,a占八字节,只赋了低四字节。
lm_whales 2013-07-09
  • 打赏
  • 举报
回复
引用 11 楼 lm_whales 的回复:
主要是所谓的代码风格问题 float f1=0.0; //常用,明确表示用浮点数0.0(通常是double 类型的) 初始化f1 float f2=0; //不常用,让编译器处理吧,反正是0就行。 除非特殊情况(如C51) C 语言的浮点常量,缺省类型都是 double 类型的。 1.0 ,1.0f ,都是double 类型的。 C++也是一样处理的。
搞错了,应该是初始化。
lm_whales 2013-07-09
  • 打赏
  • 举报
回复
主要是所谓的代码风格问题 float f1=0.0; //常用,明确表示用浮点数0.0(通常是double 类型的) 给f1赋值 float f2=0; //不常用,让编译器处理吧,反正是0就行。 除非特殊情况(如C51) C 语言的浮点常量,缺省类型都是 double 类型的。 1.0 ,1.0f ,都是double 类型的。 C++也是一样处理的。
unituniverse2 2013-07-09
  • 打赏
  • 举报
回复
引用 6 楼 bukeshuo8 的回复:
是不是这样: 默认情况下,0是int型的. 所以如果把它赋值给一个float型(或者double型)变量, 实际是先把int型的0转换成float型的0.0f(或者double型的0.0). 这样做效率比较低.
不是“默认情况”,而是0本身就是int型的(而不会作为其他类型)。除了对int初始化或赋值外,全部是依赖隐式转换的(不光是习惯的问题)。
赵4老师 2013-07-09
  • 打赏
  • 举报
回复
float f=0.0f;
赵4老师 2013-07-09
  • 打赏
  • 举报
回复
常量也有类型!
橡木疙瘩 2013-07-08
  • 打赏
  • 举报
回复
习惯问题。 如果是古老并且非常笨的编译器或许会在用0对double类型赋值或是用0初始化时产生一次从整数到浮点数的转换代码,略微影响一些效率,但这么笨的编译器还真罕见。 如果不是用在浮点数初始化或是赋值,而是用在使用宏定义的方式实现所谓的“符号常量”,声明为0.0有利于发现类型定义时的失误,并且保证使用该“常量”进行计算时被编译为浮点计算而不是整数计算指令。
赵4老师 2013-07-08
  • 打赏
  • 举报
回复
C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types. Syntax integer-constant : decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence' decimal-constant : nonzero-digit decimal-constant digit octal-constant : 0 octal-constant octal-digit hexadecimal-constant : 0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit nonzero-digit : one of 1 2 3 4 5 6 7 8 9 octal-digit : one of 0 1 2 3 4 5 6 7 hexadecimal-digit : one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix : unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix : one of u U long-suffix : one of l L 64-bit integer-suffix : i64 To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type. To specify a decimal constant, begin the specification with a nonzero digit. For example: int i = 157; // Decimal constant int j = 0198; // Not a decimal number; erroneous octal constant int k = 0365; // Leading zero specifies octal constant, not decimal To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example: int i = 0377; // Octal constant int j = 0397; // Error: 9 is not an octal digit To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example: int i = 0x3fff; // Hexadecimal constant int j = 0X3FFF; // Equal to i To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example: unsigned uVal = 328u; // Unsigned value long lVal = 0x7FFFFFL; // Long value specified // as hex constant unsigned long ulVal = 0776745ul; // Unsigned long value
mujiok2003 2013-07-07
  • 打赏
  • 举报
回复

	float f1=0.0;    //常用
00F743BE  movss       xmm0,dword ptr ds:[0F7CD88h]  
00F743C6  movss       dword ptr [f1],xmm0  
	float f2=0;      //不常用
00F743CB  movss       xmm0,dword ptr ds:[0F7CD88h]  
00F743D3  movss       dword ptr [f2],xmm0 
一样一样的。
水平不流 2013-07-07
  • 打赏
  • 举报
回复
主要还是因为 编码风格,以及程序的鲁棒性
www_adintr_com 2013-07-07
  • 打赏
  • 举报
回复
这点优化编译器还是做得起的, 不存在效率的问题吧
test0231 2013-07-07
  • 打赏
  • 举报
回复
是不是这样: 默认情况下,0是int型的. 所以如果把它赋值给一个float型(或者double型)变量, 实际是先把int型的0转换成float型的0.0f(或者double型的0.0). 这样做效率比较低.
www_adintr_com 2013-07-07
  • 打赏
  • 举报
回复
0.0 是一个 double 类型 0.0f 是一个 float 型 0 是一个 int 型 0u 是一个 unsigned int 型. 虽然它们可以转换, 但是写来匹配难道不是一个好习惯吗.
赵4老师 2013-07-07
  • 打赏
  • 举报
回复
C++ Floating-Point Constants Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents. Syntax floating-constant : fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt fractional-constant : digit-sequenceopt . digit-sequence digit-sequence . exponent-part : e signopt digit-sequence E signopt digit-sequence sign : one of + – digit-sequence : digit digit-sequence digit floating-suffix :one of f l F L Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example: 18.46 38. The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example: 18.46e0 // 18.46 18.46e1 // 184.6 If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0. Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively. Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like void func( double ); and void func( long double );
撸撸猴 2013-07-07
  • 打赏
  • 举报
回复
因为定义为0,JVM会有个转变过程。0可以依靠为int型,或者是short型,在内部会参考一个自动化转换,即int(0) ---> float(0.0)。虽然这部份成本很低,但还是养成习惯的好,毕竟如果定义的多了,就会有成本了。而且这样做,看起来也更加直观。 通常情况下,在定义变量后(JAVA的标准变量),在加载程序的时候,执行类的构造方法之前,变量初始化的时候,如果没有斌值,那么JVM还是会默认地为之赋值。如果你有显示的赋值,JVM就不会去转换去判断了,节约了JVM的成本。当然,我说的是这些变量都属于类变量,而不是方法变量(定义在方法中的变量,就必须要赋初始值了) 你更应该注意的是float和double。 float f = 5.5f; double d = 6.55d;
十一文 2013-07-06
  • 打赏
  • 举报
回复
楼主可以 移动帖子到 技术区。 两者 可能存储不一样

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧