查找下一个最大/最小浮点值 [英] Finding next largest/smallest floating point value

查看:79
本文介绍了查找下一个最大/最小浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个浮点数。我想得到最近的浮动

点数大于或小于给定数字。我调查了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

推荐答案

Peter Ammon写道:
Peter Ammon wrote:
我有一个浮点数。我想得到最近的浮动
点数大于或小于给定的数字。我调查了FLT_EPSILON,但是如果给定的
数是1,它似乎才有用。有什么建议吗?
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?




你必须将epsilon缩放到找到当你更改任何给定浮点数的最低有效位时产生的数字

数。这是非常复杂的IIRC,特别是当

指数发生变化时。


以下链接可能涵盖它。我不知道,还没看过它

但它看起来非常好所以即使#

它也不相关你的问题。

http: //docs.sun.com/source/806-3568/ncg_goldberg.html


-

托马斯。



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.


我肯定有更好的方法,但是:


float num,nextnum,guess;


//不能只添加1.0,因为1.0可能小于sig。 bit

//玩+或 -


guess = num + num;

do {

nextnum = guess;

guess =(num + nextnum)/ 2.0;

}

while((guess!= num)& ;&(guess!= nextnum)); //可以向下或向上滚动

Peter Ammon < PE ********* @ rocketmail.com>在消息中写道

news:ca ********** @ news.apple.com ...
I''m sure there''s a better way, but:

float num, nextnum,guess;

// can''t just add 1.0, because 1.0 could be less than a sig. bit
// play with + or -

guess = num+num;
do {
nextnum = guess;
guess = (num + nextnum) / 2.0;
}
while ((guess != num) && (guess != nextnum)); // could round down or up
"Peter Ammon" <pe*********@rocketmail.com> wrote in message
news:ca**********@news.apple.com...
我有一个浮点数。我想得到最近的浮动
点数大于或小于给定的数字。我调查了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






Peter Ammon写道:
Peter Ammon wrote:

我有一个浮点数。我想得到最接近的
浮点数,它大于或小于给定的
数。我调查了FLT_EPSILON但是如果给定的数字是1,它似乎只是有用。任何建议?

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?




#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!


这篇关于查找下一个最大/最小浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆