cmath

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

好久不见. 提交于 2019-12-19 06:55:35
问题 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++

Ambiguous call to abs

痴心易碎 提交于 2019-12-19 05:47:47
问题 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

Why a+=b*pow(10,c-i-1) == 99 c++? [duplicate]

南笙酒味 提交于 2019-12-19 04:46:32
问题 This question already has answers here : Why pow(10,5) = 9,999 in C++ (8 answers) Closed 12 months ago . I wrote this code and first time of loop result is 99. Why is result 99, when it should be 100? #include <iostream> #include<math.h> using namespace std; int main () { int skt = 0; int sk[3]; int nsk = 3; sk[0]=1; sk[1]=2; sk[2]=8; for (int i=0; i<nsk; i++) { skt = skt + (sk[i]*pow(10.0,nsk-i-1)); cout <<" "<<skt<<endl; } } the result of this code 99 119 127 ,but if I use library cmath it

Ambiguous overload call to abs(double)

独自空忆成欢 提交于 2019-12-18 10:15:11
问题 I have the following C++ code: #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) ) { ... } and make blows up on: error: call of overloaded 'abs(double)' is ambiguous also of interest: /usr/include/stdlib.h:785: note: candidates are: int abs(int) How can I specify that the compiler needs to call the abs() in cmath.h that can handle floats? Compiler info (Not sure if this

GCC C++ pow accuracy

被刻印的时光 ゝ 提交于 2019-12-18 05:44:19
问题 So i was in a computing contest and i noticed a weird bug. pow(26,2) would always return 675, and sometimes 674? even though correct answer is 676. These sort of errors also occur with pow(26,3), pow(26,4) etc After some debugging after the contest i believe the answer has to do with the fact int rounds down. Interestingly this kind of error has never occured to me before. The computer i had was running mingw on windows 8. GCC version was fairly new, like 2-3 months old i believe. But what i

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

心不动则不痛 提交于 2019-12-18 01:52:15
问题 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 <cmath> Here is an extract from the this page (which deals with ::abs , not 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>

cos returns wrong values?

巧了我就是萌 提交于 2019-12-17 17:07:24
问题 I have a strange problem with the standard cos function of cmath/math.h. Apparently under some circumstances it returns a wrong or simply undefined value. #include <cmath> #include <iostream> int main() { double foo = 8.0 * 0.19634955; // 1.5707964 double bla = std::cos(foo); // should be 0.9996242168245 std::cout << bla << std::endl; // cos returns -7.32051e-008 return 0; } If the input value for cos is 1.5707964 for example, cos returns -7.32051e-008 (when using doubles, with floats it's -4

Rounding error of std::cbrt?

我的梦境 提交于 2019-12-13 09:30:47
问题 I wonder if the following should be reported as a bug in gcc implementation of standard library. For all unsigned integers i , if we compare int(std::sqrt(i)) to the actual square root of the integer, the conversion always give the good result. If we do the same with std::cbrt it's not the case : // Problem of rounding of std::cbrt for i from 0 to 100 million // i, exact cbrt(i), result of int(std::cbrt(i)) 2197, 13, 12 17576, 26, 25 24389, 29, 28 140608, 52, 51 185193, 57, 56 195112, 58, 57

How to avoid getting imaginary/complex number python

我的未来我决定 提交于 2019-12-13 09:13:33
问题 I am using a python code, where one of the equations got sqrt root of negative value. I use cmath.sqrt to solve it. All answers I got from that equation are shown in imaginary/complex number (e.g. x.xxxxx j ). I don't want to get that imaginary/complex number as the code that I use subsequently cannot read those imaginary/ complex number. As such, how can I avoid not to get imaginary numbers? OR in other way, how can I convert those imaginary number into real ones? or how can I remove those

cmath std::pow function giving wrong value when assigned to a variable?

痴心易碎 提交于 2019-12-13 01:28:31
问题 The method below is keeping track of how many times specific numbers come up from groupings of various sets of numbers void build_prob_distro(const std::vector<Foo>& num_sets, std::map<int, int>& prob_distro){ int key; Foo cur_foo; for(unsigned int foo_num = 0; foo_num<num_sets.size(); foo_num++){ cur_foo = num_sets.at(foo_num); key = 0; int val; for(int cur_foo_num=0; cur_foo_num<cur_foo.get_foo_length(); cur_foo_num++){ std::cout << cur_foo.get_num_at(cur_foo_num)*std::pow(10, cur_foo.get