-2

I am a new learner in C Programming language. Right now I am learning floating point data type where the concepta say: Floating-point constants are normally represented as double-precision quantities in C or some versions of C permit the specification of a “single-precision,” floating-point constant.

Exactly what does this mean? Can anyone provide me with an example to help me understand this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Does this answer your question? [What is the difference between float and double?](https://stackoverflow.com/questions/2386772/what-is-the-difference-between-float-and-double) – Paul Hankin Jan 27 '23 at 06:47
  • 1
    They refer to the [IEEE 754 - Floating Point Format](https://en.wikipedia.org/wiki/IEEE_754) While there are many more than two such formats, the general use of `single-precision` refers to the 32-bit floating point format while `double-precision` refers to the 64-bit format. – David C. Rankin Jan 27 '23 at 06:48
  • 1
    See [Single-precision floating-point format](https://en.wikipedia.org/wiki/Single-precision_floating-point_format) and [Double-precision floating-point format](https://en.wikipedia.org/wiki/Double-precision_floating-point_format) from the table in the original link for `binary32` and `binary64` formats. – David C. Rankin Jan 27 '23 at 06:53

1 Answers1

2

When you type a number somewhere in your code, it's called a constant. For a number of reasons this constant needs to have a type. Just like variables needs to have a type. Example:

int x; // Obviously x is a variable/object of type: int

1234   // This is an integer constant but what is the type ???

The C standard describes how the type of numeric constants are detemined. For instance the 1234 in the above code will be considered as an interger constant of type int.

Likewise for floating point constants. There need to be rules for the type of floating point constants. Example:

3.14  // Floating point constant of type ??

And that is exactly what the text in the question tells you:

Floating-point constants are normally represented as double-precision quantities in C

So from this you know that

3.14 // Floating point constant of type double

And here is the quote from the C (draft N1570) standard:

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.

This means:

3.14   // Type: double
3.14F  // Type: float
3.14L  // Type: long double

So why/when is this important?

At least one example is arithmetic expressions. When doing calculations the type of the operands must be the same. If they differ from the start, there will be conversions so the operands have the same type before the calculation is done. Example:

int x = 42;
double y = 3.14 * x;

So here x (which is an int) will be converted to a double because the constant 3.14 is a double. Once x has been converted the multiplication will be done as double * double and give a double as result..

Note that it's irrelevant for the multiplication part that the code above saves the result in a double. If you do:

int x = 42;
float y = 3.14 * x;

the same happens for the multiplication, i.e. the multiplication will still be done as double * double and the result of the multiplication is still a double. The resulting double is then assigned to a float which involves conversion from double to float. Notice however that during the conversion of the result from double to float some precision may be lost so the value of y in the two examples may differ a little.

To end this post here is some code where it makes a huge difference.

int x = 5;
double y = x / 2.0;          // Floating point constant of type double
double z = x / 2;            // Integer constant of type int
printf("y=%f z=%f\n", y, z);

Output:

y=2.500000 z=2.000000

Despite both y and z being equal to x divide by two, they end up with different values. This is because the division for calculating y was done as double / double while z was calculated as int / int

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63