Radical in calculator !!!

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

crashtech

Lifer
Jan 4, 2013
10,695
2,294
146
For some reason I never considered the error inherent in calculating the root of a perfect square. I know there is no real point, but to anthropomorphize the process would necessitate a "cheat sheet" with some of the smaller perfect squares, like most humans carry around in their heads.
 

Schmide

Diamond Member
Mar 7, 2002
5,747
1,039
126
sqrt is a function call. The compiler cannot optimize it out even if the input to the sqrt function is always the same.

Most replace it with inlined constant code.

Go here

http://gcc.godbolt.org/

Put in this

Code:
#include <math.h>

void dummy(double* dDummy, int iCount)
{

double x = *dDummy;

for(int i = 0; i < iCount; i++)
	x += sqrt(4) - (2.0f + 1e-6f);
  
  *dDummy=x;
}

I added a small constant as ICC figured the constant as zero and removed all the code.

All gcc 4+ will optimize sqrt out immediately icc and 3 will do it with basic optimization flags -O2 or greater. Microsoft will do it as well for most release builds that don't explicitly tell it not to optimize.

Edit: Better code version to prevent removal of code.
 
Last edited: