Re: [vox-tech] float vs double, int vs short
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [vox-tech] float vs double, int vs short
I think that on X86, both float and double get expanded to long double, then all
the computations locally (in CPU) get done with long double) and the results are
then converted to whatever they were intended to be to begin with. See the
attached example code for finding machine epsilon (plain c, just do
gcc LUGODtest.cpp
and run ./a.out
On other architectures (tried on SUN) there is actually a difference (as it
should be).
So to conclude, I do not think that you gain much (if anything) on CPU with
floats versus doubles. There might be a gain in moving data from and to memory
and similar, but computationally, probably the best is to run a million loops on
an example (maybe even the one for machine epsilon attached below) and see
where it goes...
Boris
Ken Bloom wrote:
On Thu, Feb 16, 2006 at 11:21:11AM -0500, Peter Jay Salzman wrote:
I've been googling for the past 15 minutes and have read that:
* computations with doubles are always faster than floats
* computations with doubles are often faster than floats
* computations with floats are often faster than doubles
* computations with floats are always faster than doubles
The arguments I've read for all four cases seem reasonable. I trust
the collective brains of lugod more than any internet forum. What's
the real scoop on the relative speed of basic arithmetic operations
on floats and doubles?
Write a simple benchmark and tell us. A program that runs the desired
operations in a loop a few million times should give you a good idea
which is faster.
Likewise for shorts and ints.
--Ken
------------------------------------------------------------------------
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float f_macheps();
double d_macheps();
long double ld_macheps();
//##############################################################################
float f_macheps()
{
float eps = 1.0;
int counter = 1;
float one = 1.0;
while ( (one + eps) > one )
{
eps = eps/2.0;
counter++;
}
eps = eps*2.0;
counter--;
return eps;
}
//##############################################################################
double d_macheps()
{
double eps = 1.0;
int counter = 1;
while ( ((double) 1.0 + eps) > ((double) 1.0) )
{
eps = eps/2.0;
counter++;
}
eps = eps*2.0;
counter--;
return eps;
}
//##############################################################################
long double ld_macheps()
{
long double eps = 1.0;
int counter = 1;
while ( ((long double) 1.0 + eps) > ((long double) 1.0) )
{
eps = eps/2.0;
counter++;
}
eps = eps*2.0;
counter--;
return eps;
}
int main(void)
{
float float_eps = f_macheps(); // or use float.h values FLT_EPSILON
double double_eps = d_macheps(); // or use float.h values DBL_EPSILON
long double long_double_eps = ld_macheps(); // or use float.h values LDBL_EPSILON
::printf("\n float macheps = %.20e \n",float_eps);
::printf(" double macheps = %.20e \n",double_eps);
::printf(" long double macheps = %.20Le \n\n\n",long_double_eps);
exit(1);
}
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech
|