Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 此代码是否正确用于环绕角度(-180180)_C++_C_Angle - Fatal编程技术网

C++ 此代码是否正确用于环绕角度(-180180)

C++ 此代码是否正确用于环绕角度(-180180),c++,c,angle,C++,C,Angle,我在读这篇文章时遇到了这个函数wrapPi()。我知道如何包装角度,但这段代码到底在做什么 float wrapPi ( float theta ) { // Check if already in range. This is not strictly necessary, // but it will be a very common sit u a t i o n . We don ’ t want to // incur a speed hit and perhaps floating

我在读这篇文章时遇到了这个函数
wrapPi()
。我知道如何包装角度,但这段代码到底在做什么

float wrapPi ( float theta ) {
// Check if already in range. This is not strictly necessary,
// but it will be a very common sit u a t i o n . We don ’ t want to
// incur a speed hit and perhaps floating precision loss if
// it’s not necessary
if ( fabs( theta ) <= PI ) {
    // One revolution is 2PI .
    const float TWOPPI = 2.0f∗PI ;
    // Out of range. Determine how many ”revolutions”
    // we need to add .
    float revolutions = floor (( theta + PI ) ∗ ( 1.0f /TWOPPI )) ;
    // Subtract it off
    theta −= revolutions ∗ TWOPPI ;
}
return theta;
}
float-wrapPi(float-theta){
//检查是否已经在范围内。这不是严格必要的,
//但这将是一个非常普遍的问题,我们不想
//如果发生以下情况,可能会导致速度命中和浮点精度损失
//没必要
如果(fabs(θ)它将角度θ与(θ=pi)映射到-pi…pi的范围内

// if false theta is already >=-PI && <=PI
if ( fabs( theta ) <= PI ) {
    // This is the range between -PI and PI
    const float TWOPPI = 2.0f∗PI ;

    // The next two steps are kind of an floating point modulo(theta,2PI)
    float revolutions = floor (( theta + PI ) ∗ ( 1.0f /TWOPPI )) ;
    theta −= revolutions ∗ TWOPPI ;
}

//如果false theta已经>=-PI&&则此行有错误:

if ( fabs( theta ) <= PI ) {
这是唯一一个不能仅返回现有值
theta
的条件。
if
语句的其余部分计算出需要添加或删除的次数 减去2*PI,以在正确范围内找到相当于
theta
的角度

就个人而言,我更喜欢为
if(theta PI)
编写单独的代码块,但这可能是因为遇到
过去,
fabs
的实现非常缓慢。

请指定θ是以度数还是以弧度表示。注意:如果代码使用的是
float
,最好使用
fabsf()
floorf()
我认为wrapPi()实现返回的值范围是[-PI,PI),因为如果将-PI作为“theta”传递,则“revolutions”将为0,而“theta”将以-PI的形式不变地返回。如果确实希望以间隔(-PI,PI]返回值,如标题所示,则必须如下更改实现:
float revolutions=ceil((theta-PI)∗ (1.0f/TWOPPI));
这一行将确保
PI==wrapPi(-PI)
通常不会。如果θ=2pi,则条件为未命中,函数返回θ(=2pi)@Olotiar dam——你是对的!在我看来,(1)保持每个θ>=0不变(尽管如果θ在0…π范围内,它会进行一些不必要的计算)。(2)将每个θ-π映射到>2*π的范围,并且(3)再次离开每个θπ)
尽管:-(.更接近,但还不完全是。除非我弄错了,因为-Pifloat
/
double
转换可能不存在,而代码使用了
fabsf()?我观察到的问题实际上是使用了<代码>双输入,所以不可能是转换。这是Visual C++。我个人在VS2010上运行的最后一个标号显示:<代码> FAX <代码>占用了大约一半的时间<代码> MODF,<代码> Load < /C> >或<代码> CEIL>代码>,在<>代码> Sin < /C> >或cos
。比两个
double
变量相乘慢得多。嗯,怀疑管道问题,或者简单地说它是VS。似乎
fabs()
应该很快,因为它只需要清除1位。
if ( fabs( theta ) > PI ) {