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?
Difference in Precision (Accuracy)float and double both have varying capacities when it comes to the number of decimal digits they can hold. float can hold up to 7 decimal digits accurately while double can hold up to 15.
The difference is the size. They may be the same, or a long double may be larger. Larger meaning that it can hold greater (and smaller) values and with higher precision. The difference is that any type with long is more precise and has a greater range then the type itself without long because it uses more bytes.
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.
A float has 7 decimal digits of precision and occupies 32 bits . A double is a 64-bit IEEE 754 double-precision floating-point number. 1 bit for the sign, 11 bits for the exponent, and 52 bits for the value. A double has 15 decimal digits of precision and occupies a total of 64 bits .
Difference between float and double in C/C++. double has 2x more precision then float. float is a 32 bit IEEE 754 single precision Floating Point Number1 bit for the sign, (8 bits for the exponent, and 23* for the value), i.e. float has 7 decimal digits of precision. double is a 64 bit IEEE 754 double precision Floating Point Number...
double has 2x more precision then float. float is a 32 bit IEEE 754 single precision Floating Point Number1 bit for the sign, (8 bits for the exponent, and 23* for the value), i.e. float has 7 decimal digits of precision.
In this post, we will understand the difference between float and double data types. It has a single precision. It takes 4 bytes of memory. According to IEEE, it has 32-bit precision. It is used with graphic based libraries. It improves the processing power of programs. It is simple to manage by compilers.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With