Questions tagged [math.h]

For questions relating to functions declared by the math.h header file of C programming language.

Most of the mathematical functions are placed in math.h header (cmath header in C++).
math.h header file has following functions:

int abs (int x);                    
long labs (long x);

double fabs (double x);
double sin (double x);
double cos (double x);
double tan (double x);
double asin (double x);
double acos (double x);
double atan (double x);
double sinh (double x);
double cosh (double x);
double tanh (double x);

double exp (double x);              ex
double log (double x);              ln x
double log10 (double x);            log x

double pow (double x,double y);     xy
double sqrt(double x);              square root of x
double fmod(double x, double y);    x mod y

double ceil (double x);
double floor(double x);
310 questions
1
vote
1 answer

Double automatic rounding

I'm developing a program that calculates the roots of a cubic equation with Tartaglia's method. The problem is when I print the value of the imaginary part, the program rounds the value of 1.54686889 to 1.50. I have tried to use float instead of…
Murilo Pereira
  • 109
  • 2
  • 14
1
vote
0 answers

nvcc compiled program seems to be confusing math.h and cmath.h namespaces

I'm compiling a CUDA program, which seems confused (Or, perhaps I'm confused) about the namespaces used by cmath and math. When I run make, I get the following output: innovationcommons@IC-VR1:~/PackageDownloads/warp-ctc/build$ make…
user1245262
  • 6,968
  • 8
  • 50
  • 77
1
vote
1 answer

How can pow() function of C output the exact value of 2^1023 without any precision loss?

How does the following program print the exact value of 2^1023 without having any precision loss in C language? #include #include int main() { // Printing the value of 2 ^1023 printf("%.0f",pow(2,1023)); return 0; } Output:…
1
vote
2 answers

Trying to access the long double Math macros and the precision of long double

First of all I am trying to access these long double macros defined on Math.h. /* Long-double versions of M_E, etc for convenience on Intel where long- double is not the same as double. Define __MATH_LONG_DOUBLE_CONSTANTS to make these…
Duck
  • 34,902
  • 47
  • 248
  • 470
1
vote
4 answers

Save float result number to third digit, no rounding in C

How to round result to third digit after the third digit. float result = cos(number); Note that I want to save the result up to the third digit, no rounding. And no, I don't want to print it with .3f, I need to save it as new value; Example: …
Karina Kozarova
  • 1,145
  • 3
  • 14
  • 29
1
vote
0 answers

Android Studio/NDK doesn't find standard library headers (math.h)

I'm trying to build an Android Project I received that also uses C++ Code. I installed the NDK via SDK-Tools Manage, but everytime I try to compile I get the following Error Message: What went wrong: Execution failed for task…
Beeblebrox
  • 11
  • 3
1
vote
1 answer

Linking error with "math.h" - What option to tell the linker to link with it?

I'd like to use math.h in C++ Builder 10.1.2. Unfortunately, there is a linker error when I try to call one of math.h's functions. What I do already know is, that (for historical reasons) the linker must be explicitly set to link to use the math…
FlKo
  • 805
  • 1
  • 10
  • 27
1
vote
3 answers

How can I account for round-off errors in floating-point arithmetic for inverse trig (and sqrt) functions (in C)?

I have a fairly complicated function that takes several double values that represent two vectors in 3-space of the form (magnitude, latitude, longitude) where latitude and longitude are in radians, and an angle. The purpose of the function is to…
Talia
  • 1,400
  • 2
  • 10
  • 33
1
vote
3 answers

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

I wrote this code and first time of loop result is 99. Why is result 99, when it should be 100? #include #include 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;…
1
vote
3 answers

How to link Eclipse Project with -lm library for "floor" and "pow" function?

I'm trying to add the math.h library inside a C project on Eclipse IDE for C/C++. I am currently working on windows. Whenever I try to compile, I get the error, undefined reference to 'floor' and undefined reference to 'pow'. I tried looking up on…
Ahmad Shah
  • 199
  • 4
  • 14
1
vote
2 answers

Approximating atan without a math library

I've been googling around for a solution to this problem. I've seen a number of ways to calculate atan(theta) for any -1 <= theta <= 1, but I am not sure what to do when theta is bigger or smaller than those bounds. I assume I need to add or…
vgm
  • 148
  • 1
  • 2
  • 12
1
vote
1 answer

What is xmath.h for?

While trying to get some older code (probably from around 2001-2005 or so) to build, i cam across the following conditional include: #if _MSC_VER >= 1300 #include #endif I am currently building with Visual Studio 2012: Microsoft (R) C/C++…
1
vote
1 answer

Undefined first referenced symbol in file

I get this error and I'm not sure how to fix it. This is a project for information retrieval where i am trying to calculate tf-idf using this type (1+log(freq(t,n)))*log(N/k). freq(t,n) is the frequency of a word, t in file n and N is the number of…
1
vote
1 answer

Use "custom" sin and cos in glm::rotate

Is there any way of using my own sin and cos functions instead of ::std::sin and ::std::cos in glm::rotate(...) calls? The only way I can think of, is using macros to replace the sin and cos symbols inside the std namespace, but I really wouldn't…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
1
vote
2 answers

Unusual behavior of pow() in C during compilation

Program file name - armstrong3.c. #include #include int main(void) { int i, sum, num, rem,x; x=pow(2,5); printf("%d\n", x); printf("List of 3 digit armstrong numbers \n"); for (i=100; i<=999; i++) { num=i; sum=0; …
amritesh
  • 83
  • 6