Thread: double data type in c language

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    17

    double data type in c language

    There are four data type in c language int, chat, float and double. I can understand when to use first three data type but double data type I can't figure out when to use it.

    how do you use it?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    There are a lot of different types:
    integer (char, short, int, long, long long; signed, unsigned)
    floating point (float, double, long double)
    complex (float, double, long double)
    pointer
    array
    structure
    union
    enumeration
    function
    atomic
    void
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,011
    Quote Originally Posted by king12 View Post
    There are four data type in c language int, chat, float and double. I can understand when to use first three data type but double data type I can't figure out when to use it.

    how do you use it?
    Please read my response in another thread, and follow my recommendations. I have posted this several times over the years.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    903
    float usually has less precision and range than double. C uses double for all floating-point arithmetic (even if both operands of an operator are float). Use double to prevent truncating results to float.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    C uses double for all floating-point arithmetic (even if both operands of an operator are float).
    That's not true.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    903
    Quote Originally Posted by john.c View Post
    That's not true.
    Oh no, you're right! I just looked up the C99 standard (later versions are probably the same on this particular matter), and an arithmetic operation on two float values results in a float type result. I must have been thinking of the default type of a floating-point literal (e.g., 1.0) being double, unless you append f or F to it.

    To be frank, I haven't done much with floating point for years (I develop embedded software where floating point is rarely used).

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    Oh no, you're right!
    I think it might have been true in the original version of C. Also, when a float is passed to a function that accepts a variable number of arguments such as printf, it's converted to a double. That's why %f and %lf are interchangeable for printf. (Not for scanf, though, since that is passed a pointer.)

    Anyway, float is pretty much just used to save space if you need a large number of floating point values and don't need the extra precision or range of double. It could also speed up processing by holding more in the CPU cache. Otherwise you should probably just use double. If you need to be able to represent a 64-bit integer without loss of precision in a floating point value, then you need to use a long double since a regular double only represents 53 bits of precision.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,048
    Since people here are very touchy about ISO 9899 standard, the format of float, double and long double aren't defined there. 6.2.5 10 only says that float is a subset of double, and double is a subset of long double. By "subset" this means that all of three types could have the same representation (but it's not usual!).

    Later, in 5.2.4.2.2 some details about float.h are discussed and 8 says that the values of symbols FLT_RADIX, FLT_MANT_DIG etc are implementation defined.

    Usually, C follows ISO/IEC 60559 (IEEE 754:2008 equivalent standard) where float have 24 bits of precision, double, 53 and long double, 64, with 8, 11 and 15 bits of expoent for the scale factor, respectively.

    SysV ABI allows for __float128 type as well (optional, available on GCC/CLANG, with support using quadmath library), with 113 bits of precision and 15 bits of expoent (scale factor). This doesn't have hardware support, so it is software implemented.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    903
    Quote Originally Posted by john.c View Post
    Anyway, float is pretty much just used to save space if you need a large number of floating point values and don't need the extra precision or range of double.
    And if you need to save more space, use a minifloat format. These are commonly used in graphics processing in, e.g., GPUs. But they're not supported directly by C and usually have to be emulated in software (except when they're processed by hardware like a GPU).

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,011
    Quote Originally Posted by christop View Post
    And if you need to save more space, use a minifloat format. These are commonly used in graphics processing in, e.g., GPUs. But they're not supported directly by C and usually have to be emulated in software (except when they're processed by hardware like a GPU).
    Then why even mention it when, as a beginner in C programming, the OP wants to learn about the basic data types in standard C?
    Last edited by rstanley; 09-02-2022 at 08:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner question double data type
    By muke786 in forum C Programming
    Replies: 4
    Last Post: 04-16-2020, 12:02 AM
  2. changing a string to a 'double' data type
    By Brian_Jones in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2009, 05:59 PM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 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
  5. Double Data Type
    By HallmanBilly in forum C Programming
    Replies: 2
    Last Post: 09-01-2007, 03:25 PM

Tags for this Thread