Thread: Returning Nan or Inf

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Returning Nan or Inf

    I'm rewriting MatLab code into C and need to send values to a complex error function. The erfcomp function produces Nan and Inf values, but when I try to return these values to my other function and store them under a new variable, they somehow become numbers. My problem is that I need to stop a loop once Nan or Inf is reached.

    Is it possible to return Nan or Inf values to a function or I am I wasting my time by not looking for another approach to this?


    Thanks in advance.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't know how you can directly assign NaN to a variable in a program, but you can use isnan() to tell if a number is a Nan: http://www.opengroup.org/onlinepubs/...ons/isnan.html

    [edit] There's an isinf() as well. http://www.opengroup.org/onlinepubs/...ons/isinf.html [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Not sure how standard they are but my version of gcc has two macros, NAN and INFINITY.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Quote Originally Posted by dwks View Post
    you can use isnan() to tell if a number is a Nan: [/edit]

    I was planning on using isnan, but after I had already returned the value from the function. I was going to check the variable I saved it in using isnan or isinf. Otherwise, they don't do much for me because even if I check the values within the function, I can't break the loop from within the function. :/

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    INFINITY is C99, NAN is not. http://www.gnu.org/software/libtool/...y-and-NaN.html

    [edit] Hmm, strike that. NAN looks like it might be C99 too.
    C99 adds all sorts of stuff:
    ...
    -- macros INFINITY, NAN for special floating-point values
    http://std.dkuug.dk/jtc1/sc22/wg21/d...2002/n1372.txt

    Of course, the GCC page is likely more trustworthy than that source . . . . [/edit]
    Last edited by dwks; 07-01-2008 at 06:05 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with struct... not returning correct results.
    By drty2 in forum C Programming
    Replies: 7
    Last Post: 01-18-2009, 11:25 PM
  2. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  3. double, NaN, and super small numbers
    By nbo10 in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 06:50 PM
  4. a question about nan
    By FOOTOO in forum C Programming
    Replies: 14
    Last Post: 04-26-2007, 01:19 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM