Float and double, precision and scale??

Discussion in 'C++' started by Erik2000, Jun 27, 2003.

  1. Erik2000

    Erik2000 Guest

    If someone defines a float and double in C++, does that say anything
    about how many digits the number will hold at maximum and how many
    places there will be after the decimal point?

    Or is this open to interpretation?

    --
    Posted via http://dbforums.com
    Erik2000, Jun 27, 2003
    #1
    1. Advertising

  2. Erik2000 wrote in news::

    >
    > If someone defines a float and double in C++, does that say anything
    > about how many digits the number will hold at maximum and how many
    > places there will be after the decimal point?
    >
    > Or is this open to interpretation?
    >


    The class template std::numeric_limits< Type >

    defined in <limits> has a whole bunch of static members
    that describe the properties of C++'s numeric types.
    Here's an example:

    #include <iostream>
    #include <ostream>
    #include <limits>

    int main()
    {
    typedef std::numeric_limits< double > dl;
    typedef std::numeric_limits< float > fl;

    using namespace std;

    cout << "double:\n";
    cout << "\tdigits (bits):\t\t" << dl::digits << endl;
    cout << "\tdigits (decimal):\t" << dl::digits10 << endl;

    cout << endl;

    cout << "float:\n";
    cout << "\tdigits (bits):\t\t" << fl::digits << endl;
    cout << "\tdigits (decimal):\t" << fl::digits10 << endl;
    }

    I get:

    double:
    digits (bits): 53
    digits (decimal): 15

    float:
    digits (bits): 24
    digits (decimal): 6

    on my system.


    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jun 27, 2003
    #2
    1. Advertising

  3. Erik2000 wrote in news::

    >
    >>double:
    >>digits (bits): 53
    >>digits (decimal): 15
    >>
    >>float:
    >>digits (bits): 24
    >>digits (decimal): 6
    >>
    >>on my system.

    >
    >
    > So does this mean that a double on your system can have up to 53 digits


    No the 53 is bits, computers (well most computers) store floating
    point values in binary, base 2 not base 10 (decimal).

    The 15 is the number of decimal digits. i.e. how meny decimal
    digits can be stored in 53 bits.

    > and 15 digits behind the decimal point?


    The decimal point doesn't come in to it, hence the term
    *floating-point*.

    for example the number 2.3e20 doesn't really have "point" it is
    230000000000000000000, also the number 2.3e-20 is
    0.0000000000000000023, i.e. the "point" isn't within the digits
    at all.

    >
    > And for float, 24 digits and 6 digits behind the decimal point?


    As above 6 decimal digits total or 24 bits (base 2 digits).


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jun 27, 2003
    #3
  4. Erik2000 wrote:
    >
    > >double:
    > >digits (bits): 53
    > >digits (decimal): 15
    > >
    > >float:
    > >digits (bits): 24
    > >digits (decimal): 6
    > >
    > >on my system.

    >
    > So does this mean that a double on your system can have up to 53 digits
    > and 15 digits behind the decimal point?


    No it means, that a double uses 53 bits as its mantissa.
    In decimal this gives an average precission of 15 digits.

    >
    > And for float, 24 digits and 6 digits behind the decimal point?


    24 bits for the mantissa which results in usually
    6 decimal digits.

    Note: 100000.4
    already uses 7 decimal digits and thus cannot be represented proerly
    with this float. (Just count the digits and ignore the decimal point).

    -> Conclusion: float isn't big enough most of the time to do serious
    calculations, at least not in a technical applications. So forget
    about float until you know what you do.

    You might also want to check out:

    http://docs-pdf.sun.com/800-7895/800-7895.pdf

    >
    > 53 digits is a really big number.


    54 is bigger :)

    >
    > --
    > Posted via http://dbforums.com


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jun 27, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,444
    Victor Bazarov
    Feb 17, 2005
  2. bd
    Replies:
    0
    Views:
    602
  3. Replies:
    9
    Views:
    513
    Victor Bazarov
    Mar 27, 2009
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,500
    James Kanze
    Oct 8, 2009
  5. Anton81

    Float precision and float equality

    Anton81, Dec 5, 2009, in forum: Python
    Replies:
    26
    Views:
    1,112
Loading...

Share This Page