What does the numerical literal 0.e0f mean?

﹥>﹥吖頭↗ 提交于 2019-12-06 07:54:01

A floating-point literals have two syntaxes. The first one consists of the following parts:

  • nonempty sequence of decimal digits containing a decimal point character (defines significand)
  • (optional) e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent)
  • (optional) a suffix type specifier as a l, f, L or F

The second one consists of the following parts:

  • nonempty sequence of decimal digits (defines significant)
  • e or E followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent)
  • (optional) a suffix type specifier as a l, f, L or F

The suffix type specifier defines the actual type of the floating-point literal:

  • (no suffix) defines double
  • f F defines float
  • l L defines long double

f floating point indicator

eX is exponent value for 10 to the power of X. for example e5 means 10 to the 5 which is 100000. or e-3 means 0.001.

Combining the two

1.23e-3f --> 1.23 x 10 ^ -3 = 0.00123

By expanding your example it is

0.e0f --> 0.0 x 10 ^ 0 = 0.0 (in floating point format)

PS: A useful programming practice. Never ever (see PS2) compare two floating point numbers for equality.

There are some values cannot be represented exactly by floating points just approximations. Like this example

0.3 + 0.6 = 0.89999999999999991 != 0.9

Instead use:

float a;
float b;

....

if( abs(a-b) < FLT_EPSILON )

FLT_EPLISON is a very small floating point value where 1.0 + FLT_EPSILON != 1.0. (example 0.1e-10) (Quoted from @AlterMann)

abs is short for absolute function for floating points; which in std is fabs. Can be found in other libraries with different names too.

PS 2: OK... Never ever had been a little strong. I didn't mean that this piece of code is wrong neither as algorithm nor syntax. Question itself is a bit simple though this warning may help the new programmers end up here.

As in comments stated this code example is appropriate for checking. 0 check is a valid operation, because current standards for floating point representation guarantee that 0 can be expressed.

It's zero in scientific notation, as single-precision floating point, with a redundant decimal mark. It's the same thing as 0e0.

That's a 0 of type float.

See 6.4.4.2 in the C99 Standard ( http://port70.net/~nsz/c/c99/n1256.html#6.4.4.2 )

In my opinion a plain 0 would be better irrespective of the type of alpha_r and alpha_i

if ((alpha_r == 0) && (alpha_i == 0)) return;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!