问题描述
我有一个浮点数。我想得到最近的浮动
点数大于或小于给定数字。我调查了FLT_EPSILON
但是如果给定的
数是1,它似乎才有用。有什么建议吗?
谢谢,
-Peter
I have a floating point number. I''d like to get the nearest floating
point number that is larger or smaller than the given number. I
investigated FLT_EPSILON but it only seems to be useful if the given
number is 1. Any suggestions?
Thanks,
-Peter
推荐答案
你必须将epsilon缩放到找到当你更改任何给定浮点数的最低有效位时产生的数字
数。这是非常复杂的IIRC,特别是当
指数发生变化时。
以下链接可能涵盖它。我不知道,还没看过它
但它看起来非常好所以即使#
它也不相关你的问题。
-
托马斯。
You have to scale epsilon to find the number that results when
you change the least significant bit for any given floating point
number. This is quite complicated IIRC, especially when the
exponent changes.
The following link might cover it. I don''t know, haven''t read it
yet, but it seems to be very good so I am including it even if#
it is not relevant to your question.
http://docs.sun.com/source/806-3568/ncg_goldberg.html
--
Thomas.
#include< float.h>
浮动scaledepsilon(浮点数)
{
浮动试用;
trial =号码* FLT_EPSILON;
而(号码!=(号码 - 试用/ 2.0)试用=号码/ 2.0;
返回试用期;
} / *未经测试,但应该关闭* /
对于非常小的数字值可能非常怀疑。
-
Chuck F(cb********@yahoo.com)(cb********@worldnet.att.net)
可用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>使用worldnet地址!
#include <float.h>
float scaledepsilon(float number)
{
float trial;
trial = number * FLT_EPSILON;
while (number != (number - trial/2.0) trial = number/2.0;
return trial;
} /* untested, but should be close */
Probably highly suspicious for very small values of number.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
这篇关于查找下一个最大/最小浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!