Thread: Why have double type when float defaults to double

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    10

    Why have double type when float defaults to double

    I am a little confused - what is the purpose of 'double' type if 'float' already defaults to 'double'?

    Code:
    float number_1 23.00
    
    double number_2 23.00
    Last edited by jason_jackal; 02-14-2018 at 09:18 AM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    It isn't the variable that "defaults" to double. It's only the CONSTANTS. I.e., in
    Code:
    float x = 1.23;
    x is a float (4 bytes), whereas 1.23 is a double (8 bytes) which will need to be converted to a float before being assigned to x.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    10
    Quote Originally Posted by john.c View Post
    x is a float (4 bytes), whereas 1.23 is a double (8 bytes) which will need to be converted to a float before being assigned to x.
    Hmmm... I am still struggling I thought that since we declaired the variable type with 'float x' then whatever gets assigned to 'x' is a float. How is '1.23' a double, when X was assigned float?

    Do you know of any resources that I can use that will explain it too with layman's terms and examples?

    I am not a programmer just learning it because I always wanted to learn C.

    Thank you

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I suggest that for now you just forget that float exists and use double instead. With modern desktop systems you really should generally prefer doubles over floats because of the added precision and less round off errors. The difference in storage size and speed is really not much of a problem, except in certain cases (such as some games or when you have millions of operations). And note that most of the functions prototyped in math.h use doubles for parameters not floats so if you use a float you will do an implicit conversion when you use those functions.

    The problem you seem to be having is that you don't seem to understand that by default floating point literals are considered doubles. It doesn't matter what type of floating point variable you're trying to use, be it a float, a double, or a long double.

    Code:
    float fvalue = 1.23;
    double dvalue = 1.23;
    long double ldvalue = 1.23;
    In the above the 1.23 is the part we are talking about, the variable and the type doesn't matter.

    To force the compiler to use the proper type for the literal you need to use the proper suffix when declaring the variable.

    Code:
    float fvalue = 1.23f;  // or 1.23F;
    double dvalue = 1.23;  // No suffix required, the literal is already a double.
    long double ldvalue = 1.23l;  // or 1.23L;

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    10
    Hi Jim,
    Ok I will move on, and I hope to get it at a later time. I do understand that float is 4 bytes and double is 8 bytes, and some additional precision on decimal place.

    One last question before I move with my studies, since I really lost a lot of time trying to understand this. I am one of those people that will exhaust all options and sources to understand something in which I do not, so I appreciate your help.
    Code:
    double dvalue = 1.23;
    could the above be writen as the following to force to a 'float' at 4 bytes?

    Code:
    double fvalue = 1.23F;
    Just to confirm - it does not matter in this case if it 'f' or 'F' correct?

    Case only matters in 'printf' function correct?

    thank you

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    could the above be writen as the following to force to a 'float' at 4 bytes?
    You don't have a float to "force" a floating point literal into so??

    By the way your second snippet will probably cause problems because your telling the compiler that that literal is a float, with much less precision then that the double you're trying to store that literal.

    Remember float and double are distinct types.

    The literal "1.23" is considered a double by the compiler so no conversion will be necessary to assign that literal to a double.

  7. #7
    Registered User
    Join Date
    Feb 2018
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    You don't have a float to "force" a floating point literal into so??

    By the way your second snippet will probably cause problems because your telling the compiler that that literal is a float, with much less precision then that the double you're trying to store that literal.

    Remember float and double are distinct types.

    The literal "1.23" is considered a double by the compiler so no conversion will be necessary to assign that literal to a double.
    Oh.. I do appreciate it.

    Do you know of any update to date books or resources that are good for learning?

    Currently using a resources that used 'void main()' oppose to 'int main(void)' and I had some problems trying to add the 'return 0' into my code. It would appear that 'void main()' is not a preferred method.

  8. #8
    Guest
    Guest
    I am still struggling I thought that since we declaired the variable type with 'float x' then whatever gets assigned to 'x' is a float. How is '1.23' a double, when X was assigned float?
    My attempt, may this helps you:
    Code:
    float a = …anything…; // 'a' is always a float, guaranteed.
    double b = …anything…; // 'b' is always a double.
    
    …anything… = 1.23; // 1.23 is always a double (hence the "default" for numeric literals).
    …anything… = 1.23f; // 1.23f is always a float.
    
    float af = 1.23; // 1.23 (double) will get stored as a float, losing precision.
    float bf = 1.23f; // 1.23f (float) will get stored as a float, no conversion necessary.
    
    double ad = 1.23; // 1.23 (double) will get stored as double, no conversion...
    double bd = 1.23f; // 1.23f (float) will get stored as double, but lacks initial precision as Jim pointed out.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Unadorned floating point numbers will be implicitly converted to the variable type they are being assigned to. If you use suffixes correctly and turn the warning on, then it will create warnings when you mess with the true value.
    Code:
    C:\Users\jk\Desktop>gcc -Wfloat-conversion -c foo.c
    foo.c:1:11: warning: conversion to 'float' alters 'long double' constant value [-Wfloat-conversion]
     float f = 1.23L;
               ^
    As for books, there is a recommendation sticky in the C forum. Anyone with a high post count probably recommended a good book.

  10. #10
    Registered User
    Join Date
    Feb 2018
    Posts
    10
    So can I think of it this way?

    float f = 1.23 but what is really being said is the following assuming double is 15 place decimal precision and float is 7 decimal precision according to my book

    1.23 really seen by the application as 1.230000000000000
    However,
    float f = 1.23 will convert down to 1.23000000 and the conversion might not be correct?

  11. #11
    Registered User
    Join Date
    Feb 2018
    Posts
    10
    Quote Originally Posted by whiteflags View Post
    As for books, there is a recommendation sticky in the C forum. Anyone with a high post count probably recommended a good book.
    I will look in the sticky... I believe I overlooked it.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by jason_jackal View Post
    So can I think of it this way?

    float f = 1.23 but what is really being said is the following assuming double is 15 place decimal precision and float is 7 decimal precision according to my book

    1.23 really seen by the application as 1.230000000000000
    However,
    float f = 1.23 will convert down to 1.23000000 and the conversion might not be correct?
    Well, okay, 1.23 might not be the best example, because the precision would not be harmed in any case (1.23 fits fine in a double or a float). But if for some reason - God help you - you are working with high precision numbers or perhaps numbers on the large end of its range, then representing constants and using the right types will matter a lot. Your numbers will get truncated otherwise, and in that case it could lead to misleading answers from the computer, and such.

  13. #13
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Would you hav been just as confused with this somewhat analogous example?
    Code:
    short n = 42;
    Let's assume ints are 4-bytes and shorts are 2-bytes. Unadorned integer constants default to int (unless beyond int range). So 42 is a 4-byte int constant. Clearly there needs to be some kind of "conversion" to assign it to a 2-byte short since you can't just copy 4 bytes into 2 bytes. In the case of 42 there's no problem, but if we wrote 1000000 instead of 42 we should get a compiler warning that the value can't be converted to fit. BTW, there is no suffix to make the constant itself a short.

    Here's a look at the different precision of float and double:
    Code:
    #include <stdio.h>
    
    int main() {
        double d = 1.23456789012345678901234567890;
        float f = d;
        printf("s: 1.23456789012345678901234567890\n");
        printf("d: %.60lf\n", d);
        printf("f: %.60f\n", f);
        return 0;
    }
    
    /* Output
    
    s: 1.23456789012345678901234567890
    d: 1.234567890123456690432135474111419171094894409179687500000000
    f: 1.234567880630493164062500000000000000000000000000000000000000
    */
    A little inaccuracy saves tons of explanation. - H.H. Munro

  14. #14
    Registered User
    Join Date
    Feb 2018
    Posts
    10
    Quote Originally Posted by john.c View Post
    Would you hav been just as confused with this somewhat analogous example?
    Code:
    short n = 42;
    Let's assume ints are 4-bytes and shorts are 2-bytes. Unadorned integer constants default to int (unless beyond int range). So 42 is a 4-byte int constant. Clearly there needs to be some kind of "conversion" to assign it to a 2-byte short since you can't just copy 4 bytes into 2 bytes. In the case of 42 there's no problem, but if we wrote 1000000 instead of 42 we should get a compiler warning that the value can't be converted to fit. BTW, there is no suffix to make the constant itself a short.

    Here's a look at the different precision of float and double:
    Code:
    #include <stdio.h>
    
    int main() {
        double d = 1.23456789012345678901234567890;
        float f = d;
        printf("s: 1.23456789012345678901234567890\n");
        printf("d: %.60lf\n", d);
        printf("f: %.60f\n", f);
        return 0;
    }
    
    /* Output
    
    s: 1.23456789012345678901234567890
    d: 1.234567890123456690432135474111419171094894409179687500000000
    f: 1.234567880630493164062500000000000000000000000000000000000000
    */
    Thank you... You beat me to coding up a model for this. This was very helpful...

    I want to thank everyone for the help. All of you have represented the C programming community GREAT!!! I look forward to more great discussions and my continue learning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type conversion from float to double
    By Ufuk in forum C Programming
    Replies: 7
    Last Post: 07-20-2017, 01:51 PM
  2. Replies: 11
    Last Post: 04-15-2012, 07:10 PM
  3. double and float array type
    By a.mlw.walker in forum C Programming
    Replies: 1
    Last Post: 08-13-2011, 05:13 AM
  4. C - float and double data-type question..
    By ShadeS_07 in forum C Programming
    Replies: 10
    Last Post: 07-14-2008, 06:06 AM

Tags for this Thread