English 中文(简体)
How can I handle weird errors from calculating acos / sin / atan2?
原标题:

Has anyone seen this weird value while handling sin / cos/ tan / acos.. math stuff?

===THE WEIRD VALUE===

-1.#IND00

=====================

void inverse_pos(double x, double y, double& theta_one, double& theta_two)
{
    // Assume that L1 = 350  and L2 = 250


    double B = sqrt(x*x + y*y);
    double angle_beta = atan2(y, x);
    double angle_alpha = acos((L2*L2 - B*B - L1*L1) / (-2*B*L1));
    theta_one = angle_beta + angle_alpha;
    theta_two = atan2((y-L1*sin(theta_one)), (x-L1*cos(theta_one)));
}

This is the code I was working on.

In a particular condition - like when x & y are 10 & 10, this code stores -1.#IND00 into theta_one & theta_two.

It doesn t look like either characters or numbers :(

Without a doubt, atan2 / acos / stuff are the problems.

But the problem is, try and catch doesn t work either cuz those double variables have successfully stored some values in them.

Moreover, the following calculations never complain about it and never break the program!

I m thinking of forcing to use this value somehow and make the entire program crash... So that I can catch this error..

Except for that idea, I have no idea how I should check whether these theta_one and theta_two variables have stored this crazy values.

Any good ideas?

Thank you in advance..

最佳回答

The "weird value" is NaN (not a number).

The problem is because (L2*L2 - B*B - L1*L1) / (-2*B*L1) = 6.08112… is outside of the range [-1, 1] where acos is well-defined for real numbers. So NaN is returned.

Are you sure the formula is correct?


If you want to catch an NaN, the NaN needs to be a signaling NaN. For gcc, compile with the -fsignaling-nans option.

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签