Jump to content

C++ square problems

Featured Replies

The part

for(int i=1; i<n; i++)
{
x=sqrt(y*i);
cout<<"x"<<i<<": "<<x<<endl;
}

 

isn't working wll, for some elements the result is non, i thought that i because the numbers are complex, but not with include complex i get the same resut.. can anyone help me

#include<iostream>
#include<math.h>
#include <complex>
using namespace std;

int main()
{
    int n,elem[20];
    float y[20],A[20];
    complex<float>  x[20];
    
    cout<<"Number of elements?"<<endl;
    cin>>n;
    cout<<"Insert elements:"<<endl;
    for(int i=0; i<n; i++)
    {
        cin>>elem[i];
        
    }
    
    
    A[0]=pow(elem[0],2);
    
    for(int j=1; j<n; j++)
    {
        A[j]=pow(elem[j],2);
    
            int i=1;
            while((i+j)<=n-1&&(i<=j))
            {
                
                A[j]=A[j]+pow((-1),i)*2*(elem[j-i]*elem[j+i]);
                i=i+1;
            
            }
    

            cout<<"A"<<j<<":"<<A[j]<<endl;
    
    
    }
    
    for(int i=1; i<n; i++)
    {
        y[i]=A[i]/-A[i-1];
        cout<<"y"<<i<<": "<<y[i]<<endl;
    }
    
    for(int i=1; i<n; i++)
    {
        x[i]=sqrt(y[i]*i);
        cout<<"x"<<i<<": "<<x[i]<<endl;
    }

}

sqrt() the most likely is returning NaN (Not A Number) for negative input.

The same happens with regular hand calculators (E letter appears etc.)

 

http://en.wikipedia.org/wiki/NaN


It's mentioned on wiki page

"There are three kinds of operations that can return NaN:

[...]

The square root of a negative number."

Edited by Sensei

  • Author

sqrt() the most likely is returning NaN (Not A Number) for negative input.

The same happens with regular hand calculators (E letter appears etc.)

 

http://en.wikipedia.org/wiki/NaN

It's mentioned on wiki page

"There are three kinds of operations that can return NaN:

[...]

The square root of a negative number."

 

 

and how can i change that? that i doesn't give nan ?

Take sqrt() only from positive numbers.. So use fabs()/abs() or equivalent code, to get rid of negatives...


BTW, y[] is defined as float, so complex class sqrt() is never executed, only regular one.

Edited by Sensei

  • Author

Take sqrt() only from positive numbers.. So use fabs()/abs() or equivalent code, to get rid of negatives...

BTW, y[] is defined as float, so complex class sqrt() is never executed, only regular one.

thanks for you help... i figured it out.. now its all working :)

You are in one place dividing by variable.

If it'll be 0, you will end up with error as well.

You have to check whether it's equal 0.0 to prevent that.


If sqrt() from negative is really needed you can try using following function instead:

 

double negative_sqrt( double value )

{

if( value >= 0 ) return( sqrt( value ) );

return( -sqrt( -value ) );

}

 

It will make sure that you're not taking square from negative, and preserving sign of value.

 

f.e.

negative_sqrt( -100 ) will return -10

 

But it's not general purpose equivalent.

It depends on context in which you might need sqrt().

-10 * -10 = +100 after all.

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.