float? double?

E

Erick->

hi all...

I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??

thanks
Erick
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Erick- said:
hi all...

I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??

That's a fairly open-ended question.

However, considering that C floatingpoint math is conducted in
double-precision (with conversions to and from single precision only to
store or retrieve values from variables), and that (for certain types
of function) single-precision floatingpoint values ("float") are
promoted to double-precision values ("double") automagically, I'd have
to say that the only benefit float has over double is that float may
take less storage space than double.

For my money, unless space is an issue, it's probably better to stick
with double as the default floatingpoint format for your code.

Just my 2cents worth
- --
Lew Pitcher


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFE5hHGagVFX4UWr64RAmY2AKCBaT3TGdQdV9z8BHRXDcEgbNw5dACcCN8A
Ljp+/QJFHbOg99yloCUp00c=
=lP4v
-----END PGP SIGNATURE-----
 
W

Walter Roberson

Erick-> said:
I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??


Which is best, a pickup truck, or a half-tonne truck?


float and double are defined in terms of minimum precision allowed
for each. On any given platform, it is not required that there is
*any* difference between the two: if the float data type meets the
minimum requirements that C imposes on the double data type,
then the two could be exactly the same.

Traditionally, float was faster than double but offered less
precision. double never offers -less- precision than float, but
these days, it is not uncommon to find computers on which double
is as fast (or faster than) float. Also, float never occupies -more-
permanent storage than does double, and sometimes memory size
is the biggest factor (but these days you usually just go out and
buy more memory if you need it.)

There are also still computers which do not implement either float or
double in hardware, so from a speed perspective, sometimes both
are significantly worse than using integer arithmetic instead. But
there are also numerous computers these days on which integer arithmetic
is slower than double -- computers being sold into markets where
(say) 95% of the operations requiring speed are likely to be
floating point operations, so the development resources are spent
primarily on accelarating floating point. Thus, from a speed
perspective, you cannot trust that floating point with be either
slower or faster than integer arithmetic. But there -are- times
when using integer arithmetic can be absolutely crucial for
preserving required accuracy.

All of which is to say, "it depends" ;-)
Size, speed, precision: for any given task, any of them might be
the key factor. Speed is particularily variable: a CPU upgrade
without changing anything else might completely alter the speed factors.
 
W

Walter Roberson

However, considering that C floatingpoint math is conducted in
double-precision (with conversions to and from single precision only to
store or retrieve values from variables),

That's incorrect, at least for C89 (I haven't checked C99.)

The "usual arithmetic conversions" choses long double if necessary,
then double if necessary, then:

Otherwise, if either operand has type float, the other operand
is converted to float.

The descriptions of operations such as binary + refer to the
usual arithmetic conversions, but definitely do NOT say that
float is promoted to double for the purposes of the calculation.
 
T

Tim Prince

Erick- said:
hi all...

I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??
float would be used when better performance is required (faster, or less
storage used). double, on most current platforms, gives more range and
precision, and generally requires less care about numerical issues.
 
J

jmcgill

Tim said:
float would be used when better performance is required (faster, or less
storage used).

Be certain that this is true for your platform before making such a
decision. It's entirely possible that your FPU datapath is more
efficient with doubles than with a shorter format. It's also possible
that float and double are the same datatype.

I would suggest the possibility that for a given application, increased
precision or range could conceivably be a liability. And in projects
I've worked on, I've seen routines that were dependent on assumptions
about the internal representation of the data (bad, not my fault, not in
my power to fix).

Anyway, check before assuming that float is more efficient than double.
It's entirely possible that the assumptions are backwards from the
results.
double, on most current platforms, gives more range and
precision, and generally requires less care about numerical issues.

I don't know about "most current platforms" because every time I make an
assumption about that, I'm reminded that what I think is the predominate
platform may be far from correct... I would seriously consider checking
into whether the current ARM chips have double fpu registers, and how
the compilers for that platform define float and double...

Now granted, in my tiny corner of the world, floats are 4 bytes and
doubles are 8 and long doubles 12.


Shoot me if I ever write anything that depends on that. (Wait, no, I
*have to* do that still, don't shoot.)
 
J

jacob navia

Erick- said:
hi all...

I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??

thanks
Erick

double means DOUBLE PRECISION. Note the word precision here.

When you want more precision, use double precision, and if that
doesn't cut it use long double.

Precision means the number of significant digits you get
for the calculations. float gives you 6 digits precision,
(assuming IEEE 754) double gives you 15, and long double
more than that, using Intel/Amd implementation gives you 18.

If precision is not important (you are only interested
in a rough approximation) float is great.
 
J

jmcgill

jacob said:
double means DOUBLE PRECISION. Note the word precision here.

When you want more precision, use double precision, and if that
doesn't cut it use long double.

Precision means the number of significant digits you get
for the calculations. float gives you 6 digits precision,
(assuming IEEE 754) double gives you 15, and long double
more than that, using Intel/Amd implementation gives you 18.


I'm not up to date on the ISO specs, but I don't remember any
requirements like the ones you mention; only that long doubles be at
least as long as doubles, which themselves must be at least as long as
floats, and that there is a minimum range of -10^37 through 10^37.

It may be reasonable to assume IEEE754 on some (very common) platforms,
but is it strictly compliant to do so?
 
J

jacob navia

jmcgill said:
I'm not up to date on the ISO specs, but I don't remember any
requirements like the ones you mention; only that long doubles be at
least as long as doubles, which themselves must be at least as long as
floats, and that there is a minimum range of -10^37 through 10^37.

It may be reasonable to assume IEEE754 on some (very common) platforms,
but is it strictly compliant to do so?

Do not confuse C99 standard (ISO) and IEEE 754 (floating point)
 
J

jmcgill

jacob said:
Do not confuse C99 standard (ISO) and IEEE 754 (floating point)

I wouldn't, or at least, I would be very explicit about my intentions if
doing so. But I've seen comments on this thread today that kind of
scare me. (By "kind of", I mean, I don't really give a damn, since none
of the posters work for me or are my students ;-)
 
P

pete

Erick- said:
hi all...

I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best?
when should we use float or double??

Use float, when you want the smallest floating point type.
In C89, use long double, when you want the floating point type
with the greatest range.
Use double, all the rest of the time.
 
G

Gordon Burditt

I've readed some lines about the difference between float and double
data types... but, in the real world, which is the best? when should we
use float or double??

I've read about the difference between rat poison, condoms,
and sunscreen. In the real world, which is the best?

It depends on what you're trying to do.

Typical floats have less than 7 digits of precision (and they aren't
guaranteed to have more than 6). If you're planning on doing
accounting with amounts up to 10 million dollars and expect the
total of a bunch of numbers to be accurate to a penny for the
accountants, don't use floats. (Also, use integer quantities of
cents whether or not you use a variable type of int or float or
double or long double). Use doubles, long doubles, or long longs.

If you are attempting to store large quantities of shoe sizes, which
for normal people don't need much in precision but keeping them all
in memory is a requirement, use float (or perhaps use short with
the size multiplied by 10).

Performance issues are muddled. It is not guaranteed that the
performance of float is worse than or better than or about the same
as the performance of doubles. Yep, on some systems the performance
of floats is worse (convert to double, do operation, convert back),
on some it's better (less bits to worry about), and on some it's
the same, and on some it's worse, better, or the same depending on
what you are trying to do.
 
K

Keith Thompson

I think the name "double" probably comes from Fortran, where "DOUBLE
PRECISION" is exactly twice the size of "FLOAT". (I'm not much of a
Fortran person, so I could easily be mistaken.)
I'm not up to date on the ISO specs, but I don't remember any
requirements like the ones you mention; only that long doubles be at
least as long as doubles, which themselves must be at least as long as
floats, and that there is a minimum range of -10^37 through 10^37.

It may be reasonable to assume IEEE754 on some (very common) platforms,
but is it strictly compliant to do so?

IEEE 754 is certainly the most common set of floating-point formats,
but it's not required by the C standard; C implementations exist on a
number of platforms with other FP formats (IBM, Cray, VAX, etc.).

The C standard requires:
FLT_DIG >= 6
DBL_DIG >= 10
LDBL_DIG >= 10

FLT_MAX >= 1E+37
DBL_MAX >= 1E+37
LDBL_MAX >= 1E+37

Obviously IEEE 754 exceeds these minimal requirements.

Note that double and long double, to be conforming, need at least 40
bits (if I've done the math correctly); float could be as few as 27 or
so. An implementation with 32-bit float and 64-bit double and long
double could easily be conforming. (There's been some confusion about
this with some compilers not supporting long double, when they could
easily have just given it the same characteristics as double.)

But it's not at all obvious that float is going to be *faster* than
double. The common wisdom, as I understand it, is to use double
rather than float *unless* you really need to save storage space, but
the tradeoffs could vary on different systems.
 
D

Dik T. Winter

> When you want more precision, use double precision, and if that
> doesn't cut it use long double.

When precision is a problem, it is much better to analyse *why* it is a
problem. That is much better than going to double, and if that does not
cut it, going to long double.
 
A

Ark

jmcgill said:
I wouldn't, or at least, I would be very explicit about my intentions if
doing so. But I've seen comments on this thread today that kind of
scare me. (By "kind of", I mean, I don't really give a damn, since none
of the posters work for me or are my students ;-)
Check, however, the normative Annex F. If your compiler defines
__STDC_IEC_559__, it should be pretty close.
 
K

Keith Thompson

pete said:
Use float, when you want the smallest floating point type.
In C89, use long double, when you want the floating point type
with the greatest range.
Use double, all the rest of the time.

Why the "In C89" qualification? long double is the largest
floating-point type in both C89/C90 and C99; hardly anyone should have
to worry about pre-C89 implementations these days.
 
J

Joe Wright

jacob said:
double means DOUBLE PRECISION. Note the word precision here.

When you want more precision, use double precision, and if that
doesn't cut it use long double.

Precision means the number of significant digits you get
for the calculations. float gives you 6 digits precision,
(assuming IEEE 754) double gives you 15, and long double
more than that, using Intel/Amd implementation gives you 18.

If precision is not important (you are only interested
in a rough approximation) float is great.

Hi Jake, Joe here. Comment ca va? My machines are x86 from Intel and AMD
and SPARC from Sun. All claim IEEE 754 floating point.

The precision of a floating point type is binary and tied to the width
of the mantissa. On my machines, the mantissa of a 32-bit float is 24
bits wide and the mantissa of a 64-bit double is 53 bits wide.

The 24-bit mantissa of the float demands 8 decimal digits for its
representation. The 53-bit double mantissa demands 16 decimal digits.

I had occasion, some time ago, to express float and double as text and
then from text back to float and double. Exactly.

Given a double, text is..

char buf[30]; /* more than enough */

sprintf(buf, "%.16e", dbl);

For a float..

sprintf(buf, "%.8e", flt);

Now use of atof() or strtod() will take the text back to floating point.
Exactly.

I went through all this to avoid the tragedy of importing binary files
from disparate systems. Endianess is also an issue that disappears when
you and your recipient agree that the file format is 'text' instead of
'binary'.
 
M

Malcolm

Keith Thompson said:
But it's not at all obvious that float is going to be *faster* than
double. The common wisdom, as I understand it, is to use double
rather than float *unless* you really need to save storage space, but
the tradeoffs could vary on different systems.
By a great deal. The Playstation 2 came with a compiler that supported
double, but in software emulation. The single precision floating point unit
was, on the other hand, extremely fast, at least for its day.
 
M

Malcolm

Dik T. Winter said:
When precision is a problem, it is much better to analyse *why* it is a
problem. That is much better than going to double, and if that does not
cut it, going to long double.
There's a lot of truth there.
My first ever 3D cube rotated a cube quite nicely for a second, then pulled
it apart as errors accumlated. I immediately fixed the problem by going to
double.

Had double not been available, I would have realised that there is no need
to make incremental changes in the xyz coordinates. Simply increment the
rotation. I would have had a much better program for it.
 
P

pete

Keith said:
Why the "In C89" qualification? long double is the largest
floating-point type in both C89/C90 and C99;

I was thinking of long long int, and got confused.
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,208
Latest member
RandallLay

Latest Threads

Top