467,169 Members | 965 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

Float and double, precision and scale??


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
Jul 19 '05 #1
  • viewed: 108144
Share:
3 Replies
Erik2000 wrote in news:30****************@dbforums.com:

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/
Jul 19 '05 #2
Erik2000 wrote in news:30****************@dbforums.com:
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/
Jul 19 '05 #3


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
kb******@gascad.at
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by spasmous@yahoo.com | last post: by
8 posts views Thread by vijay | last post: by
8 posts views Thread by abdul_n_khan@hotmail.com | last post: by
60 posts views Thread by Erick-> | last post: by
9 posts views Thread by richard_lavoie@gmx.de | last post: by
11 posts views Thread by Ole Nielsby | last post: by
6 posts views Thread by Jeff Kish | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.