Skip to content
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

Open
0382 opened this issue Dec 9, 2023 · 1 comment
Open

round相关函数的实现有问题 #253

0382 opened this issue Dec 9, 2023 · 1 comment

Comments

@0382
Copy link

0382 commented Dec 9, 2023

描述问题

用宏实现的round, floor, ceil是有问题的。floorceil里面的数字精度不够,比如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标准里面它们是会受到浮点环境而改变的。

总之手写一个符合标准的舍入函数还是很不容易的,建议还是转发到标准库的实现吧。

@Issues-translate-bot
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants