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
2
votes
2 answers

Why tanh function from math.h library give wrong result?

#include #include #define PI 2*acos(0.0) int main(void) { double theta; theta=tanh(1/(sqrt(3.0))); printf("With tanh function = %lf\n",theta); printf("Actual value = %lf\n",PI/6.0); return 0; } Output: With tanh…
rh939
  • 55
  • 4
2
votes
3 answers

Error linking code with `floor();`, `ceil();` and `pow();`

I am coding under GNU/Linux Debian 8.5 I have a simple program. If I compile this with gcc prog.c it is OK! #include #include #include #include int main(int argc, char const *argv[]) { float _f =…
ch3ll0v3k
  • 336
  • 3
  • 9
2
votes
1 answer

C - Finding Cube root of a number

In one of my assignments, I am supposed to get values of x^2, x^4 and cube root of x in which x is from 0 - 100. So far, I have this. (Testing with 5 numbers) #include #include int powers(int n) { return (n < 0) ||…
Imtiaz Raqib
  • 315
  • 2
  • 20
2
votes
0 answers

C: How to add Ncurses and Math Flags into CMakeLists.txt

I'm importing a C project into the Clion IDE, that uses Cmake. My project uses these external libraries: Math and Ncurses. However i'm not able to get the script CMakeLists.txt working. This is the script: cmake_minimum_required(VERSION…
Ozeta
  • 321
  • 4
  • 18
2
votes
2 answers

Over 200 ~SYNTAX ERRORS~ in math.h for visual studio, which doesn't make sense

Using Visual Studio Express2013 for Windows Desktop, with a "Win32 Console Application" C++ project (I'm doing a project for a course. In order to start, I have to import all of the project files that the instructor provided - there are tons (all…
2
votes
2 answers

C: Better method for rounding down double to int

My question is; is there a functional difference between rounding a double down to an int via roundedInt = (int)unRoundedDouble and roundedInt = (int)(floor(unRoundedDouble)) ? I've seen the latter used in code, and thought that maybe it's for…
BigBadWolf
  • 603
  • 2
  • 8
  • 17
2
votes
1 answer

Use sin function of different glibc versions without compiling the entire glibc?

I want to try with different glibc's sin functions. Is their a way to use sin function of different glibc versions without compiling the entire glibc? i have downloaded different versions of glibcs on my virtual ubuntu machine. I do find the…
zell
  • 9,830
  • 10
  • 62
  • 115
2
votes
2 answers

Library math.h using fmod and own implementation

//test.cpp fmod( pow(2.0,127),467 );// Return result as 132 <-- correct answer When i using my own implementation int mod( int dividend , int divisor ){ return (dividend % divisor + divisor ) % divisor; } int a = mod ( pow(2.0,127),467 );// Wrong…
user3664490
  • 217
  • 4
  • 13
2
votes
2 answers

How does the function pow work?

After compiling the following program I get the output "2346" but was expecting "2345". #include #include int nr_cif(int a) { int k=0; while(a!=0) { a = a/10; k++; } return k; } void Nr(int…
2
votes
2 answers

math.h pow vs manual power performance

I just was wondering how the pow function of the math.h library works, does it implement the simplest sequencial algorithm or does it use another one else? I just know the repeated squaring algorithm, which reports O(log n), maybe this is the…
dabadaba
  • 9,064
  • 21
  • 85
  • 155
2
votes
1 answer

C sinhl undefined reference when including math.h and linking libm

Hoping someone has seen this before and can provide some insight. I'm including math.h AND linking libm, using gcc 4.2.1 on FreeBSD 9.2 x86_64. Functions like sinl, cosl, tanl work, but as soon as I call sinhl and the like I'm getting warnings about…
TCCSE
  • 31
  • 3
2
votes
2 answers

Compilation Error: "pointer can only be subtracted from another pointer"

Just learning C++, and I'm enjoying making my first program it's not much, it just solves math problems I'm having some issues with the distance formula as far as I know my logic is right sqrt((x2-x1)+(y2-y1)). However I am getting the error error…
John Snow
  • 87
  • 11
2
votes
2 answers

Is maths library included in the glibc now?

when I try to compile this simple code from terminal: #include int main(void) { printf("%f\n",sqrt(10)); return 0; } using gcc main.c command, it gets compiled and a.out gives correct answer. It means that that maths…
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
2
votes
1 answer

math.h functions in fixed-point (32,32) format (64-bit) library for C

I'm looking for a 64-bit fixed-point (32,32) library for one of my C implementations. Similar to this one http://code.google.com/p/libfixmath/ Need support for standard math.h operation. Did anyone see such implementations?
2
votes
2 answers

What is the definition for gamma(double x) and why is it different on two gcc versions?

Through unfortunate circumstances I have discovered that my standard library implementations and (C++) apparently contain a definition for a function with a prototype like: double gamma(double x); Though I do not see it listed…
NoahR
  • 1,417
  • 3
  • 18
  • 33