Thread: "nan" output error

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    "nan" output error

    Hi mates, I'm having problems with a "nan" output
    Look the code below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float raiz(float n, float *p);
    
    int main()
    {
        float n, r = 1, *p;
        p = &r;
    
        do
        {
    
            printf("Entre com o número cuja raiz será calculada\n");
            scanf("%f", &n);
    
            if(n < 0)
                printf("Não há raiz quadrada real para números negativos\n");
        }while(n < 0);
    
        printf("O valor da raiz quadrada de %.2f é %.2f\n", n, raiz(n, p));
    
        return 0;
    }
    
    float raiz(float n, float *p)
    {
        if(abs(*p * *p - n) < 0.001){
    	printf("%.2f", *p);
            return (*p);
        }
        else{
            *p = (*p * *p + n)/(2 * *p);
            raiz(n, p);
        }
    }
    I think I'm not doing well the return of the function "raiz", because the value is well shown inside the function...

    Thanks in advance.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I haven't really looked at the algorithm much but one thing I noticed is that you're not returning a value from the recursive call.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    One more thing: abs returns an int, so the condition will only be true if it returns 0, obviously.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Thank you very much mate.

    I've fixed the code and now works fine:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int raiz(float n, float *p);
    
    int main()
    {
        float n, r = 1, *p;
        p = &r;
    
        do
        {
    
            printf("Entre com o número cuja raiz será calculada\n");
            scanf("%f", &n);
    
            if(n < 0)
                printf("Não há raiz quadrada real para números negativos\n");
        }while(n < 0);
    
    	raiz(n, p);
    
        printf("O valor da raiz quadrada de %.2f é %.2f\n", n, r);
    
        return 0;
    }
    
    int raiz(float n, float *p)
    {
        if(abs(*p * *p - n) < 0.001){
            return 0;
        }
        else{
            *p = (*p * *p + n)/(2 * *p);
            raiz(n, p);
        }
    }

    hey, the problem with the previous code was the abs function? Or I did bad the returning?

    Thanks.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    NaN(not a number) error generally occurs if the ouput is indeterminate i.e whose value couldn't be determined such as 0/0.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Probably should use fabs().

    Your second post of code looks the same as the first.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    There is no need to take the absolute value. This is Newton's algorithm. The approximations will converge monotonically to the true value of the square root. So if the initial guess was less than the true root (which will be the case for any input greater than 1, since you start with an initial approximation of 1), then the subsequent approximations will also be less than the true root.

    Replace:

    Code:
    fabs( *p * *p - n )
    With:

    Code:
    n - *p * *p
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM