tbox
tbox copied to clipboard
round相关函数的实现有问题
描述问题
用宏实现的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标准里面它们是会受到浮点环境而改变的。
总之手写一个符合标准的舍入函数还是很不容易的,建议还是转发到标准库的实现吧。
Bot detected the issue body's language is not English, translate it automatically.
Title: round related functions
Describe the problem
There are problems with round, floor, ceil
implemented using macros. The numbers in floor
and ceil
are not precise enough, such as tb_ceil(1e-15) == 0
. And x+0.5
in round
will also give wrong results for 0.499999999999999994
in the default rounding mode: tb_round(0.499999999999999994) == 1
.
In fact, the floor, ceil, round
specified by the C standard are not changed by the floating point environment. The addition and subtraction written with macros will be affected by the floating point environment.
In addition, the rint
series functions in <math.h>
are not included in tbox. In the C standard, they will be changed by the floating point environment.
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