cmath

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

◇◆丶佛笑我妖孽 提交于 2019-12-01 01:23:52
This question already has an answer here: Why pow(10,5) = 9,999 in C++ 8 answers 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 is correct answer #include <iostream> #include<cmath> using namespace std; int main () { int skt = 0; int sk[3]; int nsk = 3;

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

南笙酒味 提交于 2019-11-30 11:04:31
Test code: #include <cmath> #include <cstdio> 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; j++) { cosine[i][j] = cos(i*j*2*PI/N); sine[i][j] = sin(-i*j*2*PI/N); } } printf("b\n"); } Here is the time: $ g++ main.cc -o main $ time ./main a b real 0m1.406s user 0m1.370s sys 0m0.030s After adding using namespace std; , the time is: $ g++ main.cc -o main $ time ./main a b real 0m8.743s user 0m8.680s sys 0m0.030s Compiler: $ g++ --version g++ (Ubuntu/Linaro 4.5.2

cmath compilation error when compiling old C++ code in VS2010

跟風遠走 提交于 2019-11-30 08:56:40
I've inherited a few C++ files and an accompanying makefile, which I'm trying to bring into VS2010 as a solution. I've created an empty project and added the appropriate C++ and header (.hpp) files for one of the makefile targets. When I try to compile the project, however, I immediately get a large number of C2061 (syntax error identifier) errors coming from cmath regarding acosf, asinf, atanf, etc. The error line in cmath: #pragma once #ifndef _CMATH_ #define _CMATH_ #include <yvals.h> #ifdef _STD_USING #undef _STD_USING #include <math.h> #define _STD_USING #else /* _STD_USING */ #include

Ambiguous overload call to abs(double)

纵饮孤独 提交于 2019-11-29 20:23:49
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 matters): [some_man@some_box ~/some_code]# gcc -v Using built-in specs. Target: i386-redhat-linux

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

这一生的挚爱 提交于 2019-11-29 16:31:33
问题 Test code: #include <cmath> #include <cstdio> 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; j++) { cosine[i][j] = cos(i*j*2*PI/N); sine[i][j] = sin(-i*j*2*PI/N); } } printf("b\n"); } Here is the time: $ g++ main.cc -o main $ time ./main a b real 0m1.406s user 0m1.370s sys 0m0.030s After adding using namespace std; , the time is: $ g++ main.cc -o main $ time .

cmath compilation error when compiling old C++ code in VS2010

吃可爱长大的小学妹 提交于 2019-11-29 14:08:21
问题 I've inherited a few C++ files and an accompanying makefile, which I'm trying to bring into VS2010 as a solution. I've created an empty project and added the appropriate C++ and header (.hpp) files for one of the makefile targets. When I try to compile the project, however, I immediately get a large number of C2061 (syntax error identifier) errors coming from cmath regarding acosf, asinf, atanf, etc. The error line in cmath: #pragma once #ifndef _CMATH_ #define _CMATH_ #include <yvals.h>

GCC C++ pow accuracy

北城余情 提交于 2019-11-29 13:37:29
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 found was that if i turned the o1/o2/o3 optimization flag on these sort of error would miraculously

How can I work around the fact that in C++, sin(M_PI) is not 0?

浪子不回头ぞ 提交于 2019-11-29 03:36:07
In C++, const double Pi = 3.14159265; cout << sin(Pi); // displays: 3.58979e-009 it SHOULD display the number zero I understand this is because Pi is being approximated, but is there any way I can have a value of Pi hardcoded into my program that will return 0 for sin(Pi)? (a different constant maybe?) In case you're wondering what I'm trying to do: I'm converting polar to rectangular, and while there are some printf() tricks I can do to print it as "0.00", it still doesn't consistently return decent values (in some cases I get "-0.00") The lines that require sin and cosine are: x = r*sin

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

﹥>﹥吖頭↗ 提交于 2019-11-28 16:51:24
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. This is an interesting question, but reading sources of efficient libraries won't get you very far unless you happen to know the method used. Here are some pointers to help you understand the classical methods. My information is by no means accurate. The following methods are only the classical ones, particular implementations can use other methods. Lookup tables are frequently used Trigonometric functions are often implemented

std::isfinite on MSVC

别说谁变了你拦得住时间么 提交于 2019-11-28 13:24:22
The C++11 and C11 standard define the std::isfinite function. Visual Studio 2012 doesn't seem to provide it as part of the cmath or math.h , but has amp_math.h which seems to provide this function . Is the isfinite interchangeable with std::isfinite ? The documentation doesn't talk about the behavior when called with NAN and I don't have a VS compiler to test this. As Marius has already pointed out, the isfinite from amp_math.h is to be used in C++ AMP, which is an MS extension for parallel computing on many-core architectures similar to CUDA or OpenCL. And since this function can only be used