cmath

Why does cmath pow give inaccurate answers?

我只是一个虾纸丫 提交于 2019-12-05 22:18:09
In C++11: pow(1061,6); // = 1426567426713180416 Check the last 3 digits. I know for sure that result is wrong, because 1061^6 = 1426567426713180361. But this other method does it right: long a =1; for(int i=0; i<6 ; i++){ a*=1061; } cout << a; // = 1426567426713180361 pow is included from cmath. If anyone knows, I'd like to know why both results aren't equal. std::pow will return double for the integral case and this answer here explains the first integral where double can not longer exactly represent every integral starts at 9007199254740993 after this we may have integers that can not be

Disable math.h crap when working with cmath [duplicate]

▼魔方 西西 提交于 2019-12-05 15:46:20
This question already has an answer here: cmath header confusion 4 answers I had a problem previously because of functions being overloaded without std:: . And the curse is still happening every now and then because I don't use using namespace std; . Removing using namespace std causes the program to get crap results Is there a way to disable all those non-std functions that come from c and only work with c++ functions under the namespace std (without having to use using namespace std; )? In other words: I want to get an error if I use sin() rather than std::sin() so that I won't do that

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

允我心安 提交于 2019-12-03 03:30:57
问题 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<iostream> #include<cmath> int main(){ int n = 13; std::cout << std::pow(-1, n) << std::endl; } What is better or the usual convention? (or something else), std::pow(-1, n) std::pow(-1, n%2) (n%2?-1:1) (1-2*(n%2)) // (gives incorrect value for negative n) EDIT: In addition, user @SeverinPappadeux

python scipy leastsq fit with complex numbers

假如想象 提交于 2019-12-02 07:51:05
I have a data set of complex numbers, and I'd like to be able to find parameters that best fit the data. Can you fit data in complex numbers using leastsq as implemented by scipy in python? For example, my code is something like this: import cmath from scipy.optimize import leastsq def residuals(p,y,x): L,Rs,R1,C=p denominator=1+(x**2)*(C**2)*(R1**2) sim=complex(Rs+R1/denominator,x*L-(R1**2)*x*C/denominator) return(y-sim) z=<read in data, store as complex number> x0=np.array[1, 2, 3, 4] res = leastsq(residuals,x0, args=(z,x)) However, residuals doesn't like working with my complex number, I

mingw C++ won't compile j0 funciton

巧了我就是萌 提交于 2019-12-02 07:33:56
I'm trying to compile a program on Windows using MingW (msys2) and it fails with the j0 function. On Linux it compiles no problem. It seems to hate when I use the -std=c++11 flag on the compiler. How can I get this to compile properly and with the -std=c++11 flag on? Sample code: #include <cmath> int main( int argc, char *argv[] ) { float test = j0( 5 ); } Output $ g++ -std=c++11 test.cpp -o test test.cpp: In function 'int main(int, char**)': test.cpp:6:21: error: 'j0' was not declared in this scope float test = j0( 5 ); Fred Larson Apparently, MinGW defines the Bessel functions only when _

Is fabsf part of the std namespace in C++11?

谁说我不能喝 提交于 2019-12-01 17:10:01
问题 The page https://en.cppreference.com/w/cpp/numeric/math/fabs mentions that std::fabsf is available since C++11. However, when I use G++ 6.3.0 to compile even the simplest program that uses std::fabsf , it says that fabsf is not a member of std . #include <cmath> int main() { return (int)std::fabsf(0.0f); } Which one is right? Is G++ 6.3.0 wrong in not including it in std , or is the above page wrong in mentioning it as part of std in C++11? And if it's G++ that is wrong, is that fixed in

finding nth root of a number by using divide and conquer method

你离开我真会死。 提交于 2019-12-01 12:10:33
I need help on how to get nth root of some number. User enters number n and number he wants root of. I need to solve this without cmath lib and with divide and conquer method. Here's my code that doesn't work yet: #include<iostream> using namespace std; float pow(float a,float c){ if (a == 0) return 0; else if(a == 1) return 1; else{ float p = pow(a,(c/2)); if(c%2) return p*p*a; else return p*p; } } int main(){ float a,b; float c; cout << "Enter positive number:(base)" << endl; do{ cin >> a; }while (a < 0); cout << "Enter number: (root)" << endl; cin >> b; c = 1/b; cout << "Result:"<<pow(a,c)

finding nth root of a number by using divide and conquer method

空扰寡人 提交于 2019-12-01 11:50:59
问题 I need help on how to get nth root of some number. User enters number n and number he wants root of. I need to solve this without cmath lib and with divide and conquer method. Here's my code that doesn't work yet: #include<iostream> using namespace std; float pow(float a,float c){ if (a == 0) return 0; else if(a == 1) return 1; else{ float p = pow(a,(c/2)); if(c%2) return p*p*a; else return p*p; } } int main(){ float a,b; float c; cout << "Enter positive number:(base)" << endl; do{ cin >> a;

When the C++ standard provides C headers bringing names into the global namespace, does that include overloads?

我只是一个虾纸丫 提交于 2019-12-01 05:14:10
The final committee draft of the upcoming C++0x standard says: Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3). Earlier C++ standards read similarly. My question is, when the C++ header #include<cname> uses overloaded functions,

Ambiguous call to abs

≯℡__Kan透↙ 提交于 2019-12-01 03:23:46
I have a custom data type that in practice can be either float or double . On every OS except OSX, I am able to successfully build this C++11 template: #include <cmath> #include <cstdlib> #include <cstdint> template< class REAL_T > inline REAL_T inhouse_abs(REAL_T i_val) { return std::abs((REAL_T)i_val); } int main() { int32_t ui = 2; inhouse_abs(ui); return 0; } However, clang 6.0 (3.5 LLVM) reports an ambiguous function call. If I change abs to fabs , the error is resolved on OSX, but now an identical error shows up on my Linux clang, gcc, and Visual Studio. Error on Visual Studio with fabs: