math.h

Including files in C

*爱你&永不变心* 提交于 2019-12-03 00:27:23
问题 I want to make a simple function involving sqrt() , floor() and pow() . So, I included <math.h> . When I try to use my function, my program says that sqrt() and floor() do not exist. I've triple checked my files and rewritten them, but it still gives the same error. Just to check if there was anything wrong with the <math.h> directory, I made another separate file that calculated the same thing and it worked. I am clueless right now. What am I doing wrong? The code of the non functioning

C - Rounding issues (CS50)

巧了我就是萌 提交于 2019-12-02 15:21:17
问题 I have been Googling this for days now and I am lost. So doing CS50 online and can't seem to get a handle on this rounding of numbers. My program is messing up multiplying floats like 2.10 with integers like 100 it would output 209.xxxxxxxx Now like I say I have read countless posts on that I should use ceilf and include but I am getting an error greedy.c:(.text+0x74): undefined reference to `ceilf' collect2: error: ld returned 1 exit status make: *** [greedy] Error 1 adam@beethoven:~

How does the function pow work?

北慕城南 提交于 2019-12-02 05:30:50
问题 After compiling the following program I get the output "2346" but was expecting "2345". #include<math.h> #include<iostream.h> int nr_cif(int a) { int k=0; while(a!=0) { a = a/10; k++; } return k; } void Nr(int &a){ int k = nr_cif(a); a = a % (int)pow(10,k-1); } int main() { int a = 12345; Nr(a); cout<<a; } After debugging I noticed that it bugs out after it evaluates: a = a % (int)pow(10,k-1). Why does it break here? 回答1: It's not a very good idea to use pow for integer math. I would change

How does the function pow work?

蓝咒 提交于 2019-12-02 02:16:13
After compiling the following program I get the output "2346" but was expecting "2345". #include<math.h> #include<iostream.h> int nr_cif(int a) { int k=0; while(a!=0) { a = a/10; k++; } return k; } void Nr(int &a){ int k = nr_cif(a); a = a % (int)pow(10,k-1); } int main() { int a = 12345; Nr(a); cout<<a; } After debugging I noticed that it bugs out after it evaluates: a = a % (int)pow(10,k-1). Why does it break here? It's not a very good idea to use pow for integer math. I would change the code as follows: void Nr(int &a) { int ten_k = 1; while (ten_k < a) ten_k *= 10; a %= ten_k/10; // 10-to

C++ math functions can be used without including the directive “math.h” in VS 2013

别等时光非礼了梦想. 提交于 2019-12-01 21:42:11
问题 I am very curious why I can use the math functions in C++ without including the "math.h". I can't find an answer with google search. Here is the simple code I am executing. Everything is compiling and running. #include <iostream> using namespace std; int main() { const float PI = acosf(-1); cout << PI << endl; return 0; } 回答1: Any standard header is allowed to include any other standard header. 回答2: if you would compile the same with gcc-4.8 it would complain. Keep in mind that this is not

C++ math functions can be used without including the directive “math.h” in VS 2013

天涯浪子 提交于 2019-12-01 19:09:40
I am very curious why I can use the math functions in C++ without including the "math.h". I can't find an answer with google search. Here is the simple code I am executing. Everything is compiling and running. #include <iostream> using namespace std; int main() { const float PI = acosf(-1); cout << PI << endl; return 0; } Any standard header is allowed to include any other standard header. if you would compile the same with gcc-4.8 it would complain. Keep in mind that this is not something to rely on if you want your code to be portable and compilable on different versions of the same or

Guaranteed precision of sqrt function in C/C++

让人想犯罪 __ 提交于 2019-12-01 07:37:28
Everyone knows sqrt function from math.h / cmath in C/C++ - it returns square root of its argument. Of course, it has to do it with some error, because not every number can be stored precisely. But am I guaranteed that the result has some precision? For example, 'it's the best approximation of square root that can be represented in the floating point type used or if you calculate square of the result, it will be as close to initial argument as possible using the floating point type given`? Does C/C++ standard have something about it? For C99, there are no specific requirements. But most

Guaranteed precision of sqrt function in C/C++

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 04:33:53
问题 Everyone knows sqrt function from math.h / cmath in C/C++ - it returns square root of its argument. Of course, it has to do it with some error, because not every number can be stored precisely. But am I guaranteed that the result has some precision? For example, 'it's the best approximation of square root that can be represented in the floating point type used or if you calculate square of the result, it will be as close to initial argument as possible using the floating point type given`?

erf(x) and math.h

不羁岁月 提交于 2019-12-01 03:16:02
问题 According to this site the error function erf(x) comes from math.h. But actually looking in math.h, it isn't there, and gcc cannot compile the following test program while g++ can: #include <math.h> #include <stdio.h> int main(int argc, char* argv[]) { double x; double erfX; x = 1.0; erfX = erf(x); printf("erf(%f) = %f", x, erfX); } $ gcc mathHTest.c /tmp/ccWfNox5.o: In function `main': mathHTest.c:(.text+0x28): undefined reference to `erf' collect2: ld returned 1 exit status $ g++ mathHTest

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;