Guille
Guille

Reputation: 430

Is the letter "f" necessary if the decimal point is written?

What is the difference between the 2 following code lines?

#define F_SAMP 10000.0f

#define F_SAMP 10000.0

Aren't both float? (By the way, in this particular case compiler is XC16 but I do not think that matters too much) Thanks for the attention

Upvotes: 6

Views: 656

Answers (5)

chux
chux

Reputation: 154169

Is the letter "f" necessary if the decimal point is written?

Not necessary, but best to use f-less constants with double variables/math and f suffixed constants with float ones.

Aren't both float?

There are both floating-point.
But float and double are different.

Type difference

Appending an f make a floating point constant type float rather than double.

This can steer to different code with _Generic and <tgmath.h> as well as the math used in a floating-point expression.

Given the usual range and precision difference, subtle and sometimes not so subtle differences will occur.

Value difference

The f tell the compiler to use the usually narrower range and precision of a float.
e.g. 0.1 != 0.1f.

Sometimes it makes a value difference too, even when assigned to a float.

Upvotes: 5

GigaWarTrex
GigaWarTrex

Reputation: 145

I am not 100% sure for XC16 but in general omitting the trailing f will result in your floating point number to be interpreted by the compiler as a double. The f is to specifically tell the compiler it is looking at a float.

Upvotes: 3

Keith Thompson
Keith Thompson

Reputation: 263577

A floating-point constant with an f or F suffix is of type float.

A floating-point constant with no suffix is of type double.

A floating-point constant with a suffix of l or L is of type long double.

These are the three distinct floating-point types. (Depending on the implementation, they may or may not have three distinct representations. For example, it's not uncommon for double and long double to have the same size and representation.)

In the particular case of 10000.0 vs. 10000.0f, it probably doesn't make much difference. Smallish whole number values like 10000.0 can usually be represented exactly in all three floating-point types, so any implicit or explicit conversions won't lose any information. But it's still a good idea to use a type that matches the context in which it's going to be used.

Note that if you omit the decimal point, 10000 is an integer constant (of type int), and 10000f is a syntax error. A floating-point context, with or without a suffix must have either a decimal point or an exponent.

This is a simpler set of rules than the ones for integer constants, where there are more distinct types, more suffixes, and the type of a constant can depend on its value.

Upvotes: 3

Eric Postpischil
Eric Postpischil

Reputation: 223795

Floating-point constants with an f or F suffix have type float, with no suffix have type double, and with an l or L suffix have type long double.

First, this makes a difference because the value can be different, and usually will be different if the value is not an integer. For example, using the formats most commonly used for float and double, the value of 123456789. is 123,456,789, but the value of 123456789.f is 123,456,792.

Further, if the value is assigned to a float, the result may be different than if the constant used f originally. For example, after:

float f0 = 9007198986305535.5f;
float f1 = 9007198986305535.5;

The value of f0 is 9,007,198,717,870,080, but the value of f1 is 9,007,199,254,740,992. This is because the former rounds 9,007,198,986,305,535.5 directly to float, but the latter rounds first to double and then to float.

Upvotes: 5

dbush
dbush

Reputation: 224892

The f suffix makes the constant 10000.0f have type float.

The constant 10000.0 has type double.

Upvotes: 4

Related Questions