Tag: cmath

Constexprmath函数

所以从这个页面上注意到,在c ++ 11中没有一个math函数似乎使用了constexpr,而我相信它们都可以。 所以这给我留下了两个问题,一个是他们为什么不selectfunction。 一个像sqrt这样的函数我可以写自己的constexpr,但是像sin或者cos这样的东西会更棘手,所以围绕它就会出现。

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

我有以下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) ) { … } 并打击: error: call of overloaded 'abs(double)' is ambiguous 也感兴趣: /usr/include/stdlib.h:785: note: candidates are: int abs(int) 我怎样才能指定编译器需要调用cmath.h中的可以处理浮点数的abs()? 编译器信息(不知道这是否重要): [some_man@some_box ~/some_code]# gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure –prefix=/usr […]

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

许多algorithm需要计算(-1)^n (两个整数),通常作为一系列因子。 也就是说,对于奇数n是-1对于偶数n是1 。 在C或C ++环境中,经常会看到: #include<iostream> #include<cmath> int main(){ int n = 13; std::cout << std::pow(-1, n) << std::endl; } 什么更好或惯例? (或者是其他东西), std::pow(-1, n) std::pow(-1, n%2) (n%2?-1:1) (1-2*(n%2)) // (gives incorrect value for negative n) 编辑:另外,用户@SeverinPappadeux提出了另一种基于(全局?)数组查找的替代scheme。 我的版本是: const int res[] {-1, 1, -1}; // three elements are needed for negative modulo results const int* […]

什么时候使用晶圆厂,什么时候使用std :: abs就足够了?

我认为abs和fabs在使用math.h时行为是不同的。 但是当我只使用cmath和std::abs ,是否必须使用std::fabs或fabs ? 或者是不是这个定义?