Velocity Reviews > C++ > Float and double, precision and scale??

# Float and double, precision and scale??

Erik2000
Guest
Posts: n/a

 06-27-2003

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

Rob Williscroft
Guest
Posts: n/a

 06-27-2003
Erik2000 wrote in news:(E-Mail Removed):

>
> 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
Guest
Posts: n/a

 06-27-2003
Erik2000 wrote in news:(E-Mail Removed):

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

Karl Heinz Buchegger
Guest
Posts: n/a

 06-27-2003

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
http://www.velocityreviews.com/forums/(E-Mail Removed)