sqrtf gives incorrect output

I am developing for a SH4 processor. When using the library call sqrtf(), i get incorrect output, sometimes even negative values. sqrt() behaves correctly though. This problem only occurs for sqrtf() - sin() and sinf(), cos and cosf(), exp and expf() all return correct values. Doen anyone know if there is an error in the math library for the SH4? Is there a patch?

I am not aware of a problem with the sqrtf() function.

Does it only occur on the sh4 version?
How are you determining it is broken? (attach the test program if you have one).
What version of QNX are you using? (I know it is QNX6, but which version - ‘uname -a’)


I think the problem is in the math library for the SH4. I have not tried any other platforms - I only have the SH4 to test on. (I’m too lazy to set up a PC running Neutrino)

I tried a few explicit casts for the arguments to sqrt() and sqtrf(). This is the code that I used:
//floating point test
printf (“sqrt() test:\n”);
float ftmp = 2.0f;
float fans = 1.0f;
printf (“x = %1.2f; “, ftmp);
fans = (float)sqrt((double)ftmp);
printf (”(float)sqrt((double)x) = %1.2f; x’ = %1.2f\n”, fans, ftmp);

ftmp = 2.0f;
printf ("x = %1.2f; ", ftmp);
fans = sqrt((double)ftmp);
printf ("sqrt((double)x) = %1.2f; x' = %1.2f\n", fans, ftmp);

ftmp = 2.0f;
printf ("x = %1.2f; ", ftmp);
fans = sqrtf(ftmp);
printf ("sqrtf(x) = %1.2f; x' = %1.2f\n", fans, ftmp);

ftmp = 2.0f;
printf ("x = %1.2f; ", ftmp);
fans = (float)sqrtf((double)ftmp);
printf ("(float)sqrtf((double)x) = %1.2f; x' = %1.2f\n", fans, ftmp);

ftmp = 2.0f;
double dtmp = ftmp;
double dans = sqrt(dtmp);
fans = (float) dans;
printf ("fx = %1.2f, dx = %1.2f, sqrt (dx) = %1.2f. float(ans) = %1.2f\n\n", ftmp, dtmp, dans, fans);

printf ("\nsqrt(2) = %1.2f or %1.2f or %1.2f or %1.2f\n", sqrt(2.0), sqrtf(2.0), sqrtf(2.0f), sqrt(2.0f));
printf ("\nexp(2) = %1.2f or %1.2f or %1.2f or %1.2f\n", exp(2.0), expf(2.0), expf(2.0f), exp(2.0f));
printf ("\nsin(2) = %1.2f or %1.2f or %1.2f or %1.2f\n", sin(2.0), sinf(2.0), sinf(2.0f), sin(2.0f));
printf ("\ncos(2) = %1.2f or %1.2f or %1.2f or %1.2f\n", cos(2.0), cosf(2.0), cosf(2.0f), cos(2.0f));

The output of the above is:
sqrt() test:
x = 2.00; (float)sqrt((double)x) = 1.41; x’ = 2.00
x = 2.00; sqrt((double)x) = 1.41; x’ = 2.00
x = 2.00; sqrtf(x) = 0.39; x’ = 2.00
x = 2.00; (float)sqrtf((double)x) = 0.39; x’ = 2.00
fx = 2.00, dx = 2.00, sqrt (dx) = 1.41. float(ans) = 1.41

sqrt(2) = 1.41 or 0.39 or 0.39 or 0.39
exp(2) = 7.39 or 7.39 or 7.39 or 7.39
sin(2) = 0.91 or 0.91 or 0.91 or 0.91
cos(2) = -0.42 or -0.42 or -0.42 or -0.42

As you can see, sqrt always gives the wrong output if you use float arguments (Regardless of whether you’re calling sqrt or sqrtf). And all the other math functions behave correctly. It’s easy to work around this. I just explicitly cast the arguments to double, and cast the answer back to float again. I just wondered if someone was aware of this problem.

QNX version : QNX localhost 6.2.1 2003/05/17-11:35:34edt SOLUTION_ENGINE shle

Looks like your best bet is to report it through the QSS newsgroups and see if they have a patch for it, or at least can confirm it. Use the support newsgroup qdn.private.standard (or whichever) and you may get a faster response.