标签: cmath

我什么时候使用fabs什么时候使用std :: abs就足够了?

我认为abs并且fabs在使用时表现不同math.h.但是,当我只使用cmathstd::abs,我必须使用std::fabsfabs?或者这不是定义?

c++ math.h cmath

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

不明确的过载调用abs(双)

我有以下C++代码:

#include <math.h>
#include <cmath.h>      // per http://www.cplusplus.com/reference/clibrary/cmath/abs/

// snip ...

if ( (loan_balance < 0) && (abs(loan_balance) > loan_payment) ) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

make爆炸:

error: call of overloaded 'abs(double)' is ambiguous
Run Code Online (Sandbox Code Playgroud)

也感兴趣:

/usr/include/stdlib.h:785: note: candidates are: int abs(int)
Run Code Online (Sandbox Code Playgroud)

如何指定编译器需要在cmath.h中调用可以处理浮点数的abs()?

编译器信息(不确定这是否重要):

[some_man@some_box ~/some_code]#  gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr    /share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 …
Run Code Online (Sandbox Code Playgroud)

c++ std cmath

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

Constexpr数学函数

从这个页面中可以看出,c ++ 11中的数学函数似乎都没有使用constexpr,而我相信所有这些函数都可以.所以这给我留下两个问题,一个是他们为什么选择不使函数constexpr.对于像sqrt我这样的函数来说,两个人可能会编写我自己的constexpr,但是像sin或cos这样的东西会比较棘手,所以它就在那里.

c++ cmath constexpr c++11

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

获得(-1)^ n的正确方法是什么?

许多算法需要计算(-1)^n(两者都是整数),通常作为一系列因子.也就是说,-1奇数n和偶数n 的因子1.在C++环境中,人们经常会看到:

#include<iostream>
#include<cmath>
int main(){
   int n = 13;
   std::cout << std::pow(-1, n) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

什么是更好或通常的惯例?(或者是其他东西),

std::pow(-1, n)
std::pow(-1, n%2)
(n%2?-1:1)
(1-2*(n%2))  // (gives incorrect value for negative n)
Run Code Online (Sandbox Code Playgroud)

编辑:

此外,用户@SeverinPappadeux提出了另一种基于(全局?)数组查找的替代方案.我的版本是:

const int res[] {-1, 1, -1}; // three elements are needed for negative modulo results
const int* const m1pow = res + 1; 
...
m1pow[n%2]
Run Code Online (Sandbox Code Playgroud)

这可能不会解决问题,但是,通过使用发出的代码,我们可以放弃一些选项.

首先没有优化,最终的竞争者是:

   1 - ((n & 1) << 1);
Run Code Online (Sandbox Code Playgroud)

(7操作,无内存访问)

  mov eax, DWORD PTR [rbp-20] …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm x86 cmath

52
推荐指数
3
解决办法
3931
查看次数

为什么pow(int,int)这么慢?

我一直在做一些项目Euler练习,以提高我对C++的知识.

我写了以下函数:

int a = 0,b = 0,c = 0;

for (a = 1; a <= SUMTOTAL; a++)
{
    for (b = a+1; b <= SUMTOTAL-a; b++)
    {
        c = SUMTOTAL-(a+b);

        if (c == sqrt(pow(a,2)+pow(b,2)) && b < c)
        {
            std::cout << "a: " << a << " b: " << b << " c: "<< c << std::endl;
            std::cout << a * b * c << std::endl;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这计算在17毫秒.

但是,如果我改变了这条线

if (c == sqrt(pow(a,2)+pow(b,2)) && b < …
Run Code Online (Sandbox Code Playgroud)

c++ performance cmath pow

48
推荐指数
2
解决办法
7428
查看次数

什么是C和C++中的1LL或2LL?

我正在研究Google Code Jam中的一些解决方案,有些人使用过我以前从未见过的东西.例如,

2LL*r+1LL
Run Code Online (Sandbox Code Playgroud)

2LL和1LL是什么意思?

他们的包括如下所示:

#include <math.h>
#include <algorithm>
#define _USE_MATH_DEFINES
Run Code Online (Sandbox Code Playgroud)

要么

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

c c++ math cmath long-integer

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

在cmath中sqrt,sin,cos,pow等的定义

有没有像功能的任何定义sqrt(),sin(),cos(),tan(),log(),exp()(这些从文件math.h/CMATH)可用?

我只是想知道它们是如何工作的.

c c++ math cmath definitions

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

为什么<cmath>中的某些函数不在std命名空间中?

我正在开发一个适用于多种算术类型的项目.所以我制作了一个标题,其中定义了用户定义的算术类型的最低要求:

user_defined_arithmetic.h:

typedef double ArithmeticF;   // The user chooses what type he 
                              // wants to use to represent a real number

namespace arithmetic          // and defines the functions related to that type
{

const ArithmeticF sin(const ArithmeticF& x);
const ArithmeticF cos(const ArithmeticF& x);
const ArithmeticF tan(const ArithmeticF& x);
...
}
Run Code Online (Sandbox Code Playgroud)

令我不安的是,当我使用这样的代码时:

#include "user_defined_arithmetic.h"

void some_function()
{
    using namespace arithmetic;
    ArithmeticF lala(3);
    sin(lala);
}
Run Code Online (Sandbox Code Playgroud)

我收到编译器错误:

error: call of overloaded 'sin(ArithmeticF&)' is ambiguous
candidates are:
double sin(double)
const ArithmeticF arithmetic::sin(const ArithmeticF&)
Run Code Online (Sandbox Code Playgroud)

我从来没有使用过 …

c++ namespaces cmath

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

abs vs std :: abs,引用说什么?

当心,我在说::abs(),不是std::abs()

根据cplusplus.com网站,abs对于stdlib.h C版本应该表现不同,如果你包括<cmath>

以下是此页面的摘录(::abs不处理std::abs):

double abs (double x); 
float abs (float x); 
long double abs (long double x);
Compute absolute value
/*
Returns the absolute value of x: |x|.
These convenience abs overloads are exclusive of C++. In C, abs is only declared
in  <cstdlib> (and operates on int values). 
The additional overloads are provided in this header (<cmath>) for the integral types: 
These overloads effectively …
Run Code Online (Sandbox Code Playgroud)

c++ cmath

24
推荐指数
1
解决办法
7277
查看次数

std :: abs的模板版本

这里列出了std::absC++ 中的当前重载.我想知道为什么不定义以下模板并放弃所有丑陋的C风格重载?

template <typename T> inline
T abs(const T& v) { return v < 0 ? -v : v; }
Run Code Online (Sandbox Code Playgroud)

c++ templates cmath

24
推荐指数
2
解决办法
3779
查看次数

标签 统计

c++ ×10

cmath ×10

c ×2

math ×2

algorithm ×1

c++11 ×1

constexpr ×1

definitions ×1

long-integer ×1

math.h ×1

namespaces ×1

performance ×1

pow ×1

std ×1

templates ×1

x86 ×1