Why is return from my function is nan?

Discussion in 'C++' started by utab, Jan 29, 2007.

  1. utab

    utab Guest

    Dear all, I posted this yesterday since it was a sunday, many people
    missed it I guess and I could not find the answer.

    I tried sth easy(actually this was an exercise) but I tried to use the
    standard lib. heavily for this problem(as far as I can). There was one
    point I could not figure out. The problem is :

    ../a.out 1.3+3.2+.1+40/3*8/7-4*5-32

    The program will parse the argument and find the result of the above
    expression. I have two versions(the 2nd is working , not perfect ;-}),
    but the first have a return problem I guess because if I use that
    version I get "nan" as the result at line 37. What is my problem?

    Sorry that there are some comments and cout statement in around the
    code. But I guess it could be easier to figure out.

    You can find the two versions of the code here:

    http://groups.google.be/group/comp.lang.c++/browse_thread/thread/
    7ac73062c56a73a2/074abc747f2964e5?hl=en#074abc747f2964e5

    One gentle poster commented on that UNIX may be expanding '*' to
    wildcards, I tried that it is not the problem. I quoted and supplied
    the expression. Probably I am missing sth quite easy.
     
    utab, Jan 29, 2007
    #1
    1. Advertisements

  2. utab

    Ondra Holub Guest

    utab napsal:
    > Dear all, I posted this yesterday since it was a sunday, many people
    > missed it I guess and I could not find the answer.
    >
    > I tried sth easy(actually this was an exercise) but I tried to use the
    > standard lib. heavily for this problem(as far as I can). There was one
    > point I could not figure out. The problem is :
    >
    > ./a.out 1.3+3.2+.1+40/3*8/7-4*5-32
    >
    > The program will parse the argument and find the result of the above
    > expression. I have two versions(the 2nd is working , not perfect ;-}),
    > but the first have a return problem I guess because if I use that
    > version I get "nan" as the result at line 37. What is my problem?
    >
    > Sorry that there are some comments and cout statement in around the
    > code. But I guess it could be easier to figure out.
    >
    > You can find the two versions of the code here:
    >
    > http://groups.google.be/group/comp.lang.c++/browse_thread/thread/
    > 7ac73062c56a73a2/074abc747f2964e5?hl=en#074abc747f2964e5
    >
    > One gentle poster commented on that UNIX may be expanding '*' to
    > wildcards, I tried that it is not the problem. I quoted and supplied
    > the expression. Probably I am missing sth quite easy.


    You should place here your code (minimized as possible, but still
    showing the problem). Few people will try to resolve not-working link
    (at least for me the presented link is broken).
     
    Ondra Holub, Jan 29, 2007
    #2
    1. Advertisements

  3. utab

    utab Guest

    utab, Jan 29, 2007
    #3
  4. "utab" <> wrote in message
    news:...
    > Dear all, I posted this yesterday since it was a sunday, many people
    > missed it I guess and I could not find the answer.
    >
    > I tried sth easy(actually this was an exercise) but I tried to use the
    > standard lib. heavily for this problem(as far as I can). There was one
    > point I could not figure out. The problem is :
    >
    > ./a.out 1.3+3.2+.1+40/3*8/7-4*5-32
    >
    > The program will parse the argument and find the result of the above
    > expression. I have two versions(the 2nd is working , not perfect ;-}),
    > but the first have a return problem I guess because if I use that
    > version I get "nan" as the result at line 37. What is my problem?
    >
    > Sorry that there are some comments and cout statement in around the
    > code. But I guess it could be easier to figure out.
    >
    > You can find the two versions of the code here:
    >
    > http://groups.google.be/group/comp.lang.c++/browse_thread/thread/
    > 7ac73062c56a73a2/074abc747f2964e5?hl=en#074abc747f2964e5
    >
    > One gentle poster commented on that UNIX may be expanding '*' to
    > wildcards, I tried that it is not the problem. I quoted and supplied
    > the expression. Probably I am missing sth quite easy.
    >


    Compiling the code gives this warning:

    ": warning: 'performer' : not all control paths return a value"

    That should give you a hint as to what is wrong with the code.

    Regards,
    Sami
     
    Sami Vaaraniemi, Jan 29, 2007
    #4
  5. utab

    utab Guest

    ": warning: 'performer' : not all control paths return a value"
    >
    > That should give you a hint as to what is wrong with the code.



    But if I add a "cout << vec_d[0] << endl;" before return under "else":

    I can see the output is right and there. if it is there why is it not
    returned correctly?

    My best
     
    utab, Jan 29, 2007
    #5
  6. utab

    Ondra Holub Guest

    utab napsal:
    > ": warning: 'performer' : not all control paths return a value"
    > >
    > > That should give you a hint as to what is wrong with the code.

    >
    >
    > But if I add a "cout << vec_d[0] << endl;" before return under "else":
    >
    > I can see the output is right and there. if it is there why is it not
    > returned correctly?
    >
    > My best


    Look on my answer in your first thread. You have there something like:

    if (condition)
    {
    // here is missing return
    }
    else
    {
    return result;
    }

    So although result is valid, it is not returned and return value is
    undefined.
     
    Ondra Holub, Jan 29, 2007
    #6
  7. utab

    utab Guest


    > if (condition)
    > {
    > // here is missing return}else
    > {
    > return result;
    >
    > }So although result is valid, it is not returned and return value is
    > undefined.


    Since the function is recursive, on the next run it does not return a
    value, now I got that. It was simple.

    Best regards
     
    utab, Jan 29, 2007
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    717
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,369
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,837
    Smokey Grindel
    Dec 2, 2006
  4. Greenhorn
    Replies:
    15
    Views:
    1,134
    Keith Thompson
    Mar 6, 2005
  5. max(NaN,0) should be NaN

    , Aug 28, 2006, in forum: C Programming
    Replies:
    61
    Views:
    2,033
    Michel Hack
    Sep 8, 2006
  6. utab
    Replies:
    2
    Views:
    433
    Ondra Holub
    Jan 29, 2007
  7. Replies:
    6
    Views:
    2,098
    Richard Tobin
    Mar 19, 2009
  8. Josselin

    NaN return

    Josselin, Mar 31, 2007, in forum: Ruby
    Replies:
    5
    Views:
    241
    Josselin
    Apr 10, 2007
Loading...