🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.sys.palmtops.pilot
4 messages
4 total messages Started by Luc M. Tue, 21 Oct 2003 02:27
Mathlib functions always returning 0
#198
Author: Luc M.
Date: Tue, 21 Oct 2003 02:27
28 lines
2198 bytes
I get '0' from a mathlib functions when I try to pass a parameter.
The functions return the proper value if the parameter is checked before
calling the function.

Assuming 'index' is equal to 2, why does the function 'sqrt(index)' return 0
in the following code:

   Int32 index;
   double c;

   index = StrAToI(FldGetTextPtr(fldInput));     /* field set so that index
is 2 */
   c = sqrt(index);                                              /* Returns
0 ? */


But it returns the proper value in the following code:

    index = StrAToI(FldGetTextPtr(fldInput));   /* field set so that index
is 2 */
    if(index == 2)
    {
      c = sqrt(index);                                          /* Returns
proper value !!! */
    }


Any help is greatly appreciated.
Re: Mathlib functions always returning 0
#199
Author: Logan Shaw
Date: Tue, 21 Oct 2003 03:19
43 lines
3131 bytes
Luc M. wrote:

> I get '0' from a mathlib functions when I try to pass a parameter.
> The functions return the proper value if the parameter is checked before
> calling the function.
> 
> Assuming 'index' is equal to 2, why does the function 'sqrt(index)' return 0
> in the following code:
> 
>    Int32 index;
>    double c;
> 
>    index = StrAToI(FldGetTextPtr(fldInput));     /* field set so that index
> is 2 */
>    c = sqrt(index);                                              /* Returns
> 0 ? */
> 
> 
> But it returns the proper value in the following code:
> 
>     index = StrAToI(FldGetTextPtr(fldInput));   /* field set so that index
> is 2 */
>     if(index == 2)
>     {
>       c = sqrt(index);                                          /* Returns
> proper value !!! */
>     }

I don't know why, but my advice is to disassemble the compiler's
output.  Something funny is going on.  Maybe one piece of code
ends up with index as a register variable and the other doesn't
or something along those lines.

Obviously, you also want to be sure that you have included some
.h file with a prototype for sqrt(), because if you haven't, it
will be passing in values as if they're ints, in which case I think
the behavior is undefined.

Also, it might help to go through your code with a debugger and be
sure that the value of index really is 2 just before the call to
sqrt(), because maybe something unexpected is happening before then.

   - Logan
Re: Mathlib functions always returning 0
#200
Author: Arthur Hagen
Date: Tue, 21 Oct 2003 03:26
18 lines
2053 bytes
"Luc M." <luc_martineau@hotmail.com> wrote in message
news:7Q0lb.2265$aw5.156000@news20.bellglobal.com...
> I get '0' from a mathlib functions when I try to pass a parameter.
> The functions return the proper value if the parameter is checked before
> calling the function.

Silent typecasting is often unreliable, especially with compiler
optimisations, and is bad programming too.

Try c=sqrt((double)index); /* if sqrt expects a double in your case */

Also try compiling with lower levels of optimisation and with warnings
enabled.

Regards,
-- 
*Art
Re: Mathlib functions always returning 0
#197
Author: Luc
Date: Sun, 09 Nov 2003 03:45
40 lines
2533 bytes
This turned out to be a compilation problem.  I used PRC-Tools 2.0 and
recompiling the same code with CW 9.2 fixed the problem.

Luc

"Luc M." <luc_martineau@hotmail.com> wrote in message
news:7Q0lb.2265$aw5.156000@news20.bellglobal.com...
> I get '0' from a mathlib functions when I try to pass a parameter.
> The functions return the proper value if the parameter is checked before
> calling the function.
>
> Assuming 'index' is equal to 2, why does the function 'sqrt(index)' return
0
> in the following code:
>
>    Int32 index;
>    double c;
>
>    index = StrAToI(FldGetTextPtr(fldInput));     /* field set so that
index
> is 2 */
>    c = sqrt(index);                                              /*
Returns
> 0 ? */
>
>
> But it returns the proper value in the following code:
>
>     index = StrAToI(FldGetTextPtr(fldInput));   /* field set so that index
> is 2 */
>     if(index == 2)
>     {
>       c = sqrt(index);                                          /* Returns
> proper value !!! */
>     }
>
>
> Any help is greatly appreciated.
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads