-
-
Notifications
You must be signed in to change notification settings - Fork 722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
round相关函数的实现有问题 #253
Comments
Title: round related functions Describe the problemThere are problems with In fact, the In addition, the In short, it is not easy to hand-write a rounding function that meets the standard. It is recommended to forward it to the implementation of the standard library. There is a problem with the implementation of |
描述问题
用宏实现的
round, floor, ceil
是有问题的。floor
和ceil
里面的数字精度不够,比如tb_ceil(1e-15) == 0
。而round
里面的x+0.5
对于0.49999999999999994
在默认舍入模式下也会给出错误的结果:tb_round(0.49999999999999994) == 1
。实际上c标准规定的
floor, ceil, round
是不受浮点环境而改变的,用宏写的加减法是会受到浮点环境影响的。另外tbox里面没有包含的
<math.h>
里面rint
系列函数,c标准里面它们是会受到浮点环境而改变的。总之手写一个符合标准的舍入函数还是很不容易的,建议还是转发到标准库的实现吧。
The text was updated successfully, but these errors were encountered: