l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
September 2: Social gathering
Next Installfest:
TBD
Latest News:
Aug. 18: Discounts to "Velocity" in NY; come to tonight's "Photography" talk
Page last updated:
2006 Feb 16 17:47

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
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


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
O'Reilly and Associates
For numerous book donations.