我认为abs并且fabs在使用时表现不同math.h.但是,当我只使用cmath和std::abs,我必须使用std::fabs或fabs?或者这不是定义?
我有以下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 ++ 11中的数学函数似乎都没有使用constexpr,而我相信所有这些函数都可以.所以这给我留下两个问题,一个是他们为什么选择不使函数constexpr.对于像sqrt我这样的函数来说,两个人可能会编写我自己的constexpr,但是像sin或cos这样的东西会比较棘手,所以它就在那里.
许多算法需要计算(-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) 我一直在做一些项目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) 我正在研究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) 有没有像功能的任何定义sqrt(),sin(),cos(),tan(),log(),exp()(这些从文件math.h/CMATH)可用?
我只是想知道它们是如何工作的.
我正在开发一个适用于多种算术类型的项目.所以我制作了一个标题,其中定义了用户定义的算术类型的最低要求:
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)
我从来没有使用过 …
当心,我在说::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) 这里列出了std::absC++ 中的当前重载.我想知道为什么不定义以下模板并放弃所有丑陋的C风格重载?
template <typename T> inline
T abs(const T& v) { return v < 0 ? -v : v; }
Run Code Online (Sandbox Code Playgroud) 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