Float and double, precision and scale??

E

Erik2000

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

Rob Williscroft

Erik2000 wrote in
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.
 
R

Rob Williscroft

Erik2000 wrote in
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.
 
K

Karl Heinz Buchegger

Erik2000 said:
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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top