Questions tagged [cmath]

A C++ Math library which offers basic mathematical functions of trigonometry, exponentiation, rounding etc.

263 questions
104
votes
4 answers

When do I use fabs and when is it sufficient to use std::abs?

I assume that abs and fabs are behaving different when using math.h. But when I use just cmath and std::abs, do I have to use std::fabs or fabs? Or isn't this defined?
math
  • 8,514
  • 10
  • 53
  • 61
70
votes
1 answer

What is 1LL or 2LL in C and C++?

I was looking at some of the solutions in Google Code Jam and some people used this things that I had never seen before. For example, 2LL*r+1LL What does 2LL and 1LL mean? Their includes look like this: #include #include…
fersarr
  • 3,399
  • 3
  • 28
  • 35
69
votes
3 answers

Constexpr Math Functions

So noticed from this page that none of the math functions in c++11 seems to make use of constexpr, whereas I believe all of them could be. So that leaves me with two questions, one is why did they choose not to make the functions constexpr. And two…
aaronman
  • 18,343
  • 7
  • 63
  • 78
67
votes
4 answers

Ambiguous overload call to abs(double)

I have the following C++ code: #include #include // per http://www.cplusplus.com/reference/clibrary/cmath/abs/ // snip ... if ( (loan_balance < 0) && (abs(loan_balance) > loan_payment) ) { ... } and make blows up…
some_man
54
votes
7 answers

What is the correct way to obtain (-1)^n?

Many algorithms require to compute (-1)^n (both integer), usually as a factor in a series. That is, a factor that is -1 for odd n and 1 for even n. In a C++ environment, one often sees: #include #include int main(){ int n = 13; …
alfC
  • 14,261
  • 4
  • 67
  • 118
52
votes
2 answers

Why is pow(int, int) so slow?

I've been working on a few project Euler exercises to improve my knowledge of C++. I've written the following function: int a = 0,b = 0,c = 0; for (a = 1; a <= SUMTOTAL; a++) { for (b = a+1; b <= SUMTOTAL-a; b++) { c =…
Fang
  • 2,199
  • 4
  • 23
  • 44
42
votes
3 answers

What's the difference between abs and fabs?

I checked the difference between abs and fabs on python here As I understand there are some difference regarding the speed and the passed types, but my question related to native c++ on V.S. Regarding the V.S. I tried the following on Visual Studio…
Hazem Abdullah
  • 1,837
  • 4
  • 23
  • 41
37
votes
7 answers

Definitions of sqrt, sin, cos, pow etc. in cmath

Are there any definitions of functions like sqrt(), sin(), cos(), tan(), log(), exp() (these from math.h/cmath) available ? I just wanted to know how do they work.
Pythagoras of Samos
  • 3,051
  • 5
  • 29
  • 51
34
votes
2 answers

abs vs std::abs, what does the reference say?

Beware, I am talking about ::abs(), not std::abs() According to the cplusplus.com website, abs is supposed to behave differently for the stdlib.h C version, if you include Here is an extract from the this page (which deals with ::abs, not…
Pascal T.
  • 3,866
  • 4
  • 33
  • 36
29
votes
3 answers

Why are some functions in not in the std namespace?

I am developing a project which works with multiple arithmetic types. So I made a header, where the minimal requirements for a user defined arithmetic type are defined: user_defined_arithmetic.h : typedef double ArithmeticF; // The user chooses…
Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146
28
votes
5 answers

Rounding up and down a number C++

I'm trying to allow my program to round a number up and down respectively. For example, if the number is 3.6, my program is suppose to round up the nearest number which is 4 and if the number is 3.4, it will be rounded down to 3. I tried using the…
Bryan
  • 8,488
  • 14
  • 52
  • 78
25
votes
3 answers

Template version of std::abs

Here lists the current overloads of std::abs in C++. I'm wondering why not just define the following template and let go all the ugly C-style overloads? template inline T abs(const T& v) { return v < 0 ? -v : v; }
Lingxi
  • 14,579
  • 2
  • 37
  • 93
24
votes
3 answers

hides isnan in in C++14 / C++11?

I have here a small test app which uses isnan from : #include #include int main() { double d = NAN; std::cout << isnan(d) << '\n'; return 0; } Build and run under 3 different standards: $ g++ -std=c++98…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
22
votes
4 answers

Why is std::sin() and std::cos() slower than sin() and cos()?

Test code: #include #include const int N = 4096; const float PI = 3.1415926535897932384626; float cosine[N][N]; float sine[N][N]; int main() { printf("a\n"); for (int i = 0; i < N; i++) { for (int j = 0; j < N;…
ornerylawn
  • 524
  • 1
  • 4
  • 9
22
votes
5 answers

C/C++ fastest cmath log operation

I'm trying to calculate logab (and get a floating point back, not an integer). I was planning to do this as log(b)/log(a). Mathematically speaking, I can use any of the cmath log functions (base 2, e, or 10) to do this calculation; however, I will…
Nick
  • 2,821
  • 5
  • 30
  • 35
1
2 3
17 18