I am using a thirdparty library eg. Lib::ValueType value
. I then do a call to a member function, value.theta()
which performs some mathematical operations including a call to atan2
from <cmath>
. Sometimes the theta
component is empty and an «atan2: domain error» is thrown. However, I can’t catch the exception even by wrapping try{}catch(...)
around the suspect code.
I am using C++ Builder 2009, any idea as to how the exception is being thrown and not being caught by the IDE, or my code. The error pops straight up to the screen as a dialog. I have selected all the options in the IDE to handle everytype of exception.
asked Jul 2, 2010 at 2:11
SethSeth
8,14313 gold badges70 silver badges103 bronze badges
1
The C standard library isn’t aware of C++ exception handling, so try-catch
won’t work. You might want to look at the matherr function — according to the documentation, you can redefine this function in your program in order to handle math exceptions by yourself.
answered Jul 2, 2010 at 2:19
casablancacasablanca
69k7 gold badges133 silver badges149 bronze badges
5
Unfortunately the C math library doesn’t know about C++ exceptions. Most likely you’re seeing an unhandled floating point exception from your hardware. atan2
is extremely forgiving about its inputs: The only invalid case is (0, 0) so all you have to do is verify that one argument is nonzero before making the function call to prevent the exception.
EDIT: Then you need to prevent the invalid theta component when calling the function. What does the third party library documentation say about when it’s valid to call theta
?
answered Jul 2, 2010 at 3:24
Mark BMark B
94.3k10 gold badges107 silver badges185 bronze badges
2
The atan2 function should not throw an exception for arguments (0,0). See Charles Petzold’s discussion here: http://www.charlespetzold.com/blog/2008/09/180741.html. So it looks as though C++ Builder’s standard library is buggy.
answered Jan 28, 2014 at 8:47
Graham AsherGraham Asher
1,5481 gold badge22 silver badges33 bronze badges
prikolist, у тебя в строке
Код C++1 2 otvet = pow(4*(exp(sqrt(abs(a/b)))-exp(-sqrt(abs(a/b))))+3*acos(d),c);
арккосинус не попадает в совой промежуток, вообще м и проиходит потеря значимсости, все таки надо pow по-другому написать
Добавлено через 8 минут 57 секунд
ИМХО нужно еще подключить
Код C++1 #include <complex.h>
or maby
Код C++1 2 3 #include <stdio.h> #include <math.h>
или вообще в double переводить
#include <math.h>
double acos(x);
double x;
Описание.
Функция acos возврaщaет aрккосинус x в интервaле от 0 до n.
Знaчение x должно быть между -1 и 1.
Возврaщaемое знaчение.
Функция acos возврaщaет результaт aрккосинусa. Если x мень-
ше -1 или больше 1, acos устaнaвливaет errno в EDOM, печaтaет со-
общение об ошибке DOMAIN в stderr и возврaщaет 0.
Обрaботкa ошибок может быть модифицировaнa при изменении
процедуры matherr.
См.тaкже asin, atan, atan2, cos, matherr, sin, tan.
Пример:
В следующем примере прогрaммa выдaет подскaзки для вводa до
тех пор, покa введенное знaчение не будет в интервaле от -1 до 1.
C++ | ||
|
Обрaзец выводa:
Cosine = 3
acos: DOMAIN error
Cosine = -1.0
Arc cosine of -1.000000 = 3.141593
Я использую стороннюю библиотеку, например. Lib::ValueType value
. Затем я вызываю функцию-член, value.theta()
который выполняет некоторые математические операции, включая вызов atan2
от <cmath>
, Иногда theta
компонент пуст, и выдается сообщение «atan2: domain error». Однако я не могу поймать исключение, даже обернув try{}catch(...)
вокруг подозрительного кода.
Я использую C ++ Builder 2009, любая идея о том, как генерируется исключение и не перехватывается IDE или моим кодом. Ошибка появляется прямо на экране в виде диалогового окна. Я выбрал все параметры в среде IDE для обработки всех типов исключений.
3 ответы
Стандартная библиотека C не знает обработки исключений C ++, поэтому try-catch
не сработает. Вы можете посмотреть на матер функция — согласно документации, вы можете переопределить эту функцию в своей программе, чтобы самостоятельно обрабатывать математические исключения.
Создан 02 июля ’10, 03:07
К сожалению, математическая библиотека C не знает об исключениях C ++. Скорее всего, вы видите необработанное исключение с плавающей запятой на вашем оборудовании. atan2
чрезвычайно снисходителен к своим входам: единственный недопустимый случай — (0, 0), поэтому все, что вам нужно сделать, это убедиться, что один аргумент не равен нулю, прежде чем выполнять вызов функции для предотвращения исключения.
РЕДАКТИРОВАТЬ: Затем вам нужно предотвратить недопустимый тета-компонент при вызове функции. Что говорится в документации сторонней библиотеки о том, когда можно позвонить theta
?
Создан 02 июля ’10, 14:07
Не тот ответ, который вы ищете? Просмотрите другие вопросы с метками
c++
exception-handling
or задайте свой вопрос.
The C Standard, 7.12.1 [ISO/IEC 9899:2011], defines three types of errors that relate specifically to math functions in <math.h>
. Paragraph 2 states
A domain error occurs if an input argument is outside the domain over which the mathematical function is defined.
Paragraph 3 states
A pole error (also known as a singularity or infinitary) occurs if the mathematical function has an exact infinite result as the finite input argument(s) are approached in the limit.
Paragraph 4 states
A range error occurs if the mathematical result of the function cannot be represented in an object of the specified type, due to extreme magnitude.
An example of a domain error is the square root of a negative number, such as sqrt(-1.0)
, which has no meaning in real arithmetic. Contrastingly, 10 raised to the 1-millionth power, pow(10., 1e6)
, cannot be represented in many floating-point implementations because of the limited range of the type double
and consequently constitutes a range error. In both cases, the function will return some value, but the value returned is not the correct result of the computation. An example of a pole error is log(0.0)
, which results in negative infinity.
Programmers can prevent domain and pole errors by carefully bounds-checking the arguments before calling mathematical functions and taking alternative action if the bounds are violated.
Range errors usually cannot be prevented because they are dependent on the implementation of floating-point numbers as well as on the function being applied. Instead of preventing range errors, programmers should attempt to detect them and take alternative action if a range error occurs.
The following table lists the double
forms of standard mathematical functions, along with checks that should be performed to ensure a proper input domain, and indicates whether they can also result in range or pole errors, as reported by the C Standard. Both float
and long double
forms of these functions also exist but are omitted from the table for brevity. If a function has a specific domain over which it is defined, the programmer must check its input values. The programmer must also check for range errors where they might occur. The standard math functions not listed in this table, such as fabs()
, have no domain restrictions and cannot result in range or pole errors.
Function |
Domain |
Range |
Pole |
---|---|---|---|
|
|
No |
No |
asin(x) |
-1 <= x && x <= 1 |
Yes | No |
atan(x) |
None | Yes | No |
|
|
No |
No |
|
|
Yes |
No |
asinh(x) |
None | Yes | No |
|
|
Yes |
Yes |
|
None |
Yes |
No |
|
None |
Yes |
No |
|
None |
Yes |
No |
|
|
No |
Yes |
|
|
No |
Yes |
|
|
Yes |
No |
logb(x) |
x != 0 |
Yes | Yes |
|
None |
Yes |
No |
|
None |
Yes |
No |
|
|
Yes |
Yes |
|
|
No |
No |
erf(x) |
None | Yes | No |
|
None |
Yes |
No |
|
|
Yes |
Yes |
|
None |
Yes |
No |
|
|
Yes |
No |
|
None |
Yes |
No |
|
None |
Yes |
No |
|
None |
Yes |
No |
Domain and Pole Checking
The most reliable way to handle domain and pole errors is to prevent them by checking arguments beforehand, as in the following exemplar:
double safe_sqrt(double x) { if (x < 0) { fprintf(stderr, "sqrt requires a nonnegative argument"); /* Handle domain / pole error */ } return sqrt (x); }
Range Checking
Programmers usually cannot prevent range errors, so the most reliable way to handle them is to detect when they have occurred and act accordingly.
The exact treatment of error conditions from math functions is tedious. The C Standard, 7.12.1 [ISO/IEC 9899:2011], defines the following behavior for floating-point overflow:
A floating result overflows if the magnitude of the mathematical result is finite but so large that the mathematical result cannot be represented without extraordinary roundoff error in an object of the specified type. If a floating result overflows and default rounding is in effect, then the function returns the value of the macro
HUGE_VAL
,HUGE_VALF
, orHUGE_VALL
according to the return type, with the same sign as the correct value of the function; if the integer expressionmath_errhandling & MATH_ERRNO
is nonzero, the integer expressionerrno
acquires the valueERANGE
; if the integer expressionmath_errhandling & MATH_ERREXCEPT
is nonzero, the «overflow» floating-point exception is raised.
It is preferable not to check for errors by comparing the returned value against HUGE_VAL
or 0
for several reasons:
- These are, in general, valid (albeit unlikely) data values.
- Making such tests requires detailed knowledge of the various error returns for each math function.
- Multiple results aside from
HUGE_VAL
and0
are possible, and programmers must know which are possible in each case. - Different versions of the library have varied in their error-return behavior.
It can be unreliable to check for math errors using errno
because an implementation might not set errno
. For real functions, the programmer determines if the implementation sets errno
by checking whether math_errhandling & MATH_ERRNO
is nonzero. For complex functions, the C Standard, 7.3.2, paragraph 1, simply states that «an implementation may set errno
but is not required to» [ISO/IEC 9899:2011].
The obsolete System V Interface Definition (SVID3) [UNIX 1992] provides more control over the treatment of errors in the math library. The programmer can define a function named matherr()
that is invoked if errors occur in a math function. This function can print diagnostics, terminate the execution, or specify the desired return value. The matherr()
function has not been adopted by C or POSIX, so it is not generally portable.
The following error-handing template uses C Standard functions for floating-point errors when the C macro math_errhandling
is defined and indicates that they should be used; otherwise, it examines errno
:
#include <math.h> #include <fenv.h> #include <errno.h> /* ... */ /* Use to call a math function and check errors */ { #pragma STDC FENV_ACCESS ON if (math_errhandling & MATH_ERREXCEPT) { feclearexcept(FE_ALL_EXCEPT); } errno = 0; /* Call the math function */ if ((math_errhandling & MATH_ERRNO) && errno != 0) { /* Handle range error */ } else if ((math_errhandling & MATH_ERREXCEPT) && fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) != 0) { /* Handle range error */ } }
See FLP03-C. Detect and handle floating-point errors for more details on how to detect floating-point errors.
Subnormal Numbers
A subnormal number is a nonzero number that does not use all of its precision bits [IEEE 754 2006]. These numbers can be used to represent values that are closer to 0 than the smallest normal number (one that uses all of its precision bits). However, the asin()
, asinh()
, atan()
, atanh()
, and erf()
functions may produce range errors, specifically when passed a subnormal number. When evaluated with a subnormal number, these functions can produce an inexact, subnormal value, which is an underflow error. The C Standard, 7.12.1, paragraph 6 [ISO/IEC 9899:2011], defines the following behavior for floating-point underflow:
The result underflows if the magnitude of the mathematical result is so small that the mathematical result cannot be represented, without extraordinary roundoff error, in an object of the specified type. If the result underflows, the function returns an implementation-defined value whose magnitude is no greater than the smallest normalized positive number in the specified type; if the integer expression
math_errhandling & MATH_ERRNO
is nonzero, whethererrno
acquires the valueERANGE
is implementation-defined; if the integer expressionmath_errhandling & MATH_ERREXCEPT
is nonzero, whether the ‘‘underflow’’ floating-point exception is raised is implementation-defined.
Implementations that support floating-point arithmetic but do not support subnormal numbers, such as IBM S/360 hex floating-point or nonconforming IEEE-754 implementations that skip subnormals (or support them by flushing them to zero), can return a range error when calling one of the following families of functions with the following arguments:
fmod
((min+subnorm), min)
remainder
((min+
), min)subnorm
remquo
((min+
), min, quo)subnorm
where min
is the minimum value for the corresponding floating point type and subnorm
is a subnormal value.
If Annex F is supported and subnormal results are supported, the returned value is exact and a range error cannot occur. The C Standard, F.10.7.1 [ISO/IEC 9899:2011], specifies the following for the fmod()
, remainder()
, and remquo()
functions:
When subnormal results are supported, the returned value is exact and is independent of the current rounding direction mode.
Annex F, subclause F.10.7.2, paragraph 2, and subclause F.10.7.3, paragraph 2, of the C Standard identify when subnormal results are supported.
Noncompliant Code Example (sqrt()
)
This noncompliant code example determines the square root of x
:
#include <math.h> void func(double x) { double result; result = sqrt(x); }
However, this code may produce a domain error if x
is negative.
Compliant Solution (sqrt()
)
Because this function has domain errors but no range errors, bounds checking can be used to prevent domain errors:
#include <math.h> void func(double x) { double result; if (isless(x, 0.0)) { /* Handle domain error */ } result = sqrt(x); }
Noncompliant Code Example (sinh()
, Range Errors)
This noncompliant code example determines the hyperbolic sine of x
:
#include <math.h> void func(double x) { double result; result = sinh(x); }
This code may produce a range error if x
has a very large magnitude.
Compliant Solution (sinh()
, Range Errors)
Because this function has no domain errors but may have range errors, the programmer must detect a range error and act accordingly:
#include <math.h> #include <fenv.h> #include <errno.h> void func(double x) { double result; { #pragma STDC FENV_ACCESS ON if (math_errhandling & MATH_ERREXCEPT) { feclearexcept(FE_ALL_EXCEPT); } errno = 0; result = sinh(x); if ((math_errhandling & MATH_ERRNO) && errno != 0) { /* Handle range error */ } else if ((math_errhandling & MATH_ERREXCEPT) && fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) != 0) { /* Handle range error */ } } /* Use result... */ }
Noncompliant Code Example (pow()
)
This noncompliant code example raises x
to the power of y
:
#include <math.h> void func(double x, double y) { double result; result = pow(x, y); }
This code may produce a domain error if x
is negative and y
is not an integer value or if x
is 0 and y
is 0. A domain error or pole error may occur if x
is 0 and y
is negative, and a range error may occur if the result cannot be represented as a double
.
Compliant Solution (pow()
)
Because the pow()
function can produce domain errors, pole errors, and range errors, the programmer must first check that x
and y
lie within the proper domain and do not generate a pole error and then detect whether a range error occurs and act accordingly:
#include <math.h> #include <fenv.h> #include <errno.h> void func(double x, double y) { double result; if (((x == 0.0f) && islessequal(y, 0.0)) || isless(x, 0.0)) { /* Handle domain or pole error */ } { #pragma STDC FENV_ACCESS ON if (math_errhandling & MATH_ERREXCEPT) { feclearexcept(FE_ALL_EXCEPT); } errno = 0; result = pow(x, y); if ((math_errhandling & MATH_ERRNO) && errno != 0) { /* Handle range error */ } else if ((math_errhandling & MATH_ERREXCEPT) && fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) != 0) { /* Handle range error */ } } /* Use result... */ }
Noncompliant Code Example (asin()
, Subnormal Number)
This noncompliant code example determines the inverse sine of x
:
#include <math.h> void func(float x) { float result = asin(x); /* ... */ }
Compliant Solution (asin()
, Subnormal Number)
Because this function has no domain errors but may have range errors, the programmer must detect a range error and act accordingly:
#include <math.h> #include <fenv.h> #include <errno.h> void func(float x) { float result; { #pragma STDC FENV_ACCESS ON if (math_errhandling & MATH_ERREXCEPT) { feclearexcept(FE_ALL_EXCEPT); } errno = 0; result = asin(x); if ((math_errhandling & MATH_ERRNO) && errno != 0) { /* Handle range error */ } else if ((math_errhandling & MATH_ERREXCEPT) && fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) != 0) { /* Handle range error */ } } /* Use result... */ }
Risk Assessment
Failure to prevent or detect domain and range errors in math functions may cause unexpected results.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
FLP32-C |
Medium |
Probable |
Medium |
P8 |
L2 |
Automated Detection
Tool |
Version |
Checker |
Description |
---|---|---|---|
Astrée |
22.04 |
stdlib-limits |
Partially checked |
Axivion Bauhaus Suite |
7.2.0 |
CertC-FLP32 | Partially implemented |
CodeSonar |
7.2p0 |
MATH.DOMAIN.ATAN MATH.DOMAIN.TOOHIGH MATH.DOMAIN.TOOLOW MATH.DOMAIN MATH.RANGE MATH.RANGE.GAMMA MATH.DOMAIN.LOG MATH.RANGE.LOG MATH.DOMAIN.FE_INVALID MATH.DOMAIN.POW MATH.RANGE.COSH.TOOHIGH MATH.RANGE.COSH.TOOLOW MATH.DOMAIN.SQRT |
Arctangent Domain Error Argument Too High Argument Too Low Floating Point Domain Error Floating Point Range Error Gamma on Zero Logarithm on Negative Value Logarithm on Zero Raises FE_INVALID Undefined Power of Zero cosh on High Number cosh on Low Number sqrt on Negative Value |
Helix QAC |
2022.4 |
C5025 C++5033 |
|
Parasoft C/C++test |
2022.2 |
CERT_C-FLP32-a |
Validate values passed to library functions |
PC-lint Plus |
1.4 |
2423 |
Partially supported: reports domain errors for functions with the Semantics *dom_1, *dom_lt0, or *dom_lt1, including standard library math functions |
Polyspace Bug Finder |
R2022b |
CERT-C: Rule FLP32-C | Checks for invalid use of standard library floating point routine (rule fully covered) |
PRQA QA-C |
9.7 |
5025 | |
PRQA QA-C++ |
4.4 |
5033 | |
RuleChecker |
22.04 |
stdlib-limits |
Partially checked |
TrustInSoft Analyzer |
1.38 |
out-of-range argument | Partially verified. |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Key here (explains table format and definitions)
CERT-CWE Mapping Notes
Key here for mapping notes
CWE-391 and FLP32-C
Intersection( CWE-391, FLP32-C) =
- Failure to detect range errors in floating-point calculations
CWE-391 — FLP32-C
- Failure to detect errors in functions besides floating-point calculations
FLP32-C – CWE-391 =
- Failure to detect domain errors in floating-point calculations
CWE-682 and FLP32-C
Independent( INT34-C, FLP32-C, INT33-C) CWE-682 = Union( FLP32-C, list) where list =
- Incorrect calculations that do not involve floating-point range errors
Bibliography
Определено в заголовке <math.h> |
||
---|---|---|
float atan2f( float y, float x );
|
(1) | (since C99) |
double atan2( double y, double x );
|
(2) | |
long double atan2l( long double y, long double x );
|
(3) | (since C99) |
Определено в заголовке <tgmath.h> |
||
#define atan2( y, x )
|
(4) | (since C99) |
1-3) Вычисляет арктангенс y/x
, используя знаки аргументов для определения правильного квадранта.
4) Макрос общего типа: если какой-либо аргумент имеет тип long double
, atan2l
. В противном случае, если какой-либо аргумент имеет тип integer или тип double
, вызывается atan2
.В противном случае atan2f
.
Parameters
Return value
Если ошибок не возникает, арктангенс y/x
(arctan(y/x)) в диапазоне [-π ; +π] радиан, возвращается.
Y argument
Return value
X argument
При возникновении ошибки домена возвращается значение,определяемое реализацией.
При ошибке диапазона из-за недопотока возвращается правильный результат (после округления).
Error handling
Об ошибках сообщается, как указано в math_errhandling
.
Ошибка домена может возникнуть, если x
и y
оба равны нулю.
Если реализация поддерживает арифметику с плавающей запятой IEEE (IEC 60559),
- Если
x
иy
равны нулю, ошибка доменаdoes notoccur - Если
x
иy
оба равны нулю, ошибка диапазона также не возникает - Если
y
равен нулю, ошибка полюса не возникает - Если
y
равно±0
, аx
отрицательно или-0
, возвращается±π
. - Если
y
равно±0
, аx
положительное или+0
, возвращается±0
. - Если
y
равно±∞
, аx
конечно, возвращается±π/2
. - Если
y
равно±∞
, аx
равно-∞
, возвращается±3π/4
. - Если
y
равно±∞
, аx
равно+∞
, возвращается±π/4
. - Если
x
равно±0
, аy
отрицательно, возвращается-π/2
. - Если
x
равен±0
, аy
положительный, возвращается+π/2
. - Если
x
равно-∞
, аy
конечно и положительно, возвращается+π
- Если
x
равно-∞
, аy
конечно и отрицательно, возвращается-π
- Если
x
равно+∞
, аy
конечно и положительно, возвращается+0
. - Если
x
равно+∞
, аy
конечно и отрицательно, возвращается-0
. - Если
x
равен NaN илиy
равен NaN, возвращается NaN
Notes
atan2(y, x)
эквивалентно carg(x + I*y)
.
POSIX указывает , что в случае потери значимости возвращаемым значением является y/x
, а если это не поддерживается, возвращается определяемое реализацией значение, не превышающее DBL_MIN
, FLT_MIN
и LDBL_MIN
.
Example
int main(void) { // normal usage: the signs of the two arguments determine the quadrant // atan2(1,1) = +pi/4, Quad I printf("(+1,+1) cartesian is (%f,%f) polarn", hypot( 1, 1), atan2( 1, 1)); // atan2(1, -1) = +3pi/4, Quad II printf("(+1,-1) cartesian is (%f,%f) polarn", hypot( 1,-1), atan2( 1,-1)); // atan2(-1,-1) = -3pi/4, Quad III printf("(-1,-1) cartesian is (%f,%f) polarn", hypot(-1,-1), atan2(-1,-1)); // atan2(-1,-1) = -pi/4, Quad IV printf("(-1,+1) cartesian is (%f,%f) polarn", hypot(-1, 1), atan2(-1, 1)); // special values printf("atan2(0, 0) = %f atan2(0, -0)=%fn", atan2(0,0), atan2(0,-0.0)); printf("atan2(7, 0) = %f atan2(7, -0)=%fn", atan2(7,0), atan2(7,-0.0)); }
Output:
(+1,+1) cartesian is (1.414214,0.785398) polar (+1,-1) cartesian is (1.414214,2.356194) polar (-1,-1) cartesian is (1.414214,-2.356194) polar (-1,+1) cartesian is (1.414214,-0.785398) polar atan2(0, 0) = 0.000000 atan2(0, -0)=3.141593 atan2(7, 0) = 1.570796 atan2(7, -0)=1.570796
References
- Стандарт С17 (ISO/IEC 9899:2018):
- 7.12.4.4 Функции atan2 (c:174)
- 7.25 Обобщенная математика типов <tgmath.h> (стр: 272-273)
- F.10.1.4 Функции atan2 (p:378)
- Стандарт C11 (ISO/IEC 9899:2011):
- 7.12.4.4 Функции атана2 (стр.239)
- 7.25 Типовая математика <tgmath.h> (стр: 373-375)
- F.10.1.4 Функции атана2 (стр.519)
- Стандарт С99 (ISO/IEC 9899:1999):
- 7.12.4.4 Функции атана2 (стр.219)
- 7.22 Типовая математика <tgmath.h> (стр: 335-337)
- F.9.1.4 Функции атана2 (стр.456)
- Стандарт C89/C90 (ISO/IEC 9899:1990):
- 4.5.2.4 Функция атана2
See also
(C99)(C99) |
вычисляет синус дуги (({smallarcsin{x}})arcsin(x)) (function) |
(C99)(C99) |
вычисляет косинус дуги (({smallarccos{x}})arccos(x)) (function) |
(C99)(C99) |
вычисляет тангенс дуги (({smallarctan{x}})arctan(x)) (function) |
(C99)(C99)(C99) |
вычисляет фазовый угол комплексного числа (function) |
Документация C++ для atan2 |
C
-
асинх,асинхф,асинхль
-
атан,атанф,атанф
-
atanh, atanhf, atanhl
-
cbrt, cbrtf, cbrtl
Structural Bridge Design Forum
Welcome to Autodesk’s Structural Bridge Design Forums. Share your knowledge, ask questions, and explore popular Structural Bridge Design topics.
Turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
Showing results for
Search instead for
Did you mean:
-
Back to forum -
Previous -
Next
302 Views, 3 Replies
03-31-2016
06:27 PM
Design line atan2: Domain error
Hi,
I’ve been using ASBD for about a year now, and I recently encountered this issue (see screenshot)
What I did was, I was importing a design line from a dxf file. I’ve done importing design lines before, and this is the first time I encountered such error. Can someone please help me on this.
-
Back to forum -
Previous -
Next
3 REPLIES 3
04-06-2016
12:05 PM
Are you able to share the dxf file (and tell us which layer the line is on) so we can analyze
Simon Hooper
Software Development
02-28-2017
01:04 AM
Sorry for the very late reply, apparently I redrew the structure and the software ran without a hitch.
-
Back to forum -
Previous -
Next
В предыдущем посте о связи прямоугольных и полярных координат в частности выведены формулы для перевода прямоугольных координат в полярные:
r = sqrt(x * x + y * y);
фи = atan(y / x);
Формула для вычисления угла фи выведена из геометрического определения тангенса угла
tan(фи) = y / x;
Изобразим график функции z = tan(фи)
, где z = y / x
:
Вспомним, что график обратной функции (в нашем случае арктангенса по отношению к тангенсу) можно получить, повернув исходный график влево на 90 градусов и отразив полученное зеркально слева направо. Итак, график функции фи = atan(z)
:
Получилось, что в случае функции фи = atan(z)
для одной и той же области определения (значения на оси z) существует множество областей значений (значения на оси фи).
Например, для графика, проходящего через начало координат (z = 0, фи = 0) область значений находится в пределах от –PI/2
до PI/2
. Этот график обычно и обозначают формулой фи = atan(z)
, уточняя рядом с формулой область значений. Графики, лежащие выше и ниже этого графика, обозначают, прибавляя или отнимая от исходной формулы число Пи. Например, для нескольких графиков, лежащих ближе к началу координат:
... фи = atan(z) + 2 * PI, 3*PI/2 < фи < 5*PI/2; фи = atan(z) + PI, PI/2 < фи < 3*PI/2; фи = atan(z), –PI/2 < фи < PI/2; фи = atan(z) – PI, –3*PI/2 < фи < –PI/2; фи = atan(z) – 2 * PI, –5*PI/2 < фи < –3*PI/2; ...
Определенная в стандарте языка C++ функция для вычисления арктангенса atan является отображением графика фи = atan(z)
, проходящего через начало координат, то есть она возвращает значения в пределах от –PI/2
до PI/2
.
Теперь вернемся к рисунку с полярными координатами в начале этого поста. И увидим, что, воспользовавшись стандартной функцией atan в языке C++ при попытке перевода прямоугольных координат в полярные, мы сможем получить угол фи только для 1-го и 4-го квадрантов системы координат (про квадранты я писал в посте о системах координат) из-за вышеописанного ограничения возвращаемых функцией atan значений пределами от –PI/2
до PI/2
.
Что же делать? Воспользуемся кусочками других графиков арктангенса, о которых писалось выше. Вот как нужные кусочки графиков будут выглядеть на рисунке:
То есть для реализации этого в программе на C++ через стандартную функцию atan нужно будет описать нахождение угла фи с помощью следующих равенств:
(1 и 4 квадранты) если (x > 0), то фи = atan(y / x);
красный график(2 квадрант) если (x < 0 и y > 0), то фи = atan(y / x) + PI;
зеленый(3 квадрант) если (x < 0 и y < 0), то фи = atan(y / x) – PI;
синий
Однако, вместо этого в программе на C++ можно использовать стандартную функцию нахождения арктангенса atan2, которая заменяет все вышеперечисленные равенства и выдает угол фи в нужных пределах от –PI
до PI
. То есть для перевода прямоугольных координат в полярные нужно использовать следующие формулы:
r = sqrt(x * x + y * y);
фи = atan2(y / x);
Если стандартная функция atan принимает один аргумент, то стандартная функция atan2 принимает два аргумента — прямоугольные координаты y и x (именно в таком порядке) и в зависимости от знаков каждого из аргументов выдает нужный график с нужным результатом:
radius = sqrt(x * x + y * y); angle = atan2(y, x);
Подводящие к этому посты:
1. Мера измерения углов, радианы и градусы (тут).
2. Число Пи в программе на C++ (тут).
3. Прямоугольная и полярная системы координат (тут).
4. Связь прямоугольных и полярных координат (тут).