Tests/Tutorial: Fix when USE_MYMATH is OFF

Unit tests for the square root of "-25" currently fail when USE_MYMATH
is disabled. The "mysqrt" method in the tutorials, returns "0" for a
negative value, while "sqrt" returns "NaN", and therefore the output is
not accepted by the test.

This patch checks if the number is negative and eventually returns "0"
before calling "sqrt" or "mysqrt" to fix this issue.

Printing a NaN might cause issues with the string catched by the tests
on some platform. Therefore assume that "0" is correct and "fix" the
USE_MYMATH=OFF version by checking if the number is negative and
eventually returning "0" before calling "sqrt" or "mysqrt".
This commit is contained in:
Daniele E. Domenichelli 2014-10-30 12:23:27 +01:00 committed by Brad King
parent 5c5c1e3c7d
commit 36cf8a1eb9
6 changed files with 36 additions and 12 deletions

View File

@ -21,12 +21,16 @@ int main (int argc, char *argv[])
}
double inputValue = atof(argv[1]);
double outputValue = 0;
if(inputValue >= 0)
{
#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
outputValue = mysqrt(inputValue);
#else
double outputValue = sqrt(inputValue);
outputValue = sqrt(inputValue);
#endif
}
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);

View File

@ -21,12 +21,16 @@ int main (int argc, char *argv[])
}
double inputValue = atof(argv[1]);
double outputValue = 0;
if(inputValue >= 0)
{
#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
outputValue = mysqrt(inputValue);
#else
double outputValue = sqrt(inputValue);
outputValue = sqrt(inputValue);
#endif
}
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);

View File

@ -21,12 +21,16 @@ int main (int argc, char *argv[])
}
double inputValue = atof(argv[1]);
double outputValue = 0;
if(inputValue >= 0)
{
#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
outputValue = mysqrt(inputValue);
#else
double outputValue = sqrt(inputValue);
outputValue = sqrt(inputValue);
#endif
}
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);

View File

@ -21,12 +21,16 @@ int main (int argc, char *argv[])
}
double inputValue = atof(argv[1]);
double outputValue = 0;
if(inputValue >= 0)
{
#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
outputValue = mysqrt(inputValue);
#else
double outputValue = sqrt(inputValue);
outputValue = sqrt(inputValue);
#endif
}
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);

View File

@ -21,12 +21,16 @@ int main (int argc, char *argv[])
}
double inputValue = atof(argv[1]);
double outputValue = 0;
if(inputValue >= 0)
{
#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
outputValue = mysqrt(inputValue);
#else
double outputValue = sqrt(inputValue);
outputValue = sqrt(inputValue);
#endif
}
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);

View File

@ -21,12 +21,16 @@ int main (int argc, char *argv[])
}
double inputValue = atof(argv[1]);
double outputValue = 0;
if(inputValue >= 0)
{
#ifdef USE_MYMATH
double outputValue = mysqrt(inputValue);
outputValue = mysqrt(inputValue);
#else
double outputValue = sqrt(inputValue);
outputValue = sqrt(inputValue);
#endif
}
fprintf(stdout,"The square root of %g is %g\n",
inputValue, outputValue);