Why does %f print large values when floating point constants are passed instead of variables?Is floating point math broken?Correct format specifier for double in printf'float' vs. 'double' precisionWhy does printf() promote a float to a double?How does printf and co differentiate between float and doubleC read file line by lineWhat happens to a float variable when %d is used in a printf?Hexadecimal Floating-Point ConstantWhy are elementwise additions much faster in separate loops than in a combined loop?How dangerous is it to compare floating point values?Why does the C preprocessor interpret the word “linux” as the constant “1”?Why does GCC generate 15-20% faster code if I optimize for size instead of speed?Why the same variable print the different output?Why are the int and float passed in printf going to the wrong positions in the format string?Float and int members in a union does not show values
Why is matter-antimatter asymmetry surprising, if asymmetry can be generated by a random walk in which particles go into black holes?
Transiting through Switzerland by coach with lots of cash
Can I use I2C over 2m cables?
Find the percentage
Solving a Certainty Equivalent (Decision Analysis) problem
Creating chess engine, machine learning vs. traditional engine?
How to ride a fish?
How acceptable is an ellipsis "..." in formal mathematics?
Low-magic medieval fantasy clothes that allow the wearer to grow?
What is the next number in the series: 21, 21, 23, 20, 5, 25, 31, 24,?
Can I color text by using an image, so that the color isn't flat?
How to copy the path of current directory in ubuntu 18.04
Does "Op. cit." stand for "opus citatum" or "opere citato"?
Tikz – Box/frame arround Text with interruption
There is any way today to recover/dump 2M disks?
Noun phrase and is
What is the meaning of "log" in this sentence?
how do you value what your leisure time is worth?
In Men at Arms, why announce Edward was caught?
My first random password generator
Proving roots of a function cannot all be real
grep pairs of patterns and file
How to find out which object is taking space?
What can I do to avoid potential charges for bribery?
Why does %f print large values when floating point constants are passed instead of variables?
Is floating point math broken?Correct format specifier for double in printf'float' vs. 'double' precisionWhy does printf() promote a float to a double?How does printf and co differentiate between float and doubleC read file line by lineWhat happens to a float variable when %d is used in a printf?Hexadecimal Floating-Point ConstantWhy are elementwise additions much faster in separate loops than in a combined loop?How dangerous is it to compare floating point values?Why does the C preprocessor interpret the word “linux” as the constant “1”?Why does GCC generate 15-20% faster code if I optimize for size instead of speed?Why the same variable print the different output?Why are the int and float passed in printf going to the wrong positions in the format string?Float and int members in a union does not show values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
In the given program why did I get different results for each of the printfs?
#include <stdio.h>
int main()
float c = 4.4e10;
printf("%fn", c);
printf("%fn", 4.4e10);
return 0;
And it shows the following output:
44000002048.000000
44000000000.000000
c
|
show 3 more comments
In the given program why did I get different results for each of the printfs?
#include <stdio.h>
int main()
float c = 4.4e10;
printf("%fn", c);
printf("%fn", 4.4e10);
return 0;
And it shows the following output:
44000002048.000000
44000000000.000000
c
4
The answers so far explain that4.4e10is adoubleconstant which is converted tofloatin the initialization ofcbut kept as adoublewhen passed toprintf. However, you might also like to know that adding anfsuffix makes it afloatconstant: Printing4.4e10fwill show the same value that results from initializingcto4.4e10f. Distinguishingfloatconstants fromdoubleconstants can be important to doing quality work with floating-point arithmetic.
– Eric Postpischil
8 hours ago
Is this conversion method named anything? I want to read about it.
– user10056563
8 hours ago
Do you want to know when the conversion fromdoubletofloatoccurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?
– Eric Postpischil
8 hours ago
I'm questioning neither the replies here nor the standard but, when I was young and learningCwe usedprintf("%f",x)for afloatandprintf("%lf",x)for adouble. When did things change? And how would one explicitly printf a (single)float-printf("%hf",x)??
– Adrian
8 hours ago
2
@Adrian%lfin printf is the same thing as%f. Afloatin a variable argument is converted into adoubleby the compiler, just like ashortgets converted into anint.
– JL2210
7 hours ago
|
show 3 more comments
In the given program why did I get different results for each of the printfs?
#include <stdio.h>
int main()
float c = 4.4e10;
printf("%fn", c);
printf("%fn", 4.4e10);
return 0;
And it shows the following output:
44000002048.000000
44000000000.000000
c
In the given program why did I get different results for each of the printfs?
#include <stdio.h>
int main()
float c = 4.4e10;
printf("%fn", c);
printf("%fn", 4.4e10);
return 0;
And it shows the following output:
44000002048.000000
44000000000.000000
c
c
edited 7 hours ago
JL2210
4,5664 gold badges12 silver badges42 bronze badges
4,5664 gold badges12 silver badges42 bronze badges
asked 8 hours ago
user10056563user10056563
443 bronze badges
443 bronze badges
4
The answers so far explain that4.4e10is adoubleconstant which is converted tofloatin the initialization ofcbut kept as adoublewhen passed toprintf. However, you might also like to know that adding anfsuffix makes it afloatconstant: Printing4.4e10fwill show the same value that results from initializingcto4.4e10f. Distinguishingfloatconstants fromdoubleconstants can be important to doing quality work with floating-point arithmetic.
– Eric Postpischil
8 hours ago
Is this conversion method named anything? I want to read about it.
– user10056563
8 hours ago
Do you want to know when the conversion fromdoubletofloatoccurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?
– Eric Postpischil
8 hours ago
I'm questioning neither the replies here nor the standard but, when I was young and learningCwe usedprintf("%f",x)for afloatandprintf("%lf",x)for adouble. When did things change? And how would one explicitly printf a (single)float-printf("%hf",x)??
– Adrian
8 hours ago
2
@Adrian%lfin printf is the same thing as%f. Afloatin a variable argument is converted into adoubleby the compiler, just like ashortgets converted into anint.
– JL2210
7 hours ago
|
show 3 more comments
4
The answers so far explain that4.4e10is adoubleconstant which is converted tofloatin the initialization ofcbut kept as adoublewhen passed toprintf. However, you might also like to know that adding anfsuffix makes it afloatconstant: Printing4.4e10fwill show the same value that results from initializingcto4.4e10f. Distinguishingfloatconstants fromdoubleconstants can be important to doing quality work with floating-point arithmetic.
– Eric Postpischil
8 hours ago
Is this conversion method named anything? I want to read about it.
– user10056563
8 hours ago
Do you want to know when the conversion fromdoubletofloatoccurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?
– Eric Postpischil
8 hours ago
I'm questioning neither the replies here nor the standard but, when I was young and learningCwe usedprintf("%f",x)for afloatandprintf("%lf",x)for adouble. When did things change? And how would one explicitly printf a (single)float-printf("%hf",x)??
– Adrian
8 hours ago
2
@Adrian%lfin printf is the same thing as%f. Afloatin a variable argument is converted into adoubleby the compiler, just like ashortgets converted into anint.
– JL2210
7 hours ago
4
4
The answers so far explain that
4.4e10 is a double constant which is converted to float in the initialization of c but kept as a double when passed to printf. However, you might also like to know that adding an f suffix makes it a float constant: Printing 4.4e10f will show the same value that results from initializing c to 4.4e10f. Distinguishing float constants from double constants can be important to doing quality work with floating-point arithmetic.– Eric Postpischil
8 hours ago
The answers so far explain that
4.4e10 is a double constant which is converted to float in the initialization of c but kept as a double when passed to printf. However, you might also like to know that adding an f suffix makes it a float constant: Printing 4.4e10f will show the same value that results from initializing c to 4.4e10f. Distinguishing float constants from double constants can be important to doing quality work with floating-point arithmetic.– Eric Postpischil
8 hours ago
Is this conversion method named anything? I want to read about it.
– user10056563
8 hours ago
Is this conversion method named anything? I want to read about it.
– user10056563
8 hours ago
Do you want to know when the conversion from
double to float occurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?– Eric Postpischil
8 hours ago
Do you want to know when the conversion from
double to float occurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?– Eric Postpischil
8 hours ago
I'm questioning neither the replies here nor the standard but, when I was young and learning
C we used printf("%f",x) for a float and printf("%lf",x) for a double. When did things change? And how would one explicitly printf a (single) float - printf("%hf",x)??– Adrian
8 hours ago
I'm questioning neither the replies here nor the standard but, when I was young and learning
C we used printf("%f",x) for a float and printf("%lf",x) for a double. When did things change? And how would one explicitly printf a (single) float - printf("%hf",x)??– Adrian
8 hours ago
2
2
@Adrian
%lf in printf is the same thing as %f. A float in a variable argument is converted into a double by the compiler, just like a short gets converted into an int.– JL2210
7 hours ago
@Adrian
%lf in printf is the same thing as %f. A float in a variable argument is converted into a double by the compiler, just like a short gets converted into an int.– JL2210
7 hours ago
|
show 3 more comments
2 Answers
2
active
oldest
votes
A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)
When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.
In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.
If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).
add a comment
|
You're actually printing the values of two different types here.
In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.
In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.
Why it specifically prints 2048 at the end ?
– user10056563
8 hours ago
@user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.
– dbush
8 hours ago
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f58187573%2fwhy-does-f-print-large-values-when-floating-point-constants-are-passed-instead%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)
When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.
In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.
If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).
add a comment
|
A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)
When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.
In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.
If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).
add a comment
|
A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)
When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.
In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.
If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).
A float is a type that holds a 32-bit floating point number, while the constant 4.4e10 represents a double, which holds a 64-bit floating point number (i.e. a double-precision floating point number)
When you assign 4.4e10 to c, the value 4.4e10 cannot be represented precisely (a rounding error in a parameter called the mantissa), and the closest possible value (44000002048) is stored. When it is passed to printf, it is promoted back to double, including the rounding error.
In the second case, the value is a double the whole time, without narrowing and widening, and it happens to be the case that a double can represent the value exactly.
If this is undesirable behavior, you can declare c as a double for a bit more precision (but beware that you'll still hit precision limits eventually).
edited 7 hours ago
JL2210
4,5664 gold badges12 silver badges42 bronze badges
4,5664 gold badges12 silver badges42 bronze badges
answered 8 hours ago
ζ--ζ--
31.5k4 gold badges66 silver badges91 bronze badges
31.5k4 gold badges66 silver badges91 bronze badges
add a comment
|
add a comment
|
You're actually printing the values of two different types here.
In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.
In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.
Why it specifically prints 2048 at the end ?
– user10056563
8 hours ago
@user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.
– dbush
8 hours ago
add a comment
|
You're actually printing the values of two different types here.
In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.
In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.
Why it specifically prints 2048 at the end ?
– user10056563
8 hours ago
@user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.
– dbush
8 hours ago
add a comment
|
You're actually printing the values of two different types here.
In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.
In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.
You're actually printing the values of two different types here.
In the first case you're assigning a value to a variable of type float. The precision of a float is roughly 6 or 7 decimal digits, so unless the value can be represented exactly you'll see the closest value that can be represented by that type.
In the second case you're passing the constant 4.4e10 which has type double. This type has around 16 decimal digits of precision, and the value is within that range, so the exact value is printed.
edited 7 hours ago
JL2210
4,5664 gold badges12 silver badges42 bronze badges
4,5664 gold badges12 silver badges42 bronze badges
answered 8 hours ago
dbushdbush
114k15 gold badges129 silver badges162 bronze badges
114k15 gold badges129 silver badges162 bronze badges
Why it specifically prints 2048 at the end ?
– user10056563
8 hours ago
@user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.
– dbush
8 hours ago
add a comment
|
Why it specifically prints 2048 at the end ?
– user10056563
8 hours ago
@user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.
– dbush
8 hours ago
Why it specifically prints 2048 at the end ?
– user10056563
8 hours ago
Why it specifically prints 2048 at the end ?
– user10056563
8 hours ago
@user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.
– dbush
8 hours ago
@user10056563 Because that's the closest number to 4.4e10 that can be stored in a 32 bit float.
– dbush
8 hours ago
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f58187573%2fwhy-does-f-print-large-values-when-floating-point-constants-are-passed-instead%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
The answers so far explain that
4.4e10is adoubleconstant which is converted tofloatin the initialization ofcbut kept as adoublewhen passed toprintf. However, you might also like to know that adding anfsuffix makes it afloatconstant: Printing4.4e10fwill show the same value that results from initializingcto4.4e10f. Distinguishingfloatconstants fromdoubleconstants can be important to doing quality work with floating-point arithmetic.– Eric Postpischil
8 hours ago
Is this conversion method named anything? I want to read about it.
– user10056563
8 hours ago
Do you want to know when the conversion from
doubletofloatoccurs in the C language? Or do you want to know what values result from the conversion, that is, what effects the conversion has? Or something else?– Eric Postpischil
8 hours ago
I'm questioning neither the replies here nor the standard but, when I was young and learning
Cwe usedprintf("%f",x)for afloatandprintf("%lf",x)for adouble. When did things change? And how would one explicitly printf a (single)float-printf("%hf",x)??– Adrian
8 hours ago
2
@Adrian
%lfin printf is the same thing as%f. Afloatin a variable argument is converted into adoubleby the compiler, just like ashortgets converted into anint.– JL2210
7 hours ago