标签: cmath

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

这是一些代码:

#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
查看次数

在xcode中包含cmath get error:':: acos'尚未声明等

在尝试构建包含<cmath>在Xcode中的小而简单的项目时,我收到以下错误:

cmath: '*' has not been declared
'::acos' has not been declared
In file included from /Xcode4/Projects/libraryLAFMath/Classes/libraryLAFMath.cp
In file included from /Xcode4/Projects/libraryLAFMath/Classes/libraryLAFMath.h
'::acos' has not been declared in /Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.1.sdk/usr/include/c++/4.2.1/cmath
...

错误日志抱怨所有其他数学函数以及,sin,pow,等,而不仅仅是acos.我查看了cmath源代码内部,它引用了全局定义的相应数学函数math.h,即::acos等等.由于根错误抱怨不存在::acos一个会假设math.h无法找到,但a)它存在,并且b)我得到一个不同的错误抱怨math.h找不到.

源代码如下:

libraryLAFMath.cp:

#include "libraryLAFMath.h"
Run Code Online (Sandbox Code Playgroud)

libraryLAFMath.h:

#include <cmath>
struct libraryLAFMath {
    void test() {
        double a = std::acos(0);
    }
};
Run Code Online (Sandbox Code Playgroud)

现在,我有一个来自外部源的另一个项目,它使用cmath和编译好.我尝试比较这两个项目之间的构建设置,但它们几乎相同.我使用的是LLVM GCC 4.2编译器,但在使用GCC 4.2时获得了类似的结果,所以我认为这不是编译器设置问题.

我是Xcode开发的新手,感谢任何帮助.

c++ xcode cmath

7
推荐指数
1
解决办法
5423
查看次数

pow()实现在cmath和有效的替换

我已经阅读了通过执行来cmath计算.当它是整数时,不应该使用它,因为它会大大减慢计算速度.有什么替代方案pow(a,b)exp(b*log(a))b

  1. 用相同的常数计算很多连续的pow()sa
  2. 它是事先知道这b肯定是一个整数?

我正在寻找在这些特定情况下有效的快速替代方案.

c c++ math cmath

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

当C++标准提供C头将名称带入全局命名空间时,是否包含重载?

即将推出的C++ 0x标准的最终委员会草案说:

每个C头(每个头都具有name.h形式的名称)的行为就好像每个由相应的cname头放置在标准库命名空间中的名称放在全局命名空间范围内.未指定是在名称空间std的名称空间作用域(3.3.6)中首先声明或定义这些名称,然后通过显式使用声明(7.3.3)将这些名称注入到全局名称空间作用域中.

早期的C++标准读起来类似.

我的问题是,当C++标头#include<cname>使用重载函数时,都会引入所有重载#include<name.h>,因为重载不是单独的"名称"?

以下代码的行为应该在符合标准的C和C++编译器之间有所不同吗?

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void)
{
    double arg = -2.5;
    double result = abs(arg) / 3;
    printf("%f\n", result);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译就绪测试用例:

从这个测试来看,C++ math.h就像C一样,而不像C++ cmath.

但在Visual C++ 2010上,C++ math.h就像C++一样cmath.

Comeau试用的编译时金丝雀:

#include<stdio.h>
#include<stdlib.h>
#include<math.h> …
Run Code Online (Sandbox Code Playgroud)

c++ overloading header backwards-compatibility cmath

6
推荐指数
1
解决办法
828
查看次数

cmath在Visual Studio 2010中生成语法错误(ANSI C)

我必须做一些纯ANSI C,我试图在Visual Studio 2010中这样做.问题是; 无论将编译器设置为纯C并翻转\ Za编译器开关以禁用Windows扩展 - 代码都不会编译:

#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>

using namespace std;

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

这是一个名为Test.c的文件 - 没有预编译的头文件,一切都应该没问题,但这就是编译器吐出的内容:

1>ClCompile:
1>  Test.c
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(19): error C2061: syntax error : identifier 'acosf'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(19): error C2059: syntax error : ';'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(19): error C2061: syntax error : identifier 'asinf'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cmath(19): error C2059: syntax error …
Run Code Online (Sandbox Code Playgroud)

syntax cmath visual-studio

6
推荐指数
0
解决办法
8983
查看次数

在c ++中的命名空间std中重载数学函数是一个好习惯

我正在编写一个代表算术类型的C++类(围绕mpfr的c ++包装器),我想支持<cmath>中的一些函数(我将以std :: sqrt为例).

所以我有以下课程:

namespace ns
{
  class MyClass
  {
      /* ... */
      public:
      friend MyClass sqrt(const MyClass& mc);
  };
}
Run Code Online (Sandbox Code Playgroud)

我可以这样使用它:

MyClass c;
/* ... */
MyClass d = ns::sqrt(c);
MyClass e = sqrt(c); // Apparently I don't have to specify ns::
Run Code Online (Sandbox Code Playgroud)

但我不能这样使用它:

MyClass f = std::sqrt(c);
Run Code Online (Sandbox Code Playgroud)

编译器(g ++(Debian 4.7.2-5))错误是:"没有用于调用sqrt(ns :: MyClass&)的匹配函数".

这是正常的,但对我来说这是一个问题.我需要这个有效,因为MyClass应该用于现有的模板函数(我不应该修改).例如:

template <typename T>
void func(T a)
{
    /* ... */
    T c = std::sqrt(a);
    /* ... */
}
int main()
{
    func<float>(3);
    func<MyClass>(MyClass(3)); …
Run Code Online (Sandbox Code Playgroud)

c++ overloading std cmath

6
推荐指数
2
解决办法
1572
查看次数

Clang:找不到“cmath”文件

我正在用 clang 编译我的项目,但遇到了一个奇怪的错误:

[ 1%] Building CXX object CMakeFiles/tfs.dir/src/actions.cpp.o
In file included from /home/travis/build/dominique120/miniature-adventure/src/actions.cpp:20:
In file included from /home/travis/build/dominique120/miniature-adventure/src/otpch.h:27:
/home/travis/build/dominique120/miniature-adventure/src/definitions.h:39:10: fatal error:
'cmath' file not found
 #include <cmath>
 ^
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)

我的 actions.cpp 第 20 行:

#include "otpch.h"
Run Code Online (Sandbox Code Playgroud)

otpch.h 第 27 行:

#include "definitions.h"
Run Code Online (Sandbox Code Playgroud)

Definitions.h 第 31 行:

#include <cmath>
Run Code Online (Sandbox Code Playgroud)

我做了一些编辑,但我不知道是什么导致了这个错误,在这里编辑: https: //github.com/dominique120/miniature-adventure/commits/master

PS:GCC 只是转储大量错误: https://travis-ci.org/dominique120/miniature-adventure/jobs/21905513

c++ gcc clang cmath travis-ci

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

fabsf是C ++ 11中std名称空间的一部分吗?

https://en.cppreference.com/w/cpp/numeric/math/fabs页面提到std::fabsf自C ++ 11起可用。但是,当我使用G ++ 6.3.0甚至编译使用的最简单的程序时std::fabsf,它说fabsf也不是的成员std

#include <cmath>
int main()
{
    return (int)std::fabsf(0.0f);
}
Run Code Online (Sandbox Code Playgroud)

哪一个是对的?G ++ 6.3.0是否没有将其包含在其中std是错误的,还是上述页面将其作为stdC ++ 11的一部分提及时是错误的?

并且如果是G ++错误,那么在更高版本中是否已解决?

c++ cmath c++11

6
推荐指数
2
解决办法
422
查看次数

如何修复“错误:调用 'abs' 不明确”

我正在从 HackerRank 运行一个关于指针的简单 C++ 程序,它在网站上运行良好。但是,当我在 MacOS 上运行它时,我得到了error: call to 'abs' is ambiguous并且我不确定到底什么是模棱两可的。

我查看了类似问题的其他答案,但错误消息往往是Ambiguous overload call to abs(double),这不是我遇到的问题,因为我没有使用任何双打。我也试过包括头文件cmathmath.h,但问题仍然存在。

#include <stdio.h>
#include <cmath>

void update(int *a,int *b) {
    int num1 = *a;
    int num2 = *b;
    *a = num1 + num2;
    *b = abs(num1 - num2);
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;

    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题出现在第 8 …

c++ cmath

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

cmath和y0变量名称未编译

我得到了一个名为变量y0并包含cmath的变量,在g++不带任何参数的情况下进行编译时,出现编译错误

error: invalid operands of types ‘double(double) throw ()’ and ‘double’ to binary ‘operator+’ y = y0 + R; ~~~^~~

为了进行实验,我删除了cmath,其中包括所有内容,一切正常。

#include <iostream>
#include <cmath>

double y0, y, R;
int main() {
    std::cin >> y0 >> R; 
    y = y0 + R;
}
Run Code Online (Sandbox Code Playgroud)

c++ cmath

5
推荐指数
1
解决办法
33
查看次数