Visual C++ 上的 _nextafterf

Posted

技术标签:

【中文标题】Visual C++ 上的 _nextafterf【英文标题】:_nextafterf on Visual C++ 【发布时间】:2011-05-05 12:44:36 【问题描述】:

我需要在 Visual C++ 32 位上使用函数 _nextafterf 来计算 ULP 中某些结果与参考函数相比的距离。

C99 为 float 和 double 提供了 nextafterf 和 nextafter,但 Visual C++ 不支持这些函数。但是,Visual C++ 2010 支持 _nextafterf 和 nextafter from 但仅支持 64 位...对于 32 位模型,仅支持 _nextafter 和 from ...

有没有办法让 _nextafterf 在 Visual Studio 上工作到 Visual C++ 2005?

谢谢, 克里斯托夫

【问题讨论】:

不了解 C++。我听说 Visual Studio C 编译器不是 C99 编译器。因此,如果您需要 C99 功能,请切换编译器(或向您的编译器制造商施加压力,以在下一版本中包含您想要的功能)。 【参考方案1】:

在 Boost.Math 中有一个等效的模板化 nextafter,请参阅 Finding the Next Representable Value in a Specific Direction (nextafter)。

C99 函数必须使用后缀 f 和 l 区分 float 和 long 双版本。 C++ 使用模板 代替机制。

【讨论】:

【参考方案2】:

你总是可以实现它...

https://www.google.com/search?q=libm%2Fsrc%2Fs_nextafterf.c

【讨论】:

很遗憾,Google 代码搜索不再存在。这个答案不再有太大帮助了。 @Slavik81 - 您可以使用 code.ohloh.net(以前的 koders.com)。我唯一的抱怨是您必须复制和粘贴才能获取项目的 URL。 code.ohloh.net/search?s=nextafterf.c 导致 github.com/cloudius-systems/osv/blob/master/libc/math/… (并且该目录包含许多其他数学函数)。从提交信息来看,这个文件似乎来自 musl,它使用 MIT 许可证:git.musl-libc.org/cgit/musl/tree/COPYRIGHT【参考方案3】:

在这里找到代码:

/*
 *  nextafterf.c
 *  cLibm
 *
 *  Created by Ian Ollmann on 6/14/07.
 *  Copyright 2007 Apple Inc. All rights reserved.
 *
 */
...
float nextafterf( float x, float y )

union float f; uint32_t u; ux =  x ;
uint32_t    step = 1;

if( y != y || x != x)
    return x + y;

if( y <= x ) // a subtraction here would risk Inf-Inf

    if( y == x )
        return y;   //make sure sign of 0 is correct

    step = -1;


//correct for the sign
int32_t signMask = (int32_t) ux.u >> 31;
step = (step ^ signMask) - signMask;

uint32_t absux = ux.u & 0x7fffffffU;

if( absux == 0 )                // zero

    ux.f = y;
    ux.u = (ux.u & 0x80000000U) + 1U;
    return ux.f;


ux.u += step;
return ux.f;

http://www.opensource.apple.com/source/Libm/Libm-315/Source/ARM/nextafterf.c

工作?

【讨论】:

以上是关于Visual C++ 上的 _nextafterf的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Mac OS X 上的 Visual Code 中调试 C++

检查 Visual C++ 断点(不停止)

无法在 Windows 7 机器上的 Microsoft Visual C++ 2010 中运行 OpenCV

包含上的 Visual Studio C++“未找到函数定义”

Visual C++ 矢量擦除会增加内存使用量?

持续集成:Visual Studio 2008 上的非托管 C++