Nealon
Nealon

Reputation: 2243

What's the difference between LONG float and double in C++?

So I know that there's a large difference in the precision of floats and doubles. I get that. Promise.

But, in C++, when calling scanf and printf, the notation used to specify a double is "%lf", and that stands for long float, right? So while a float is less precise than a double, a LONG float (presumedly called long float because it can be "longer" by having more terms) is the same accuracy and therefore essentially the same thing?

Just to clarify, here's what I mean:

double number = 3.14159;
printf("The number is %lf", number);

So the root of my question: Is long float another name for double?

Upvotes: 7

Views: 59778

Answers (4)

Abraham Le
Abraham Le

Reputation: 134

The long float is a K&R C first edition type that existed. It is synonymous with double.

After the first standard C89/C90, long float is removed. It is not deprecated. C89/C90 is also K&R C second edition. Then there is the multilingual amendment known as C94/C95 that adds wchar_t, as well as features such as <iso646.h>.

Many features of K&R C first edition are deprecated but not removed until the second standard C99. Automatic return type to int is removed, and default parameter type to int and double is removed from C99. The C99 standard requires function prototype, not function declaration i.e. int function_declaration(); vs int function_prototype(void);. It also removed the K&R C style prototype.

// implicit int type
main(argc, argv)
char ** argv;

// explicit int type
int main(argc, argv)
int argc;
char ** argv;

C++ started long before C was standardized. Templates were standardized in 1983, making it harder to compile to C code. It was not standardized until 1998. Old compilers may have deprecated old features that are removed with more contemporary compilers. The %lf is a legacy of long float that gets carried forward for C's standard library.

Upvotes: 6

Pete Becker
Pete Becker

Reputation: 76235

For scanf you pass a pointer to the location where the result will be stored; you read a float value with %f and a double value with %lf; you have to distinguish them because float and double are not required to have the same representation. For printf you pass in the value to be displayed, and float values get promoted to double because they're passes as arguments in the ... portion of the prototype; that is, for printf there's no difference between a float and a double, so %f and %lf do the same thing.

Upvotes: 0

milleniumbug
milleniumbug

Reputation: 15804

printf specifier names don't have anything in common with names of types.

They are just named that way so they are short and easy to remember.

float -> double -> long double

%f -> %lf -> %Lf

(also, they couldn't name printf double specifier as %d because that name is already reserved for decimal representation of int (compared to octal %o))

@taocp's answer explains why you can use both %f and %lf with printf, but note you can't do it with scanf

Upvotes: 5

taocp
taocp

Reputation: 23624

There is no such a type as long float within my knowledge.

This post gives you information about why people use lf to print double with printf if this is the cause of your confusion.

By courtesy of @Jerry Coffin:

"%f" is the (or at least "a") correct format for a double. There is no format for a float, because if you attempt to pass a float to printf, it'll be promoted to double before printf receives it. "%lf" is also acceptable under the current standard -- the l is specified as having no effect if followed by the f conversion specifier (among others).

So the reason is that when people do:

 printf("The number is %lf", number);

It is equivalent to do:

printf("The number is %f", number); //l has no effect when printing double

Upvotes: 19

Related Questions