Questions tagged [cmath]

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

cmath
Filter by
Sorted by
Tagged with
3 votes
2 answers
470 views

Why aren't cmath's pow and sqrt templates?

As a matter of curiosity, why are std::sqrt and std::pow overloaded just for a single type of parameter? Why are they not implemented as function/functor templates?
Maestro's user avatar
  • 2,512
0 votes
0 answers
56 views

typename specific arithmetic in the standard math library

I am just learning about templates, so I apologize if this question expresses some ignorance. I am curious if the standard math library cmath supports typename specific arithmetic. The library ...
brickbobed's user avatar
-1 votes
1 answer
333 views

How do I solve this cmath compiler error?

Im fairly new to coding. I am doing a project with function calls, and my POW() and SQRT() functions say they're not declared in this scope. I'm not particularly sure how to solve this compiler error ...
Fiona's user avatar
  • 1
0 votes
2 answers
223 views

Why is `isnan` returning 0 for a long double set to NAN?

#include <stdio.h> #include <math.h> int main() { long double x = NAN; printf("x = %Lg\nisnan(x) = %d\n", x, isnan(x)); return 0; } When I run the above program, I ...
Sparkette's user avatar
  • 1,827
0 votes
1 answer
109 views

How can I solve c++ std::rintf issue in gcc

I don't understand, why std::rintf returns the following results (g++ 7.3.0): cout << rintf(14.5f) << endl; cout << rintf(15.5f) << endl; cout << ...
FERcsI's user avatar
  • 398
-1 votes
1 answer
3k views

How to find Largest Factor of a Number (other than itself) in c++

I am trying to find largest factor of given number its simple but don't why my code not giving correct output: #include<iostream> #include<math.h> #include <bits/stdc++.h> using ...
coderinsight's user avatar
2 votes
1 answer
460 views

At what point will the atan2 function reach a domain error for a zero value in c++?

If I have a function similar to this: c=atan2( a, 0 ) I am trying to figure out what the a value should be limited to in order to ensure atan2() does not result in any domain errors. So the question ...
masked's user avatar
  • 81
0 votes
1 answer
905 views

Does the cmath macro INFINITY always works for double?

I'm using the cmath macro INFINITY with double numbers and I've not seen any problem so far: in my experience it has always evaluated to double infinity. For instance: #include <iostream> #...
Myvh's user avatar
  • 23
0 votes
1 answer
408 views

cmath won't build with cmake on MacOS Catalina

I recently installed MacOS Catalina (10.15.6) with Xcode 11.6 and Command Line tools. I am attempting to build a C++ library that uses Boost::Python using the below cmakelists.txt: ...
Sal's user avatar
  • 655
0 votes
1 answer
618 views

Multiple issues with math.h on macOS catalina after upgrade

I have updated system to macOS catalina 10.15.6 and I was hitting issue described and solved by https://stackoverflow.com/a/61519060/3616359. I was hitting issues like: /usr/local/include/boost/math/...
user3616359's user avatar
1 vote
1 answer
846 views

cmath is throwing errors C2062, C2059, C2143 and C2447. cmath is included in the vector file

I have this old C++ project I'm compiling. It wasn't written by me. I'm attempting to compile it for someone I know. The code dates way back to the early 1990s, late 1980s, though some of it was ...
John Alway's user avatar
2 votes
1 answer
291 views

How to called special functions in the cmath library in C++?

I am trying to define a function that requires me to make use of something called an "Associated Laguerre Polynomial". It's listed here under the library. In visual studio code, ...
Felix Hunter's user avatar
0 votes
0 answers
41 views

Debugging a logic error in a Denary to binary converter (C++ 17)

So I have been working on a Denary to Binary convertor in C++ and I keep getting an error with the output statement. cin >> entered_number; bit_size = log(entered_number) / log(2); ...
Antarip Kashyap's user avatar
2 votes
2 answers
952 views

How to count decimal digits of an integer?

I wrote a program to determine the number of digits in a number #include <iostream> #include <cmath> using namespace std; int main() { long long num1 = 1999999999999999; long ...
cuong.pq's user avatar
  • 157
1 vote
1 answer
1k views

What is the time complexity of log10 function in cmath?

What is the time complexity of log10 function in cmath ? Its nowhere mentioned on the internet. Does anyone know for sure ? Later edit: My initial question was if the following code is faster. int ...
royer's user avatar
  • 35
1 vote
0 answers
364 views

cmath import problem Mac OS Catalina and PCL

When using cmath in Mac OS Catalina along with PCL Library I have to do the change proposed in https://stackoverflow.com/a/61519060/6131111 2. Using #include</Applications/Xcode.app/Contents/...
Hector Esteban's user avatar
1 vote
1 answer
748 views

"fpclassify': ambiguous call to overloaded function

I have a very similar problem to this one: How to resolve "fpclassify': ambiguous call to overloaded function I have a large project with about 100 cpp-files and 100 header-files that work ...
KGB91's user avatar
  • 629
3 votes
1 answer
2k views

How to resolve "fpclassify': ambiguous call to overloaded function

I am as new to C++ as someone can be. I have been given the task of installing a simulation, but I keep running into error codes. I was able to resolve most of them, but there is one I am not sure how ...
patrick7's user avatar
  • 386
0 votes
1 answer
257 views

c++ program doesn't execute with `#include<cmath>`

I'm writing a program for my class that solves a few problems one after another. This part: #include <iostream> #include <fstream> #include <cmath> using namespace std; int main() ...
Ovkorz's user avatar
  • 13
1 vote
0 answers
204 views

round() and round() and round()

Why is the cmath library is so slow in terms of rounding (round, ceil, floor, trunc)? We are talking about a factor of 10 compared to SSE (roundsd, cvtsd2si) or good old FPU (FIST(P)), the latter ...
Lois's user avatar
  • 31
2 votes
2 answers
2k views

How is it possible to use pow without including cmath library

I'm trying to learn C++ and I'm using MS Visual Studio 2019. I have the code below: #include <iostream> int main() { std::cout << pow(10, 2); } How is it possible to compile and run ...
Liviu's user avatar
  • 33
11 votes
1 answer
3k views

clang++ doesn't compile cmath in macOS Catalina

I have a simple C++ program that imports cmath. #include <cmath> int main() { return 0; } I'm trying to compile it using the following command. clang++ -o test main.cpp -std=c++14 But, ...
Carpetfizz's user avatar
  • 8,939
0 votes
0 answers
220 views

Debugger and Integrated terminal gives different output for the c++ code

The following code's output is different when my integrated terminal is used and when the online editor (GeeksForGeeks)is used. It works perfectly fine on my IDE (Vscode, MingGW64 g++(8.1.0)). For ...
Chinmay Vemuri's user avatar
1 vote
1 answer
738 views

std::inner_product to calculate the standard deviation of a vector

I'm trying to create a function to calculate the standard deviation. I tried using std::inner_product and lambda expressions for op1 and op2 of inner_product to modify the operations of the std::...
shyney's user avatar
  • 77
1 vote
2 answers
43 views

C++ trying to have user input values for function calls via getline function

I am trying to use class to create a program, that with user input, can perform the operations of the Pythagorean theorum, but i get this error: Error (active) E0304 no instance of overloaded ...
N Sandhar's user avatar
0 votes
2 answers
303 views

How to stop c++ from rounding to closest integer

I'm doing an area of a cylinder calculator and for some reason, c++ is rounding to the nearest number , here's my code: int volume() { int radius; int height; double long volume; ...
The Capitalist Gamer's user avatar
1 vote
2 answers
917 views

Why is my program returning a garbage value when k == 0 in the for-loop?

This is a simple C++ program which computes f(x) using a formula for a fixed number of values between 2 given numbers. There seems to be some problem when k = 0 in the for loop. It is returning a ...
Vaibav79's user avatar
  • 207
0 votes
0 answers
67 views

why do the trigonometry functions in c++ skip some values? [duplicate]

I am working on a trigonometric functions calculator. The user would choose a function for example sin() then input a constant for example 1, then a lower boundary and an upper boundary. The ...
saiffarid's user avatar
0 votes
1 answer
306 views

Does cmath borrow it's implementation from math.h

I want to switch from math.h to cmath for an embedded application. I am a bit skeptical about switching the from math.h to cmath lib, as this embedded system is quiet sensitive to change in precision ...
Deepak Kiran's user avatar
0 votes
2 answers
268 views

Standard Deviation Formula

I have this assignment in which i will show the prompt for it: Write a program that reads in a set of test score data from the user. (Test scores should be entered in decimal form to represent the ...
Larry the Guy's user avatar
1 vote
1 answer
373 views

Xcode 11 project on MacOS not to use sin and cos in one function: undefined symbol "___sincosf_stret"

Building my libraries on Mac suddenly started to fail with Xcode Version 11.3 (11C29). I was able to isolate one problem: It seems that when you use sin and cos in the same function, the optimizer ...
Rudi Bartels's user avatar
0 votes
1 answer
771 views

C++ Cmath.h issue leading to variables not being in theglobal namespace

When running C++ in visual studio, I'm trying to write a simple Hello World command. However, when I do, I keep getting this issue. 1>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\...
user avatar
1 vote
1 answer
1k views

Brew clang++ issue with cmath in OSX Catalina

Having a recurring issue with cmath upon compiling the source R package in OSX 10.15, as follows; In file included from /usr/local/opt/llvm/bin/../include/c++/v1/complex:245: /usr/local/opt/llvm/bin/....
akh22's user avatar
  • 681
1 vote
1 answer
747 views

Print precise value of pow() function in c++

my output is coming wrong. I guess i'm wrong with casting. please help me out. int n; cin>>n; unsigned long long int a,s; cin>>a; s=(2*pow(10,n)+a); But when I am ...
Tejas Jadhav's user avatar
3 votes
1 answer
177 views

std::complex<> in C++ math special functions: technical specification or proposal

In C++17, mathematical special functions (such as Bessel functions, for example) were added to the standard and are already relatively well supported by modern compilers. However, those functions are ...
Anton Menshov's user avatar
1 vote
0 answers
175 views

c++ calculation of long doubles giving inf or zero

so I created a program that takes in two long double numbers and some a particular form of calculation on them. but the issue is, the output for me is inf or in some compilers, 0.. So the error only ...
Jim Kelly's user avatar
5 votes
1 answer
168 views

cmath and y0 name of variable not compiling

I got variable named y0 and included cmath, when compiling it with g++ without any args, I get a compilation error error: invalid operands of types ‘double(double) throw ()’ and ‘double’ to binary ‘...
Егор Лебедев's user avatar
-2 votes
1 answer
120 views

How can I fix the nan error in this triangle program?

I get the following output: This program is designed to aid in the analysis of triangles. Would you like to specify the coordinates of a triangle (C), or the lengths of their ...
DXMtripper's user avatar
2 votes
1 answer
357 views

Best way calculating remainder on floating points

Question What is the computational cost of the remainder function, is there a specific instruction to calculate it in a cheap way in a specific case? Description I need to transform a mathematical ...
DanielTuzes's user avatar
  • 2,604
0 votes
1 answer
45 views

c++, problems with cin? [closed]

I wrote a small test to figure out the fastest mathematic operation for a special x. I wanted the x to be entered by the user, so that I can run the tests for different x. In the following code I ...
Riceflow70's user avatar
0 votes
4 answers
113 views

Result of calculation is always 0

I'm simulating a loan payment calculator and I'm sure I used the right equation and data types. Am I missing a data type conversion or something? Am I doing something that isn't allowed in C++? I've ...
MillyP1's user avatar
  • 13
4 votes
1 answer
2k views

c++ gcc error: 'sqrtl' is not a member of 'std'

When I try to compile the following program, the compiler gives error: 'sqrtl' is not a member of 'std'. #include <cmath> #include <iostream> int main(){ std::cout << std::sqrtl(...
SmileyCraft's user avatar
1 vote
1 answer
128 views

Issues with cmath in 3D plotting using Matplotlib

I have this simple code which tries to get a 3D plot of the real parts of two complex numbers E1 and E2 as functions of t and g. import numpy as np import matplotlib.pyplot as plt from mpl_toolkits....
hbaromega's user avatar
  • 2,342
-1 votes
1 answer
444 views

The asin() function from cmath library returns inaccurate values

I'm using the asin and acos functions from cmath library to find the angles from their corresponding sine and cosine values but it sometimes returns inaccurate values. For example the result of the ...
therealak12's user avatar
  • 1,242
0 votes
1 answer
251 views

How to increase decimal precision with 'exotic' functions?

I can't figure out how to specify my decimal precision for logs, importing decimal and setting the context does not affect the log functions. from decimal import * getcontext().prec = 54 print(...
user7778287's user avatar
1 vote
1 answer
346 views

Compile-time optimization for `std::isfinite()` applied to the integral types

I have a design choice to make. I have a templated class myClass<T> that has a member data_ of the type T. Currently, the intention is to support int, several complex types, and double; thus ...
Anton Menshov's user avatar
0 votes
1 answer
348 views

Solving for x in a complex trigonometric equation

I need to write a script that solves for a variable inside a complex numbers-trigonometric equation, how do i solve it? So far I tried using the Eq function from sympy to input the equation from ...
Juan Camilo Andrade's user avatar
1 vote
1 answer
159 views

Why is std::remquo so slow?

In some inner loop I have: double x; ... int i = x/h_; double xx = x - i*h_; Thinking that might be a better way to do this, I tried with std::remquo double x; ... int i;...
alfC's user avatar
  • 15.2k
0 votes
0 answers
178 views

having trouble using nextafter C++ stl

This seems a bit strange to me, so please let me know if I am doing something wrong. I need to use the std::nextafter function. As explained here: https://en.cppreference.com/w/cpp/numeric/math/...
deathNode's user avatar
  • 134
18 votes
5 answers
36k views

How to fix "error: call to 'abs' is ambiguous"

I'm running a simple C++ program from HackerRank about pointers and it works fine on the website. However, when I run it on MacOS, I get error: call to 'abs' is ambiguous and I'm not sure exactly what ...
AkThao's user avatar
  • 632