如何确定一个数字是否在另一个数字的百分比范围内
我正在编写 iPhone 代码,模糊地识别滑动的线是否是直线。我获取两个端点的方位角,并将其与 0、90、180 和 270 度进行比较,公差为正负 10 度。现在我用一堆 if 块来做这件事,这看起来超级笨重。
如何编写一个函数,在给定方位 0..360、公差百分比(例如 20% = (-10° 到 +10°))和直线的情况下角度如90度,返回轴承是否在公差范围内?
更新: 我也许太具体了。我认为一个很好的通用函数可以确定一个数字是否在另一个数字的百分比范围内,在许多领域都有用处。
例如:数字 swipeLength 是否在 maxSwipe 的 10% 范围内?那会有用的。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该适用于任意两个角度。
This should work for any two angles.
20% 小数化等于 0.2。只需除以 100.0 即可得到小数。除以 2.0 即可得到可接受范围的一半。 (合并为 200.0 除数)
从那里,加上 1.0 并减去 1.0,得到 90% 和 110% 值。
如果第一个数字在范围之间,那么就可以了。
注意:这里没有对 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.
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.
回答您的改进/新问题:
为了解释一下,您将测试值和目标值之间的绝对差除以目标值(两个“绝对”调用确保这也适用于负目标值和测试值,但不具有负百分比/公差)。这给出了以小数形式表示的差异百分比,将其乘以 100 得到百分比的“常见”表达式 (10% = 0.10),
Answer to your refined/new question:
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),