2012-12-22 88 views
3

我正在使用Code :: Blocks 10.05編譯此程序,但通常我會在每次輸出中開始生成Nan之前完成大約10次迭代。我想知道這是否是由於使用cos和sin函數而引起的問題,以及是否有體面的工作來避免這種情況?使用sin和cos函數迭代時的Nan結果

我必須產生很多迭代,因爲我正在爲大學開展一個項目,所以它也必須是準確的。我查閱了一些關於如何避免使用罪惡和罪惡的文章,儘管我需要嚴格遵循幾個公式,否則我產生的結果可能不準確,所以我不確定是否妥協。

struct Particle // Need to define what qualities our particle has 
{ 
    double dPosition; 
    double dAngle; 

}; 

Particle Subject; 

void M1(double &x, double &y) //Defines movement if particle doesn't touch inner boundary 
{ 
    x = x + 2*y; 
} 

double d = 0.25; //This can and will be changed when I need to find a distance between 
       // the two cricles at a later stage 


void M2(double &x,double &y, double d) //Defines movement of a particle if it impacts the inner boundary 
{ 
    double z = asin(-(sin(y)+d*cos(x + y))/0.35); 
    double y1 = y; 
    y = asin(-0.35*sin(z) + d*cos(x + y + 2*z)); 
    x = y + y1 + x + 2*z; 
} 

int main() 
{ 
    cout << "Please tell me where you want this particle to start positions-wise? (Between 0 and 2PI" << endl; 
    cin >> Subject.dPosition; 
    cout << "Please tell me the angle that you would like it to make with the normal? (Between 0 and PI/2)" << endl; 
    cin >> Subject.dAngle; 
    cout << "How far would you like the distances of the two middle circles to be?" << endl; 
    double d; 
    cin >> d; 

    // These two functions are to understand where the experiment begins from. 
    // I may add a function to change where the circle starts however I will use radius = 0.35 throughout 

    cout << "So position is: " << Subject.dPosition << endl; 
    cout << "And angle with the normal is: " << Subject.dAngle <<endl; 

    int n=0; 
    while (n <= 100) //This is used to iterate the process and create an array of Particle data points 
    {    // in order to use this data to build up Poincare diagrams. 

    { 
     while (Subject.dPosition > 2*M_PI) 
      Subject.dPosition = Subject.dPosition - 2*M_PI; 
    } 
    { 
     if (0.35 >= abs(0.35*cos(Subject.dPosition + Subject.dAngle)+sin(Subject.dAngle))) //This is the condition of hitting the inner boundary 
      M2(Subject.dPosition, Subject.dAngle, d); //Inner boundary collision 
     else 
      M1(Subject.dPosition, Subject.dAngle); // Outer boundary collision 
    }; 
    cout << "So position is: " << Subject.dPosition << endl; 
    cout << "And angle with the normal is: " << Subject.dAngle <<endl; 
    n++; 
} 
    return 0; 
} 
+1

我把我的錢放在'asin()'函數成爲問題。 Arcsin沒有爲所有輸入定義。我的意思是它的行爲是正確的,但結果是不受歡迎的。 –

回答

0

如果該值不在[-1,+ 1],並傳遞到ASIN(),其結果將是楠

如果需要檢查南,請嘗試以下內容

if(value != value){ 
    printf("value is nan\n"); 
} 
+0

非常感謝!我設法修復它並添加一些if循環,以便在漂移低於-1或高於1時進行排序。 –

3

Nan示於C++作爲無限的指示,零等分,和非表示數字的一些其它變型。

編輯

正如指出由利瑪Itallia,inf用於無限/零除。我發現這些方法:

template<typename T> 
inline bool isnan(T value) { 
    return value != value; 
} 

// requires #include <limits> 
template<typename T> 
inline bool isinf(T value) { 
    return std::numeric_limits<T>::has_infinity && 
     value == std::numeric_limits<T>::infinity(); 
} 

參考:http://bytes.com/topic/c/answers/588254-how-check-double-inf-nan

+1

實際上,零分會產生'Inf'(除非股利本身是'NaN')。 –

+0

@MatteoItalia感謝您指出它;我發佈了一個修改。 – Rubens