Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Mac > Mac Programmer Help > float vs. doubl...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 10 Topic 3779 of 3882
Post > Topic >>

float vs. double

by MacRules <dodeca001@[EMAIL PROTECTED] > Jun 22, 2008 at 09:37 AM

I have inherited some plain old C code written back in '94 that uses
only floats. I did a little empirical testing on my Mac (2 x 2.8 GHz
Quad-Core Intel Xeon) to see whether it would be worthwhile to change
everything to double. I would like the greater accuracy, but don't
want any kind of performance hit.

I'm using Xcode 3.0 with gcc 4.0.

My first experiment was to take a look at the number of information-
bearing digits with float vs. double:

printf("float: %.30f\n",(float)1/9);
printf("double: %.58lf\n",(double)1/9);

Result:
float: 0.111111111938953399658203125000
double: 0.1111111111111111049432054187491303309798240661621093750000

>> Question: I was a bit surprised to see a substantial amount of (what
looks like) garbage in the least significant digits of these values -
should I be?

>> My next experiment was to measure latency. Here's the code:

void ASG_fd_timing_test(void) {
	ASG_TIMER		timer;
	long				num_iter;
	float				a_f,b_f,c_f,d_f,e_f,f_f;
	double			a_d,b_d,c_d,d_d,e_d,f_d;
	int				duration_float;
	int				duration_dble;
	int				n;

	printf("Number of iterations? ");
	scanf("%ld",&num_iter);

// --- start timer ---

	ASG_timing_init(
			&timer);

	ASG_timing_stamp_advance(
			&timer);

// --- do float testing ---

	a_f = 1.6;
	b_f = -0.33;
	for (n=0; n<num_iter; n++) {
		c_f = a_f + b_f;
		d_f = a_f - b_f;
		e_f = a_f * b_f;
		f_f = a_f / b_f;
		a_f += 3.2;
		b_f -= 1.05;

// -- record stop time --

	ASG_timing_stamp_advance(
			&timer);

// --- do double testing ---

	a_d = 1.6;
	b_d = -0.33;
	for (n=0; n<num_iter; n++) {
		c_d = a_d + b_d;
		d_d = a_d - b_d;
		e_d = a_d * b_d;
		f_d = a_d / b_d;
		a_d += 3.2;
		b_d -= 1.05; }

// -- record stop time --

	ASG_timing_stamp_advance(
			&timer);

// -- find float duration --

	ASG_timing_find_delta(
			&timer.timestamp[0],
			&timer.timestamp[1],
			&duration_float);

// -- find double duration --

	ASG_timing_find_delta(
			&timer.timestamp[1],
			&timer.timestamp[2],
			&duration_dble);

	printf("\nDuration float, double = %d %d ms
\n",duration_float,duration_dble);
}

The results:

Number of iterations? 1000000000

Duration float, double = 359 358 ms

>> It appears that using doubles has no performance hit. If I took a look
at the ASM code and knew more about Intel processors, perhaps I would know
why. My tentative conclusion is that I should go ahead and upgrade. Any
comments?

TIA,
Steve
 




 10 Posts in Topic:
float vs. double
MacRules <dodeca001@[E  2008-06-22 09:37:54 
Re: float vs. double
MacRules <dodeca001@[E  2008-06-22 19:55:39 
Re: float vs. double
Gregory Weston <uce@[E  2008-06-22 23:42:28 
Re: float vs. double
Reinder Verlinde <rein  2008-06-23 18:56:28 
Re: float vs. double
MacRules <dodeca001@[E  2008-06-23 05:31:21 
Re: float vs. double
MacRules <dodeca001@[E  2008-06-23 18:32:47 
Re: float vs. double
Korchkidu <korchkidu@[  2008-06-24 07:55:57 
Re: float vs. double
MacRules <dodeca001@[E  2008-06-24 09:39:46 
Re: float vs. double
Korchkidu <korchkidu@[  2008-06-29 08:14:36 
Re: float vs. double
glenn andreas <gandrea  2008-06-29 10:35:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 1:35:29 CST 2008.