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;








8















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



enter image description here




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.










share|improve this question





















  • 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

















8















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



enter image description here




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.










share|improve this question





















  • 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













8












8








8


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



enter image description here




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.










share|improve this question
















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



enter image description here




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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












3 Answers
3






active

oldest

votes


















11
















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.






share|improve this answer



























  • 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 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






  • 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


















1
















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.






share|improve this answer

























  • 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


















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





share|improve this answer



























    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
    );



    );














    draft saved

    draft discarded
















    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









    11
















    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.






    share|improve this answer



























    • 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 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






    • 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















    11
















    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.






    share|improve this answer



























    • 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 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






    • 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













    11














    11










    11









    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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






    • 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

















    • 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 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






    • 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
















    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













    1
















    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.






    share|improve this answer

























    • 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















    1
















    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.






    share|improve this answer

























    • 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













    1














    1










    1









    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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 statement 0; is still the same as the statement 1;.

      – mlp
      Mar 28 at 16:46

















    • 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
















    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











    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





    share|improve this answer





























      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





      share|improve this answer



























        0














        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        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































            draft saved

            draft discarded















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해