0

Can someone explain what does literals e and f mean while representing a floating point in c ? for eg

float f = 6.8558e-4f
Mohanish
  • 9
  • 2
  • What does your books, tutorials or teacher say? – Some programmer dude Oct 01 '20 at 10:00
  • To be honest, this isn't really a C question. The use of "E" or "e" to indicate an exponent is absolutely ubiquitous in math and scientific computing. Even Excel understands it ;) Having said that, I guess it's possible that a person might see it for the first time in a programming course. – Kevin Boone Oct 01 '20 at 10:47

2 Answers2

2

From the 2018 C standard, clause 6.4.4.2, reworded:

  • “A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type.”
  • The significand is a sequence of digits that may include a period. It represents a number in the usual way. For example, 3.4 represents 3 ones and 4 tenths.
  • The exponent part is an e or E followed by an optional sign and a sequence of digits, such as e13 or e-4.
  • The “exponent indicates the power of 10 by which the significand part is to be scaled.”
  • “An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.”

Thus 6.8558e-4f represents 6.8558•10−4 and produces a float value.

Note that most C implementations use a float format based on powers of 2, not 10, so they cannot represent 6.8558•10−4 exactly. In the format commonly used, IEEE-754 binary32, the closest representable value is 0.0006855800165794789791107177734375.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

The e indicates scientific notation. So 6.8558e-4 is equivalent to 6.8558×10−4.

The f is a suffix that indicates a floating-point number. (Floating-point numbers are treated as double precision by default.)

Incidentally, C99 also introduced a hexadecimal notation for floating point numbers, which you can read about here: hexadecimal floating constant in C

r3mainer
  • 23,981
  • 3
  • 51
  • 88