标签: cmath

cmath.h并编译错误

我曾经没有任何问题地使用math.h.现在,我使用一个外部库,它本身有一个名为的文件math.h,但包括< cmath>.

将此库添加到我的项目中(甚至只是添加include目录,而不触及代码)现在会产生大量错误< cmath>:

C:\ Program Files\Microsoft Visual Studio 8\VC\include\cmath(18):错误C2039:'acosf':不是'`global namespace''的成员

C:\ Program Files\Microsoft Visual Studio 8\VC\include\cmath(18):错误C2873:'acosf':符号不能在using声明中使用

C:\ Program Files\Microsoft Visual Studio 8\VC\include\cmath(18):错误C2039:'asinf':不是'`global namespace''的成员

C:\ Program Files\Microsoft Visual Studio 8\VC\include\cmath(18):错误C2873:'asinf':符号不能在using声明中使用

[等等...]

我不明白为什么会这样.我正在使用Visual Studio 2005并在互联网上查看,似乎这个问题在VS 2008下得到了解决.但是,我想留在VS 2005 ...

包括using namespace std;无处不在,或改变我的包含顺序似乎没有任何改变.定义_STD_BEGIN解决了错误,但产生了多少< xlocinfo>.

怎么解决这个问题?

compiler-errors cmath visual-studio visual-c++

11
推荐指数
2
解决办法
2万
查看次数

std :: numeric_limits :: quiet_NaN()与std :: nan()vs. NAN

我很惊讶表示安静的NaN值的C++工具的数量.我发现了三种标准方式:

  1. std::numeric_limits<T>::quiet_NaN() - 通用,我认为这是被选中的
  2. std::nan,std::nanf,std::nanl-家庭的接收功能const char*参数
  3. NAN - 一个宏,"评估为一个安静的非数字"

其中每一个都是在C++ 11中引入的.我有几个问题:

  1. const char*争论代表什么std::nan和合作?如何使用?
  2. 为什么我们需要在C++ 11中添加,而我们已经为此目的添加了一个通用特征类?(是否与C兼容?)
  3. 我应该默认使用的是什么?(我想这是第一次)

floating-point nan cmath c++11

11
推荐指数
1
解决办法
4306
查看次数

在VS2010中编译旧C++代码时出现cmath编译错误

我继承了一些C++文件和一个附带的makefile,我试图将其作为解决方案引入VS2010.我创建了一个空项目,并为其中一个makefile目标添加了适当的C++和头文件(.hpp).

但是,当我尝试编译项目时,我立即从cmath获得了大量关于acosf,asinf,atanf等的C2061(语法错误标识符)错误.

cmath中的错误行:

#pragma once
#ifndef _CMATH_
#define _CMATH_
#include <yvals.h>

#ifdef _STD_USING
   #undef _STD_USING
     #include <math.h>
   #define _STD_USING

#else /* _STD_USING */
   #include <math.h>
#endif /* _STD_USING */

#if _GLOBAL_USING && !defined(RC_INVOKED)

_STD_BEGIN
using _CSTD acosf; using _CSTD asinf;
Run Code Online (Sandbox Code Playgroud)

相关C++文件的顶部块(虽然命名为.C):

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
Run Code Online (Sandbox Code Playgroud)

接下来是main()函数,它不直接调用任何trig函数.这必须是非常明显的事情,但我很想念它.有人可以帮忙吗?

谢谢!

c++ visual-studio-2010 cmath

10
推荐指数
2
解决办法
2万
查看次数

MSVC上的std :: isfinite

C++ 11和C11标准定义了 std :: isfinite 函数.的Visual Studio 2012似乎并没有提供它的一部分 cmathmath.h,但amp_math.h似乎提供了这一功能.

isfinite可以互换std::isfinite?文档没有讨论调用时的行为NAN ,我没有VS编译器来测试它.

cmath visual-c++ c++11 c11 visual-studio-2012

10
推荐指数
2
解决办法
7975
查看次数

对abs的暧昧呼唤

我有一个自定义数据类型,实际上可以是floatdouble.在除OSX之外的每个操作系统上,我都能够成功构建此C++ 11模板:

#include <cmath>
#include <cstdlib>
#include <cstdint>

template< class REAL_T >
inline REAL_T inhouse_abs(REAL_T i_val)
{
    return std::abs((REAL_T)i_val);
}

int main()
{
    int32_t ui = 2;
    inhouse_abs(ui);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,clang 6.0(3.5 LLVM)报告了一个模糊的函数调用.如果我abs改为fabs,则在OSX上解决了错误,但现在我的Linux clang,gcc和Visual Studio上出现了相同的错误.

使用fabs的Visual Studio出错:

349 error C2668: 'fabs' : ambiguous call to overloaded function
Run Code Online (Sandbox Code Playgroud)

UPDATE

这个例子在我们的OS X系统上编译,尽管在几乎相同的项目中它没有.解决方案<cstdlib>明确包含在源代码中,而不是包含在另一个头文件中.原因尚不清楚,但似乎是xcode/clang没有跟随我们的标题包括正确.

c++ macos cmath c++11

10
推荐指数
1
解决办法
2万
查看次数

为什么isnan含糊不清以及如何避免它?

既然isnan可以是宏(在C++ 98中)或在命名空间std中定义的函数(在C++ 11中),这个简单的例子说明了编写在两种情况下都有效的代码的明显(并且可能是天真的)方式.

#include <cmath>

int main() {
  double x = 0;
  using namespace std;
  isnan(x);
}
Run Code Online (Sandbox Code Playgroud)

但是,编译它会在GCC(使用-std = c ++ 11)和Clang中产生错误:

test.cc: In function ‘int main()’:
test.cc:6:10: error: call of overloaded ‘isnan(double&)’ is ambiguous
   isnan(x);
          ^
test.cc:6:10: note: candidates are:
In file included from /usr/include/features.h:374:0,
                 from /usr/include/x86_64-linux-gnu/c++/4.8/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h:426,
                 from /usr/include/c++/4.8/cmath:41,
                 from test.cc:1:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:234:1: note: int isnan(double)
 __MATHDECL_1 (int,isnan,, (_Mdouble_ __value)) __attribute__ ((__const__));
 ^
In file included from test.cc:1:0:
/usr/include/c++/4.8/cmath:626:3: note: constexpr bool std::isnan(long double) …
Run Code Online (Sandbox Code Playgroud)

c++ gcc compiler-errors cmath c++11

10
推荐指数
2
解决办法
1万
查看次数

数学接口与C++中的cMath

我的构建系统MacOS 10.6.3上用于POSIX数学库的接口是math.h,但是在我的目标系统上,接口文件的名称是cmath.h.在学校我们使用cmath,我想确保我的项目在交付时编译,这是如何实现的.学校的服务器和工作站是运行Windows XP的x86.GCC可在两个平台上使用.

c++ gcc cmath

9
推荐指数
1
解决办法
4669
查看次数

为什么pow(sqrt(-1),2)不返回-1?

相反,此操作返回-1.IND,因为sqrt(-1)返回-1.IND.C++引用中提到的域错误是否该sqrt为负值返回不会保留这实际上是i的信息吗?

是否有某种方法可以对所有负数执行此操作,以便返回正确的值,即pow(sqrt(-36), 2)返回-36?

c++ cmath

9
推荐指数
2
解决办法
640
查看次数

clang++ 不会在 macOS Catalina 中编译 cmath

我有一个简单的 C++ 程序,可以导入cmath.

#include <cmath>

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下命令编译它。

clang++ -o test main.cpp -std=c++14
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误

In file included from main.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9: error: 
      no member named 'signbit' in the global namespace
using ::signbit;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:318:9: error: 
      no member named 'fpclassify' in the global namespace
using ::fpclassify;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:319:9: error: 
      no member named 'isfinite' in the global namespace; did you mean 'finite'?
using ::isfinite;
      ~~^
/usr/local/include/math.h:749:12: note: 'finite' declared here
extern int finite(double)
           ^
In file included …
Run Code Online (Sandbox Code Playgroud)

macos clang cmath clang++ macos-catalina

9
推荐指数
1
解决办法
1913
查看次数

使用来自不同库的相同功能的不同结果

这是一些代码:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    long long int a, b;
    long long int c;

    cin >> a >> b;

    c = abs(a) + abs(b);
    cout << c;
    cout << endl;
}
Run Code Online (Sandbox Code Playgroud)

这应该返回1000000000000000002,当我输入10000000000000000002.

如果我尝试用cmath它会返回1000000000000000000,但如果我使用cstdlib它将返回1000000000000000002.为什么会发生这种情况?

还考虑到我正在使用cmath,它不应该更合适吗?

我正在使用Linux Mint 18.2 64bit,Eclipse Platform.

c++ cmath

8
推荐指数
2
解决办法
336
查看次数