Why is the statement “f == (float)(double)f;” wrong?Why use apparently meaningless do-while and if-else statements in macros?With arrays, why is it the case that a[5] == 5[a]?What is the difference between float and double?Warning about data loss c++/cWhy is “while ( !feof (file) )” always wrong?Why are elementwise additions much faster in separate loops than in a combined loop?When a double with an integer value is cast to an integer, is it guaranteed to do it 'properly'?g++ warn when double/float is converted to unsigned integer without using -WconversionWhy does the C preprocessor interpret the word “linux” as the constant “1”?Operations on “double” and optimization in C
Quick Kurodoko Puzzle: Threes and Triples
The relationship of noch nicht and the passive voice
Why is it called a Blood Knot?
Does battery condition have anything to do with macbook pro performance?
I feel like most of my characters are the same, what can I do?
Manager manipulates my leaves, what's in it for him?
Get the encrypted payload from an unencrypted wrapper PDF document
Can Brexit be undone in an emergency?
Changing States from child through parent while obeying SOLID principles
How to create template of guides in Illustrator which appears with every document I open?
Why do we need to use transistors when building an OR gate?
How to ask a man to not take up more than one seat on public transport while avoiding conflict?
As a discovery writer, how to complete unfinished novel (which is highly diverted from original plot ) after a time-gap
Does Mage Hand give away the caster's position?
What is the origin of the "being immortal sucks" trope?
What's the word for a student who doesn't register but goes to a class anyway?
Why would a fighter use the afterburner and air brakes at the same time?
What did the controller say during my approach to land (audio clip)?
Are lay articles good enough to be the main source of information for PhD research?
How often is duct tape used during crewed space missions?
Dear Fellow PSE Users,
Why are Fuji lenses more expensive than others?
Is it safe to unplug a blinking USB drive after 'safely' ejecting it?
Is the name of an interval between two notes unique and absolute?
Why is the statement “f == (float)(double)f;” wrong?
Why use apparently meaningless do-while and if-else statements in macros?With arrays, why is it the case that a[5] == 5[a]?What is the difference between float and double?Warning about data loss c++/cWhy is “while ( !feof (file) )” always wrong?Why are elementwise additions much faster in separate loops than in a combined loop?When a double with an integer value is cast to an integer, is it guaranteed to do it 'properly'?g++ warn when double/float is converted to unsigned integer without using -WconversionWhy does the C preprocessor interpret the word “linux” as the constant “1”?Operations on “double” and optimization in C
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have recently taken a lecture of System Programming, and my professor told me that f == (float)(double) f is which wrong that I cannot get.
I know that double type loses its data when converted to float, but I believe the loss happens only if the stored number in double type cannot be expressed in float type.
Shouldn't it be true as same as x == (int)(double)x; is true?
the picture is the way I'm understanding it

I'm so sorry that I didn't make my question clearly.
the question is not about declaration, but about double type conversion.
I hope you don't lose your precious time because of my fault.
c
|
show 4 more comments
I have recently taken a lecture of System Programming, and my professor told me that f == (float)(double) f is which wrong that I cannot get.
I know that double type loses its data when converted to float, but I believe the loss happens only if the stored number in double type cannot be expressed in float type.
Shouldn't it be true as same as x == (int)(double)x; is true?
the picture is the way I'm understanding it

I'm so sorry that I didn't make my question clearly.
the question is not about declaration, but about double type conversion.
I hope you don't lose your precious time because of my fault.
c
4
What reasoning did your professor give for why this is wrong?
– Daniel Pryden
Mar 28 at 14:18
7
The only thing I see wrong is that you initialize the variable with itself.
– Eugene Sh.
Mar 28 at 14:20
3
What are you trying to achieve with that statement ?
– Sander De Dycker
Mar 28 at 14:21
@Eugenesh I did edit the question. Sorry.
– Veomchan Kim
Mar 28 at 14:43
@DanielPryden He didn't mentioned any reason, just saying it is wrong.
– Veomchan Kim
Mar 28 at 14:43
|
show 4 more comments
I have recently taken a lecture of System Programming, and my professor told me that f == (float)(double) f is which wrong that I cannot get.
I know that double type loses its data when converted to float, but I believe the loss happens only if the stored number in double type cannot be expressed in float type.
Shouldn't it be true as same as x == (int)(double)x; is true?
the picture is the way I'm understanding it

I'm so sorry that I didn't make my question clearly.
the question is not about declaration, but about double type conversion.
I hope you don't lose your precious time because of my fault.
c
I have recently taken a lecture of System Programming, and my professor told me that f == (float)(double) f is which wrong that I cannot get.
I know that double type loses its data when converted to float, but I believe the loss happens only if the stored number in double type cannot be expressed in float type.
Shouldn't it be true as same as x == (int)(double)x; is true?
the picture is the way I'm understanding it

I'm so sorry that I didn't make my question clearly.
the question is not about declaration, but about double type conversion.
I hope you don't lose your precious time because of my fault.
c
c
edited Mar 28 at 15:09
Veomchan Kim
asked Mar 28 at 14:16
Veomchan KimVeomchan Kim
484 bronze badges
484 bronze badges
4
What reasoning did your professor give for why this is wrong?
– Daniel Pryden
Mar 28 at 14:18
7
The only thing I see wrong is that you initialize the variable with itself.
– Eugene Sh.
Mar 28 at 14:20
3
What are you trying to achieve with that statement ?
– Sander De Dycker
Mar 28 at 14:21
@Eugenesh I did edit the question. Sorry.
– Veomchan Kim
Mar 28 at 14:43
@DanielPryden He didn't mentioned any reason, just saying it is wrong.
– Veomchan Kim
Mar 28 at 14:43
|
show 4 more comments
4
What reasoning did your professor give for why this is wrong?
– Daniel Pryden
Mar 28 at 14:18
7
The only thing I see wrong is that you initialize the variable with itself.
– Eugene Sh.
Mar 28 at 14:20
3
What are you trying to achieve with that statement ?
– Sander De Dycker
Mar 28 at 14:21
@Eugenesh I did edit the question. Sorry.
– Veomchan Kim
Mar 28 at 14:43
@DanielPryden He didn't mentioned any reason, just saying it is wrong.
– Veomchan Kim
Mar 28 at 14:43
4
4
What reasoning did your professor give for why this is wrong?
– Daniel Pryden
Mar 28 at 14:18
What reasoning did your professor give for why this is wrong?
– Daniel Pryden
Mar 28 at 14:18
7
7
The only thing I see wrong is that you initialize the variable with itself.
– Eugene Sh.
Mar 28 at 14:20
The only thing I see wrong is that you initialize the variable with itself.
– Eugene Sh.
Mar 28 at 14:20
3
3
What are you trying to achieve with that statement ?
– Sander De Dycker
Mar 28 at 14:21
What are you trying to achieve with that statement ?
– Sander De Dycker
Mar 28 at 14:21
@Eugenesh I did edit the question. Sorry.
– Veomchan Kim
Mar 28 at 14:43
@Eugenesh I did edit the question. Sorry.
– Veomchan Kim
Mar 28 at 14:43
@DanielPryden He didn't mentioned any reason, just saying it is wrong.
– Veomchan Kim
Mar 28 at 14:43
@DanielPryden He didn't mentioned any reason, just saying it is wrong.
– Veomchan Kim
Mar 28 at 14:43
|
show 4 more comments
3 Answers
3
active
oldest
votes
Assuming IEC 60559, the result of f == (float)(double) f depends on the type of f.
Further assuming f is a float, then there's nothing "wrong" about the expression - it will evaluate to true (unless f held NaN, in which case the expression will evaluate to false).
On the other hand, x == (int)(double)x (assuming x is a int) is (potentially) problematic, since a double precision IEC 60559 floating point value only has 53 bits for the significand1, which cannot represent all possible values of an int if it uses more than 53 bits for its value on your platform (admittedly rare). So it will evaluate to true on platforms where ints are 32-bit (using 31 bits for the value), and might evaluate to false on platforms where ints are 64-bit (using 63 bits for the value) (depending on the value).
Relevant quotes from the C standard (6.3.1.4 and 6.3.1.5) :
When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
When a finite value of real floating type is converted to an integer type other than
_Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
When a value of real floating type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
1 a double precision IEC 60559 floating point value consists of 1 bit for the sign, 11 bits for the exponent, and 53 bits for the significand (of which 1 is implied and not stored) - totaling 64 (stored) bits.
Thank you for you answer, but I found my mistake again and edit the question again. I'm so sorry and he said double type can express every numbers that int type expresses, you mean he taught me wrong?
– Veomchan Kim
Mar 28 at 15:11
@VeomchanKim : edited my answer accordingly - the gist is the same.
– Sander De Dycker
Mar 28 at 15:12
1
@VeomchanKim : the C standard allows theinttype to have a larger range than can be represented exactly by thedoubletype. On most of today's platforms, that's not a concern though. So, strictly speaking, that statement by your professor ("double type can express every numbers that int type expresses") is wrong, but practically speaking (or when speaking about a specific environment), the statement is fine.
– Sander De Dycker
Mar 28 at 15:24
Thank you again for your comments, I appreciate them very much.
– Veomchan Kim
Mar 28 at 15:25
1
Detail: Typical double can exactly represent allint54_t(which has sign and 53 bit of precision anddoublehas sign and 52 explicitly encoded + 1 implied precision bits).
– chux
Mar 28 at 20:53
|
show 3 more comments
Taking the question as posed in the title literally,
Why is the statement “f == (float)(double)f;” wrong?
the statement is "wrong" not in any way related to the representation of floating point values but because it is trivially optimized away by any compiler and thus you might as well have saved the electrons used to store it. It is exactly equivalent to the statement
1;
or, if you like, to the statement (from the original question)
x == (int)(double)x;
(which has exactly the same effect as that in the title, regardless of the available precision of the types int, float, and double, i.e. none whatsoever).
Programming being somewhat concerned with precision you should perhaps take note of the difference between a statement and an expression. An expression has a value which might be true or false or something else, but when you add a semicolon (as you did in the question) it becomes a statement (as you called it in the question) and in the absence of side effects the compiler is free to throw it away.
what about NaNs?
– Aki Suihkonen
Mar 28 at 16:04
@AkiSuihkonen a NaN will never compare equal to itself or anything else, but without side effects the statement0;is still the same as the statement1;.
– mlp
Mar 28 at 16:46
add a comment
|
NaNs are retained through float => double => float, but they not equal themselves.
#include <math.h>
#include <stdio.h>
int main(void)
float f = HUGE_VALF;
printf("%dn", f == (float)(double) f);
f = NAN;
printf("%dn", f == (float)(double) f);
printf("%dn", f == f);
Prints
1
0
0
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%2f55399820%2fwhy-is-the-statement-f-floatdoublef-wrong%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming IEC 60559, the result of f == (float)(double) f depends on the type of f.
Further assuming f is a float, then there's nothing "wrong" about the expression - it will evaluate to true (unless f held NaN, in which case the expression will evaluate to false).
On the other hand, x == (int)(double)x (assuming x is a int) is (potentially) problematic, since a double precision IEC 60559 floating point value only has 53 bits for the significand1, which cannot represent all possible values of an int if it uses more than 53 bits for its value on your platform (admittedly rare). So it will evaluate to true on platforms where ints are 32-bit (using 31 bits for the value), and might evaluate to false on platforms where ints are 64-bit (using 63 bits for the value) (depending on the value).
Relevant quotes from the C standard (6.3.1.4 and 6.3.1.5) :
When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
When a finite value of real floating type is converted to an integer type other than
_Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
When a value of real floating type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
1 a double precision IEC 60559 floating point value consists of 1 bit for the sign, 11 bits for the exponent, and 53 bits for the significand (of which 1 is implied and not stored) - totaling 64 (stored) bits.
Thank you for you answer, but I found my mistake again and edit the question again. I'm so sorry and he said double type can express every numbers that int type expresses, you mean he taught me wrong?
– Veomchan Kim
Mar 28 at 15:11
@VeomchanKim : edited my answer accordingly - the gist is the same.
– Sander De Dycker
Mar 28 at 15:12
1
@VeomchanKim : the C standard allows theinttype to have a larger range than can be represented exactly by thedoubletype. On most of today's platforms, that's not a concern though. So, strictly speaking, that statement by your professor ("double type can express every numbers that int type expresses") is wrong, but practically speaking (or when speaking about a specific environment), the statement is fine.
– Sander De Dycker
Mar 28 at 15:24
Thank you again for your comments, I appreciate them very much.
– Veomchan Kim
Mar 28 at 15:25
1
Detail: Typical double can exactly represent allint54_t(which has sign and 53 bit of precision anddoublehas sign and 52 explicitly encoded + 1 implied precision bits).
– chux
Mar 28 at 20:53
|
show 3 more comments
Assuming IEC 60559, the result of f == (float)(double) f depends on the type of f.
Further assuming f is a float, then there's nothing "wrong" about the expression - it will evaluate to true (unless f held NaN, in which case the expression will evaluate to false).
On the other hand, x == (int)(double)x (assuming x is a int) is (potentially) problematic, since a double precision IEC 60559 floating point value only has 53 bits for the significand1, which cannot represent all possible values of an int if it uses more than 53 bits for its value on your platform (admittedly rare). So it will evaluate to true on platforms where ints are 32-bit (using 31 bits for the value), and might evaluate to false on platforms where ints are 64-bit (using 63 bits for the value) (depending on the value).
Relevant quotes from the C standard (6.3.1.4 and 6.3.1.5) :
When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
When a finite value of real floating type is converted to an integer type other than
_Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
When a value of real floating type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
1 a double precision IEC 60559 floating point value consists of 1 bit for the sign, 11 bits for the exponent, and 53 bits for the significand (of which 1 is implied and not stored) - totaling 64 (stored) bits.
Thank you for you answer, but I found my mistake again and edit the question again. I'm so sorry and he said double type can express every numbers that int type expresses, you mean he taught me wrong?
– Veomchan Kim
Mar 28 at 15:11
@VeomchanKim : edited my answer accordingly - the gist is the same.
– Sander De Dycker
Mar 28 at 15:12
1
@VeomchanKim : the C standard allows theinttype to have a larger range than can be represented exactly by thedoubletype. On most of today's platforms, that's not a concern though. So, strictly speaking, that statement by your professor ("double type can express every numbers that int type expresses") is wrong, but practically speaking (or when speaking about a specific environment), the statement is fine.
– Sander De Dycker
Mar 28 at 15:24
Thank you again for your comments, I appreciate them very much.
– Veomchan Kim
Mar 28 at 15:25
1
Detail: Typical double can exactly represent allint54_t(which has sign and 53 bit of precision anddoublehas sign and 52 explicitly encoded + 1 implied precision bits).
– chux
Mar 28 at 20:53
|
show 3 more comments
Assuming IEC 60559, the result of f == (float)(double) f depends on the type of f.
Further assuming f is a float, then there's nothing "wrong" about the expression - it will evaluate to true (unless f held NaN, in which case the expression will evaluate to false).
On the other hand, x == (int)(double)x (assuming x is a int) is (potentially) problematic, since a double precision IEC 60559 floating point value only has 53 bits for the significand1, which cannot represent all possible values of an int if it uses more than 53 bits for its value on your platform (admittedly rare). So it will evaluate to true on platforms where ints are 32-bit (using 31 bits for the value), and might evaluate to false on platforms where ints are 64-bit (using 63 bits for the value) (depending on the value).
Relevant quotes from the C standard (6.3.1.4 and 6.3.1.5) :
When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
When a finite value of real floating type is converted to an integer type other than
_Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
When a value of real floating type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
1 a double precision IEC 60559 floating point value consists of 1 bit for the sign, 11 bits for the exponent, and 53 bits for the significand (of which 1 is implied and not stored) - totaling 64 (stored) bits.
Assuming IEC 60559, the result of f == (float)(double) f depends on the type of f.
Further assuming f is a float, then there's nothing "wrong" about the expression - it will evaluate to true (unless f held NaN, in which case the expression will evaluate to false).
On the other hand, x == (int)(double)x (assuming x is a int) is (potentially) problematic, since a double precision IEC 60559 floating point value only has 53 bits for the significand1, which cannot represent all possible values of an int if it uses more than 53 bits for its value on your platform (admittedly rare). So it will evaluate to true on platforms where ints are 32-bit (using 31 bits for the value), and might evaluate to false on platforms where ints are 64-bit (using 63 bits for the value) (depending on the value).
Relevant quotes from the C standard (6.3.1.4 and 6.3.1.5) :
When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
When a finite value of real floating type is converted to an integer type other than
_Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
When a value of real floating type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.
1 a double precision IEC 60559 floating point value consists of 1 bit for the sign, 11 bits for the exponent, and 53 bits for the significand (of which 1 is implied and not stored) - totaling 64 (stored) bits.
edited Mar 29 at 8:42
answered Mar 28 at 15:01
Sander De DyckerSander De Dycker
13.5k1 gold badge25 silver badges32 bronze badges
13.5k1 gold badge25 silver badges32 bronze badges
Thank you for you answer, but I found my mistake again and edit the question again. I'm so sorry and he said double type can express every numbers that int type expresses, you mean he taught me wrong?
– Veomchan Kim
Mar 28 at 15:11
@VeomchanKim : edited my answer accordingly - the gist is the same.
– Sander De Dycker
Mar 28 at 15:12
1
@VeomchanKim : the C standard allows theinttype to have a larger range than can be represented exactly by thedoubletype. On most of today's platforms, that's not a concern though. So, strictly speaking, that statement by your professor ("double type can express every numbers that int type expresses") is wrong, but practically speaking (or when speaking about a specific environment), the statement is fine.
– Sander De Dycker
Mar 28 at 15:24
Thank you again for your comments, I appreciate them very much.
– Veomchan Kim
Mar 28 at 15:25
1
Detail: Typical double can exactly represent allint54_t(which has sign and 53 bit of precision anddoublehas sign and 52 explicitly encoded + 1 implied precision bits).
– chux
Mar 28 at 20:53
|
show 3 more comments
Thank you for you answer, but I found my mistake again and edit the question again. I'm so sorry and he said double type can express every numbers that int type expresses, you mean he taught me wrong?
– Veomchan Kim
Mar 28 at 15:11
@VeomchanKim : edited my answer accordingly - the gist is the same.
– Sander De Dycker
Mar 28 at 15:12
1
@VeomchanKim : the C standard allows theinttype to have a larger range than can be represented exactly by thedoubletype. On most of today's platforms, that's not a concern though. So, strictly speaking, that statement by your professor ("double type can express every numbers that int type expresses") is wrong, but practically speaking (or when speaking about a specific environment), the statement is fine.
– Sander De Dycker
Mar 28 at 15:24
Thank you again for your comments, I appreciate them very much.
– Veomchan Kim
Mar 28 at 15:25
1
Detail: Typical double can exactly represent allint54_t(which has sign and 53 bit of precision anddoublehas sign and 52 explicitly encoded + 1 implied precision bits).
– chux
Mar 28 at 20:53
Thank you for you answer, but I found my mistake again and edit the question again. I'm so sorry and he said double type can express every numbers that int type expresses, you mean he taught me wrong?
– Veomchan Kim
Mar 28 at 15:11
Thank you for you answer, but I found my mistake again and edit the question again. I'm so sorry and he said double type can express every numbers that int type expresses, you mean he taught me wrong?
– Veomchan Kim
Mar 28 at 15:11
@VeomchanKim : edited my answer accordingly - the gist is the same.
– Sander De Dycker
Mar 28 at 15:12
@VeomchanKim : edited my answer accordingly - the gist is the same.
– Sander De Dycker
Mar 28 at 15:12
1
1
@VeomchanKim : the C standard allows the
int type to have a larger range than can be represented exactly by the double type. On most of today's platforms, that's not a concern though. So, strictly speaking, that statement by your professor ("double type can express every numbers that int type expresses") is wrong, but practically speaking (or when speaking about a specific environment), the statement is fine.– Sander De Dycker
Mar 28 at 15:24
@VeomchanKim : the C standard allows the
int type to have a larger range than can be represented exactly by the double type. On most of today's platforms, that's not a concern though. So, strictly speaking, that statement by your professor ("double type can express every numbers that int type expresses") is wrong, but practically speaking (or when speaking about a specific environment), the statement is fine.– Sander De Dycker
Mar 28 at 15:24
Thank you again for your comments, I appreciate them very much.
– Veomchan Kim
Mar 28 at 15:25
Thank you again for your comments, I appreciate them very much.
– Veomchan Kim
Mar 28 at 15:25
1
1
Detail: Typical double can exactly represent all
int54_t (which has sign and 53 bit of precision and double has sign and 52 explicitly encoded + 1 implied precision bits).– chux
Mar 28 at 20:53
Detail: Typical double can exactly represent all
int54_t (which has sign and 53 bit of precision and double has sign and 52 explicitly encoded + 1 implied precision bits).– chux
Mar 28 at 20:53
|
show 3 more comments
Taking the question as posed in the title literally,
Why is the statement “f == (float)(double)f;” wrong?
the statement is "wrong" not in any way related to the representation of floating point values but because it is trivially optimized away by any compiler and thus you might as well have saved the electrons used to store it. It is exactly equivalent to the statement
1;
or, if you like, to the statement (from the original question)
x == (int)(double)x;
(which has exactly the same effect as that in the title, regardless of the available precision of the types int, float, and double, i.e. none whatsoever).
Programming being somewhat concerned with precision you should perhaps take note of the difference between a statement and an expression. An expression has a value which might be true or false or something else, but when you add a semicolon (as you did in the question) it becomes a statement (as you called it in the question) and in the absence of side effects the compiler is free to throw it away.
what about NaNs?
– Aki Suihkonen
Mar 28 at 16:04
@AkiSuihkonen a NaN will never compare equal to itself or anything else, but without side effects the statement0;is still the same as the statement1;.
– mlp
Mar 28 at 16:46
add a comment
|
Taking the question as posed in the title literally,
Why is the statement “f == (float)(double)f;” wrong?
the statement is "wrong" not in any way related to the representation of floating point values but because it is trivially optimized away by any compiler and thus you might as well have saved the electrons used to store it. It is exactly equivalent to the statement
1;
or, if you like, to the statement (from the original question)
x == (int)(double)x;
(which has exactly the same effect as that in the title, regardless of the available precision of the types int, float, and double, i.e. none whatsoever).
Programming being somewhat concerned with precision you should perhaps take note of the difference between a statement and an expression. An expression has a value which might be true or false or something else, but when you add a semicolon (as you did in the question) it becomes a statement (as you called it in the question) and in the absence of side effects the compiler is free to throw it away.
what about NaNs?
– Aki Suihkonen
Mar 28 at 16:04
@AkiSuihkonen a NaN will never compare equal to itself or anything else, but without side effects the statement0;is still the same as the statement1;.
– mlp
Mar 28 at 16:46
add a comment
|
Taking the question as posed in the title literally,
Why is the statement “f == (float)(double)f;” wrong?
the statement is "wrong" not in any way related to the representation of floating point values but because it is trivially optimized away by any compiler and thus you might as well have saved the electrons used to store it. It is exactly equivalent to the statement
1;
or, if you like, to the statement (from the original question)
x == (int)(double)x;
(which has exactly the same effect as that in the title, regardless of the available precision of the types int, float, and double, i.e. none whatsoever).
Programming being somewhat concerned with precision you should perhaps take note of the difference between a statement and an expression. An expression has a value which might be true or false or something else, but when you add a semicolon (as you did in the question) it becomes a statement (as you called it in the question) and in the absence of side effects the compiler is free to throw it away.
Taking the question as posed in the title literally,
Why is the statement “f == (float)(double)f;” wrong?
the statement is "wrong" not in any way related to the representation of floating point values but because it is trivially optimized away by any compiler and thus you might as well have saved the electrons used to store it. It is exactly equivalent to the statement
1;
or, if you like, to the statement (from the original question)
x == (int)(double)x;
(which has exactly the same effect as that in the title, regardless of the available precision of the types int, float, and double, i.e. none whatsoever).
Programming being somewhat concerned with precision you should perhaps take note of the difference between a statement and an expression. An expression has a value which might be true or false or something else, but when you add a semicolon (as you did in the question) it becomes a statement (as you called it in the question) and in the absence of side effects the compiler is free to throw it away.
answered Mar 28 at 15:49
mlpmlp
6286 silver badges16 bronze badges
6286 silver badges16 bronze badges
what about NaNs?
– Aki Suihkonen
Mar 28 at 16:04
@AkiSuihkonen a NaN will never compare equal to itself or anything else, but without side effects the statement0;is still the same as the statement1;.
– mlp
Mar 28 at 16:46
add a comment
|
what about NaNs?
– Aki Suihkonen
Mar 28 at 16:04
@AkiSuihkonen a NaN will never compare equal to itself or anything else, but without side effects the statement0;is still the same as the statement1;.
– mlp
Mar 28 at 16:46
what about NaNs?
– Aki Suihkonen
Mar 28 at 16:04
what about NaNs?
– Aki Suihkonen
Mar 28 at 16:04
@AkiSuihkonen a NaN will never compare equal to itself or anything else, but without side effects the statement
0; is still the same as the statement 1;.– mlp
Mar 28 at 16:46
@AkiSuihkonen a NaN will never compare equal to itself or anything else, but without side effects the statement
0; is still the same as the statement 1;.– mlp
Mar 28 at 16:46
add a comment
|
NaNs are retained through float => double => float, but they not equal themselves.
#include <math.h>
#include <stdio.h>
int main(void)
float f = HUGE_VALF;
printf("%dn", f == (float)(double) f);
f = NAN;
printf("%dn", f == (float)(double) f);
printf("%dn", f == f);
Prints
1
0
0
add a comment
|
NaNs are retained through float => double => float, but they not equal themselves.
#include <math.h>
#include <stdio.h>
int main(void)
float f = HUGE_VALF;
printf("%dn", f == (float)(double) f);
f = NAN;
printf("%dn", f == (float)(double) f);
printf("%dn", f == f);
Prints
1
0
0
add a comment
|
NaNs are retained through float => double => float, but they not equal themselves.
#include <math.h>
#include <stdio.h>
int main(void)
float f = HUGE_VALF;
printf("%dn", f == (float)(double) f);
f = NAN;
printf("%dn", f == (float)(double) f);
printf("%dn", f == f);
Prints
1
0
0
NaNs are retained through float => double => float, but they not equal themselves.
#include <math.h>
#include <stdio.h>
int main(void)
float f = HUGE_VALF;
printf("%dn", f == (float)(double) f);
f = NAN;
printf("%dn", f == (float)(double) f);
printf("%dn", f == f);
Prints
1
0
0
answered Mar 29 at 7:51
Antti HaapalaAntti Haapala
93.8k18 gold badges185 silver badges219 bronze badges
93.8k18 gold badges185 silver badges219 bronze badges
add a comment
|
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%2f55399820%2fwhy-is-the-statement-f-floatdoublef-wrong%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
What reasoning did your professor give for why this is wrong?
– Daniel Pryden
Mar 28 at 14:18
7
The only thing I see wrong is that you initialize the variable with itself.
– Eugene Sh.
Mar 28 at 14:20
3
What are you trying to achieve with that statement ?
– Sander De Dycker
Mar 28 at 14:21
@Eugenesh I did edit the question. Sorry.
– Veomchan Kim
Mar 28 at 14:43
@DanielPryden He didn't mentioned any reason, just saying it is wrong.
– Veomchan Kim
Mar 28 at 14:43