查找下一个最大/最小浮点值

编程入门 行业动态 更新时间:2024-10-23 07:25:10
本文介绍了查找下一个最大/最小浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个浮点数。我想得到最近的浮动 点数大于或小于给定数字。我调查了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/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. docs.sun/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>在消息中写道 news:ca ********** @ news.apple ... 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> wrote in message news:ca**********@news.apple... 我有一个浮点数。我想得到最近的浮动点数大于或小于给定的数字。我调查了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)(cb********@worldnet.att) 可用于咨询/临时嵌入式和系统。 < cbfalconer.home.att>使用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) (cb********@worldnet.att) Available for consulting/temporary embedded and systems. <cbfalconer.home.att> USE worldnet address!

更多推荐

查找下一个最大/最小浮点值

本文发布于:2023-11-30 04:10:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浮点   最小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!