double or float

  • Thread starter E. Robert Tisdale
  • Start date
C

Christian Christmann

Hi,

just a general question.
Which data type is more precise for large values:
float or double?

Thank you.
Chris
 
V

Victor Bazarov

Christian said:
just a general question.
Which data type is more precise for large values:
float or double?

Usually, 'double' contains more _significant_digits_, so, as
a general case, 'double' is more precise for *any* values.
Also, usually, 'double' has a wider range.

V
 
M

mlimber

Christian said:
Hi,

just a general question.
Which data type is more precise for large values:
float or double?

Define "large" and "values". If you mean integral values, then an
integer is more precise, assuming it can hold the value. Your compiler
may support 64-bit integers, which hold a fairly vast range.

If you require floating point computations or if the numbers are too
large for integral types, use floating point types. The sizes of float,
double, and long double are implementation-defined, but it is
guaranteed that:

sizeof(float) <= sizeof(double) <= sizeof(long double)

More bits generally equals more precision, but it depends how the bits
are allocated (i.e. what the floating-point format is). For more info
on floating point arithmetic, see "What Every Computer Scientist Should
Know About Floating-Point Arithmetic"
(http://docs.sun.com/source/806-3568/ncg_goldberg.html).

Cheers! --M
 
R

Rui Maciel

Christian said:
Hi,

just a general question.
Which data type is more precise for large values:
float or double?

Thank you.
Chris

Hi Chris

First of all, your question is somewhat off-topic to this newsgroup. AFAIK,
the precision of C++ floating point data types is platform-dependent and
not a language subject. You would get a better answer if you posted your
question in a numerical analysis group you would get better answers than in
this newsgroup.

Still, here are my 0.02?

The float data type and the double data types are floating point data types.
A floating point data type is a digital representation of a number, which
aims to be equivalent to a real number.

Physically, a floating point data type is a set of bits which is divided
into sections (ranges of bits in the bit set). Two of those bit sections
are the significand and the exponent parts of the number. If the size of
the data type increases, the bits available to be allocated to the
significand section and to the exponent section also increase. So, if the
data type gets bigger, it also increases it's precision and it's range.

So, to finally answer your question, being the size of the double data type
usually bigger than the size of the float, then the precision of the double
data type should be greater than the precision of the float data type. But
have in mind that in the end this is all platform-dependent and not a
language issue.

for a light introduction check out the wikipedia article:
http://en.wikipedia.org/wiki/Floating_point


Hope this helps
Rui Maciel
 
K

Karl Heinz Buchegger

Christian said:
Hi,

just a general question.
Which data type is more precise for large values:
float or double?

On thing has not been mentioned up to now:

Use float only, iff:
* you know what you do
* you have the knowledge to fight that beast
* you are willing to fight that beast
* you have a very, very, very good reason to use float
* you have at least 4 times thought the whole thing through and
still come to the conclusion that float would be a good idea in
this specific case.

In all other cases (and that accounts for >98% of all cases): use double
The usual limitted precision of float will kill you.
 
F

f0r3nsic

Ain't float faster on 32-bit machines (although I agree it has pretty
limited precision)?
 
T

tmartsum

float is normally faster and normally half the size.
I have had sizeproblems with double in a homemade racetrayer
where I had a figure with about 20 millon triangles ...

But I agree with Karl. Use double unless there are real good reasons to
use float.
 
D

Dave Rahardja

Ain't float faster on 32-bit machines (although I agree it has pretty
limited precision)?

It depends--on certain machines with floating point math units, it could make
basically no difference.
 
T

tmartsum

Yeah its true
sizeof(float) <= sizeof(double) <= sizeof(long double)

- but can you give me an example on a platform/compiler with
float = double ?

And if you can then still nothing is lost by writing double instead of
float.
 
K

Karl Heinz Buchegger

tmartsum said:
float is normally faster and normally half the size.
I have had sizeproblems with double in a homemade racetrayer
where I had a figure with about 20 millon triangles ...

Now *that* is a real, real, real good reason.
And I guess the problem of 'surface acne' kept you busy
for quite a while.

('Surface acne' is the name for the arithmetic problem that your
formulas come up with an intersection, when there really is none.
Such as a shadow ray emitting from a surface with the surface itself.
The ray tracer erronously sets this pixel into shadow giving the whole
surface a speckled appearance)
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top