如何确定一个数字是否在另一个数字的百分比范围内

发布于 2024-08-02 21:30:29 字数 944 浏览 3 评论 0原文

我正在编写 iPhone 代码,模糊地识别滑动的线是否是直线。我获取两个端点的方位角,并将其与 0、90、180 和 270 度进行比较,公差为正负 10 度。现在我用一堆 if 块来做这件事,这看起来超级笨重。

如何编写一个函数,在给定方位 0..360、公差百分比(例如 20% = (-10° 到 +10°))和直线的情况下角度如90度,返回轴承是否在公差范围内?

更新: 我也许太具体了。我认为一个很好的通用函数可以确定一个数字是否在另一个数字的百分比范围内,在许多领域都有用处。

例如:数字 swipeLength 是否在 ma​​xSwipe10% 范围内?那会有用的。

 BOOL isNumberWithinPercentOfNumber(float firstN, float percent, float secondN) {
      // dunno how to calculate
 }

 BOOL result;

 float swipeLength1 = 303; 
 float swipeLength2 = 310; 

 float tolerance = 10.0; // from -5% to 5%
 float maxSwipe = 320.0;

 result = isNumberWithinPercentOfNumber(swipeLength1, tolerance, maxSwipe); 
 // result = NO

 result = isNumberWithinPercentOfNumber(swipeLength2, tolerance, maxSwipe);
 // result = YES

你明白我的意思吗?

I'm writing iPhone code that fuzzily recognizes whether a swiped line is straight-ish. I get the bearing of the two end points and compare it to 0, 90, 180 and 270 degrees with a tolerance of 10 degrees plus or minus. Right now I do it with a bunch of if blocks, which seems super clunky.

How to write a function that, given the bearing 0..360, the tolerance percentage (say 20% = (-10° to +10°)) and a straight angle like 90 degrees, returns whether the bearing is within the tolerance?

Update: I am, perhaps, being too specific. I think a nice, general function that determines whether a number is within a percentage of another number has utility in many areas.

For instance: Is the number swipeLength within 10% of maxSwipe? That would be useful.

 BOOL isNumberWithinPercentOfNumber(float firstN, float percent, float secondN) {
      // dunno how to calculate
 }

 BOOL result;

 float swipeLength1 = 303; 
 float swipeLength2 = 310; 

 float tolerance = 10.0; // from -5% to 5%
 float maxSwipe = 320.0;

 result = isNumberWithinPercentOfNumber(swipeLength1, tolerance, maxSwipe); 
 // result = NO

 result = isNumberWithinPercentOfNumber(swipeLength2, tolerance, maxSwipe);
 // result = YES

Do you see what I'm getting at?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚风撩人 2024-08-09 21:30:29
int AngularDistance (int angle, int targetAngle) 
{

    int diff = 0;
    diff = abs(targetAngle - angle)

    if (diff > 180) diff = 360 - diff;

    return diff;
}

这应该适用于任意两个角度。

int AngularDistance (int angle, int targetAngle) 
{

    int diff = 0;
    diff = abs(targetAngle - angle)

    if (diff > 180) diff = 360 - diff;

    return diff;
}

This should work for any two angles.

晌融 2024-08-09 21:30:29

20% 小数化等于 0.2。只需除以 100.0 即可得到小数。除以 2.0 即可得到可接受范围的一半。 (合并为 200.0 除数)

从那里,加上 1.0 并减去 1.0,得到 90% 和 110% 值。
如果第一个数字在范围之间,那么就可以了。

BOOL isNumberWithinPercentOfNumber(float firstN, float percent, float secondN) {
      float decimalPercent = percent / 200.0;
      float highRange = secondN * (1.0 + decimalPercent);
      float lowRange = secondN * (1.0 - decimalPercent);
      return lowRange <= firstN && firstN <= highRange;
 }

注意:这里没有对 NaN 或负值进行错误检查。您需要将其添加到生产代码中。

更新:使百分比包含+/-范围。

20% as a decimal is equal to 0.2. Just divide by 100.0 to get the decimal. Divide by 2.0 to get half of the acceptable range. (Combined into 200.0 divisor)

From there, add and subtract from 1.0 to get the 90%, and 110% values.
If the first number is between the ranges, then there you have it.

BOOL isNumberWithinPercentOfNumber(float firstN, float percent, float secondN) {
      float decimalPercent = percent / 200.0;
      float highRange = secondN * (1.0 + decimalPercent);
      float lowRange = secondN * (1.0 - decimalPercent);
      return lowRange <= firstN && firstN <= highRange;
 }

Note: there's no error checking here for NaN or negative values. You'll want to add that for production code.

Update: to make percent to include both +/- range.

心病无药医 2024-08-09 21:30:29

回答您的改进/新问题:

bool isNumberWithinPercentOfNumber (float n1, float percentage, float n2)
{

if (n2 == 0.0) //check for div by zero, may not be necessary for float
   return false; //default for a target value of zero is false
else
   return (percentage > abs(abs(n2 - n1)/n2)*100.0);
}

为了解释一下,您将测试值和目标值之间的绝对差除以目标值(两个“绝对”调用确保这也适用于负目标值和测试值,但不具有负百分比/公差)。这给出了以小数形式表示的差异百分比,将其乘以 100 得到百分比的“常见”表达式 (10% = 0.10),

Answer to your refined/new question:

bool isNumberWithinPercentOfNumber (float n1, float percentage, float n2)
{

if (n2 == 0.0) //check for div by zero, may not be necessary for float
   return false; //default for a target value of zero is false
else
   return (percentage > abs(abs(n2 - n1)/n2)*100.0);
}

To explain, you take the absolute difference between your test and target value, and divide it by the target value (the two 'abs'olute calls make sure this works with negative target and test numbers too, but not with negative percentages/tolerances). This gives you the percentage of the difference expressed as decimal fraction, multiplies it with 100 to give the 'common' expression of percentage (10% = 0.10),

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文