• AVR Freaks

Hot!Wrong value on math function?

Author
elberto
Super Member
  • Total Posts : 510
  • Reward points : 0
  • Joined: 2005/05/18 06:35:17
  • Status: offline
2020/07/12 01:23:17 (permalink)
0

Wrong value on math function?

Hi to all users!
Im' using XC16 compiler (v1.40), taking a look to CX16 libraries:
 
double ret, x;


ret = sin(-1.0); // this gives me ret=-0.841, correct

x = -1.0;

ret = sin(ret); // this gives me ret = -1.0, wrong

I get wrong values using simulator and sprintf too, have I missed something?
How can I pass to sin function variable?
#1

9 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Wrong value on math function? 2020/07/12 01:50:17 (permalink)
    +1 (1)
    Did you #include math.h?
     
    It is always best to provide a full real program that other people can test, then we don't have to ask these sort of questions.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    pcbbc
    Super Member
    • Total Posts : 1703
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Wrong value on math function? 2020/07/12 02:11:49 (permalink)
    +1 (1)
    What’s x for?
    #3
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Wrong value on math function? 2020/07/12 02:23:17 (permalink)
    +1 (1)
    Good point.
    I would expect "ret" to contain -0.7456 after that code
    but of course, this isn't the real code the OP is testing with,
    and we are just told that "-1.0" is wrong, without ever being told what value was expected.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    du00000001
    Just Some Member
    • Total Posts : 3851
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Wrong value on math function? 2020/07/12 02:55:28 (permalink)
    -2 (2)
    Might be sin() is defined for single precision. (Check this!)
     
    The "-1.0" constant might well default to single precision - the output of sin() converted to double precision where the receiving variable is double.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #5
    erikS
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2009/03/08 10:11:25
    • Location: Germany
    • Status: offline
    Re: Wrong value on math function? 2020/07/12 04:17:20 (permalink)
    0

    Your sin function is calculating the radian measure (rad).

    -0.841 is absolutely correct.#

    The second part I don't understand. Should be -0,7456...

    #6
    1and0
    Access is Denied
    • Total Posts : 10999
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Wrong value on math function? 2020/07/12 08:06:28 (permalink)
    +2 (2)
    I bet OP's intention is this:

    x = -1.0;
    ret = sin(x); // this gives me ret = -1.0, wrong

     
    As said, we need to see the REAL code.
    #7
    pcbbc
    Super Member
    • Total Posts : 1703
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: Wrong value on math function? 2020/07/12 10:29:20 (permalink)
    +2 (2)
    Real code and how they are determining it is “wrong”.
    For example if the OP is looking in the debugger, but not actually using the result, then it is very likely the compiler will simply throw the code away.
    #8
    NorthGuy
    Super Member
    • Total Posts : 6228
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Wrong value on math function? 2020/07/12 12:22:47 (permalink)
    0
    1and0
    I bet OP's intention is this:

    x = -1.0;
     
    ret = sin(x); // this gives me ret = -1.0, wrong

     
    As said, we need to see the REAL code.



    I bet the next line prints x instead of ret :)
    #9
    elberto
    Super Member
    • Total Posts : 510
    • Reward points : 0
    • Joined: 2005/05/18 06:35:17
    • Status: offline
    Re: Wrong value on math function? 2020/07/12 12:49:35 (permalink)
    +1 (1)
    Sorry guys, this morning I was extremely late, correct code was ret = sin(x).....
    I followed code from DS50001456J just to test sin function.
    Obviously included math.h and stdio.h (if not I had error due to undeclared sin):
     
    #include <math.h> /* for sin */
    #include <stdio.h> /* for printf, perror */
    #include <errno.h> /* for errno */
    [...]

    double ret, x;


    int main(void)
    {
       while(1)
    {
       ret = sin(-1.0); // this gives me ret=-0.841, correct
      Nop();
      Nop();
      Nop();
       x = -1.0;
       ret = sin(x); // this gives me ret = -1.0, wrong: ret always the same of x
      Nop();
      Nop();
      Nop();

    // missing perror check

    }
    return 0;
    }

     
    Yesterday I spent one hour trying to understand what's wrong, I removed sprintf and increasing x to get code as simple as possible, I still got wrong value if sin argument not hardcoded.
    I found issues about math functions but discussions were about 2003...
    Today I reloaded code: I don't know if it was about compiler or Mplab, code is correctly working.
    I apologize to waste your time.
    #10
    Jump to:
    © 2020 APG vNext Commercial Version 4.5