Code Review - Bound Parameter 代码审核之限制参数范围

in #cn7 years ago (edited)

There is a piece of bad-smelling code, I happen to read today. The following aims to compute the angle between two line vectors.

这两天又看到一个历史遗留下来写得很糟糕的代码。这个代码大概是求两条线的夹角。

how-to-test-bad-smell-code.jpg

This code has been in the code repro for a long time, and it is brought to attention because of a failing unit test, where the input to the Math.acos function is bigger but very close to 1. Math.acos function returns Nan in this case.

发现这代码是因为有一个单元测试不通过,DEBUG进来 发现是因为 Acos 函数在输入参数大于 1的时候返回 Nan,浮点运算有误差返回了类似 1.00000012 之类的大于1但是却很靠近1的数值。

Easy verification in JS:
比如JS里:

Our 'final' solution is to bound the parameter to the function.
最后面的解决方法是:

Math.acos(Math.max(Math.min(1, x), -1));

It is not elegant, but it works. If the precision is a high priority, we can create a class that stores the parts of the floating number (integer and fraction) using integer type. The integers do not lose precision and we need to implement the arithmetic operations e.g. how to carry over the values.

通过这种方法把参数强制规定在-1到1期间。但是这样的代码并不优美。如果精度要求更严格,可以自定义一个数值类型的类,分开用整数来保存整数部分和小数部分,这样需要定义加减乘除的运算,但是由于整数部分参加运算没有精度损失,所以不会有这样的问题。不过得小心处理像进位这样的问题。

We also have the Math.Net library which provides a rich set of library regarding to Math vectors so this is totally re-inventing the wheel, however if this is a sandbox code, you may also consider operator overloading the vector type. The above code is easily error-prone because it is easy to mess up with the array indices.

撇开重新造轮子的问题(.NET 有像 Math.Net的库了),所以完全可以用现成的库来操作这些向量。即使你不用,你也需要考虑重载这些三维向量的加减乘除运算,上面的代码很容易下标给弄错了。不好维护。

Originally published at https://steemit.com Thank you for reading my post, feel free to Follow, Upvote, Reply, ReSteem (repost) @justyy which motivates me to create more quality posts.

原创 https://Steemit.com 首发。感谢阅读,如有可能,欢迎Follow, Upvote, Reply, ReSteem (repost) @justyy 激励我创作更多更好的内容。

// 稍后同步到我的中文博客和英文算法博客

近期热贴 Recent Popular Posts

Sort:  

It's just wonderful. I beg you to continue.

thanks for the valuable information...
upvoted and followed...!!!

Thanks for info

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 63706.08
ETH 2615.50
USDT 1.00
SBD 2.82