生成下一个最大或最小的可表示的浮点数,而无需窜改 [英] Generate next largest or smallest representable floating point number without bit twiddling

查看:237
本文介绍了生成下一个最大或最小的可表示的浮点数,而无需窜改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个任意有限的浮点数,是否有办法确定下一个可表示的浮点数?例如,给定1.0f,根据定义,下一个最大的可表示数字是1.0f + std :: numeric_limits< float> :: epsilon()。有没有一种方法来合成一个epsilon的任何价值 - 不只是1.0f - 没有诉诸点twiddling和/或机器如何表示浮点值的显式知识?在C ++ 11中,使用 std :: nextafter()。在C99系统中,你可以使用 nextafterf nextafter nextafterl double 长双倍)。


Given an arbitrary finite floating point number, is there a way to determine what the next representable floating point number? For example, given 1.0f, by definition the next largest representable number is 1.0f + std::numeric_limits<float>::epsilon(). Is there a way to synthesize an epsilon for any value - not just 1.0f - without resorting to bit twiddling and/or explicit knowledge of how the machine represents floating point values?

解决方案

In C++11, you use std::nextafter(). Lacking that, on a C99 system, you use nextafterf, nextafter, or nextafterl from the C math library (for types float, double, and long double, respectively).

这篇关于生成下一个最大或最小的可表示的浮点数,而无需窜改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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