What is the library for mathematical calculation in C? - c

How can i use square?
For example below calculation:
2^4=16
But when i do this the result is:6
Is there any library for this calculation without using multiplication?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int c,a=2,b=4;
c=a^b;
printf("%d",c);
return 0;
}
and How i can use radical in C?

In C, ^ is the XOR operator. 2 XOR 4 does indeed equal 6.
You should include the math.h header file:
#include <math.h>
double pow( double base, double exp );
...
x = pow(2, 4);

^ in C is the XOR operator.
Use pow() for what you want to achieve, like;
c = pow(a, b);
When linking your program you might need to reference the math library, by adding the linker option -lm.
Referring the update to your question: Use sqrt():
double x = sqrt(4); /* Results in 2. */
Or simply use:
... = pow(4, 1./2.); /* Also results in 2. */
or
double y = pow(8, 1./3.); /* To also find 2. */
In general this formular applies:
Programmatical this can be done by:
/* Return n-th radical root of the m-th power to x. */
double nrt_mpow(doubel x, double m, double n)
{
return pow(x, m/n);
}
/* Return n-th radical root of x. */
double nrt(double x, root n)
{
return nrt_mpow(x, 1, n):
}
Or alternativly implemented as macros:
#define NRT_MPOW(x, m, n) pow((x), (m)/(n))
#define NRT(x, n) NRT_MPOW((x), 1., (n))

The header to the math library is:
#include <math.h>
And instead of ^, C uses pow().
c = pow(a,b);

The operator ^ is not power in C language. You should use pow(a,b).

There is a header file math.h. You can use numerous mathematical function which are written there.
pow() function of math.h is the tool you want for exponentiation.

Related

Initializing 2D array with math declaration [duplicate]

This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
Closed 7 years ago.
So in python, all I have to do is
print(3**4)
Which gives me 81
How do I do this in C? I searched a bit and say the exp() function, but have no clue how to use it, thanks in advance
You need pow(); function from math.h header.
syntax
#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
Here x is base and y is exponent. result is x^y.
usage
pow(2,4);
result is 2^4 = 16. //this is math notation only
// In c ^ is a bitwise operator
And make sure you include math.h to avoid warning ("incompatible implicit declaration of built in function 'pow' ").
Link math library by using -lm while compiling. This is dependent on Your environment.
For example if you use Windows it's not required to do so, but it is in UNIX based systems.
you can use pow(base, exponent) from #include <math.h>
or create your own:
int myPow(int x,int n)
{
int i; /* Variable used in loop counter */
int number = 1;
for (i = 0; i < n; ++i)
number *= x;
return(number);
}
#include <math.h>
printf ("%d", (int) pow (3, 4));
There's no operator for such usage in C, but a family of functions:
double pow (double base , double exponent);
float powf (float base , float exponent);
long double powl (long double base, long double exponent);
Note that the later two are only part of standard C since C99.
If you get a warning like:
"incompatible implicit declaration of built in function 'pow' "
That's because you forgot #include <math.h>.
For another approach, note that all the standard library functions work with floating point types. You can implement an integer type function like this:
unsigned power(unsigned base, unsigned degree)
{
unsigned result = 1;
unsigned term = base;
while (degree)
{
if (degree & 1)
result *= term;
term *= term;
degree = degree >> 1;
}
return result;
}
This effectively does repeated multiples, but cuts down on that a bit by using the bit representation. For low integer powers this is quite effective.
just use pow(a,b),which is exactly 3**4 in python
Actually in C, you don't have an power operator. You will need to manually run a loop to get the result. Even the exp function just operates in that way only. But if you need to use that function, include the following header
#include <math.h>
then you can use pow().

Lowest Common Multiple with doubles in C

I am doing an assignment for a Coursera class that asks me to calculate the Lowest Common Multiple of two numbers, either of which are no larger than 2 * 10 ^ 9. I'm writing this in C and I'm running my code on a test case with the numbers 226553150 and 1023473145. The answer is 46374212988031350, but I'm getting 46374212988031344, which is off by 6!
I've written a correct solution in Python that uses essentially the same approach as the one I've posted below, but the details of numeric precision are obviously taken care of for me. I post this to SO to learn something about floating point precision in C, and because most of the questions I've seen on the internet and SO regarding LCM deal only with integers.
Here is my code, which I'm compiling with gcc -pipe -O2 -std=c11 lcm.c:
#include <stdio.h>
#include <math.h>
double gcd(double a, double b) {
if (b == 0) {
return a;
}
return gcd(b, fmod(a,b));
}
double lcm(double a, double b) {
return (a * b) / gcd(a,b);
}
int main() {
double a;
double b;
scanf("%lf %lf", &a, &b);
printf("%.0lf\n", lcm(a,b));
return 0;
}
The closest number to 46374212988031350 that can be represented by a double is 46374212988031352 (off by 2). You can test that by using the most straight forward calculation.
#include <stdio.h>
int main() {
// The gcd of 226553150 and 1023473145 is 5
double a = 226553150;
double b = 1023473145;
double c = b/5;
double lcm = a*c;
printf("lcm: %.0lf\n", lcm);
return 0;
}
Output:
lcm: 46374212988031352
You are making things worse by computing (a * b)/gcd(a, b). The closest number to 231871064940156750, (a*b), that can be represented by floating point numbers is 231871064940156736. In other words, you are losing more accuracy by computing (a*b) first.
You haven't posted the Python code you used to do the same computation. I am guessing Python is using integral types for the numbers. If I use:
a = 226553150;
b = 1023473145;
c = b/5;
lcm = a*c
print("lcm:", lcm)
I get the output:
('lcm:', 46374212988031350)
However, if I use floating point literals for a and b, I get a different output:
a = 226553150.0;
b = 1023473145.0;
c = b/5;
lcm = a*c
print("lcm:", "%18.0lf" % lcm)
Output:
('lcm:', ' 46374212988031352')
In summary, the difference you are seeing between the C program and the Python program is due to use of floating point types vs integral types. If you use long or long long instead of double, you should get the same output as the Python program, as shown in the answer by Deng Haijun.
I doubt why you use float not long. I change float to long as following, then it works fine.
#include <stdio.h>
#include <math.h>
long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a%b);
}
long lcm(long a, long b) {
return (a * b) / gcd(a,b);
}
int main() {
long a;
long b;
scanf("%ld %ld", &a, &b);
printf("%ld\n", lcm(a,b));
return 0;
}

Mathematical task with point numbers

I just started to study about C Programing.
I want to write a program to solve this mathematical task (1/2*r2*3.14)
This is the code that I wrote:
#include <stdio.h>
#include <conio.h>
main()
{
int r=5;
float sum;
sum = 1/2*r^2*3.14;
printf("%f", sum);
getch();
}
but there is an error and I don`t know what the mistake is.
First I thought that there is something wrong about the number 3.14, but when I changed it to 3 the program ran but the answer was 6.0000 but it should be 37.5
In C there is no operator for power calculation. ^ is used as XOR operator. You need to use library function pow for power calculations.
sum = 1.0 / 2 * pow(r,2) * 3.14;
Note that I changed 1/2 to 1.0/2 because 1/2 will always give 0 and the result you will get is 0.
^ is bitwise XOR operator. You have to use pow() for your purpose
sum = 1.0/2.0*pow(r,2)*3.14;
Your code will give you 6.000. Because ^ is using as xor operator
1/2*r^2*3 = (0)d ^ (6)d = (000)b ^ (110)b = (110)b = (6)d
But, 3.14 instead of 3 will give you error
1/2*r^2*3.14
Because, Xor operator don't take double as operand
The ^ operator is not for power raising you can write it explicitly
#include <stdio.h>
#include <conio.h>
main()
{
int r=5;
float sum;
/* 1/2 * r^2 * pi <- this is the expression */
sum = 1.57 * r * r;
printf("%f", sum);
getch();
}
the expression has no meaning in the program except for someone reading it, so you can add a comment and write the values directly.
And in case you are going to raise to a higher power just use the pow() function.
Also, if you skip the .0 the compiler assumes the values as integers, and 1/2 is 0.5 truncated it yeilds 0, 1./2 would also work, but not 1/2.

Why do I receive this error when generating a random float?

Trying to generate a psuedo-random float btwn 0 and 1. Getting this error when I run the code:
‘RAND_MAX’ undeclared (first use in this function)
#include <stdio.h>
#include <time.h>
int main(test, numpoints, numtrials, dimension){
//generate a random number
srand(time(NULL));
float r = (float)(rand()/RAND_MAX +1);
printf("%.6f", r);
printf("\n");
}
To use RAND_MAX in C, you need to include its header:
#include <stdlib.h>
rand()/RAND_MAX is an integer division, if you want floating division, you should covert it to float first, then divide: (float)(rand()) / RAND_MAX
You need to define your main as:
int main ( int arc, char **argv )
RAND_MAX is within stdlib. You'll need to include it.
#include <stdlib.h>
(rand() / RAND_MAX) is an integer division, and will equal zero. You then add 1, and then convert to float, giving you 1.0. If you want floating division, you have to covert first, then divide: (float)(rand()) / RAND_MAX . . .
Plus, you're not including stdlib, and your arguments to main are wrong.
Try importing stdlib.h. Thats what is needed in C++ but I am not entirely sure about C.

To the power of in C? [duplicate]

This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
Closed 7 years ago.
So in python, all I have to do is
print(3**4)
Which gives me 81
How do I do this in C? I searched a bit and say the exp() function, but have no clue how to use it, thanks in advance
You need pow(); function from math.h header.
syntax
#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
Here x is base and y is exponent. result is x^y.
usage
pow(2,4);
result is 2^4 = 16. //this is math notation only
// In c ^ is a bitwise operator
And make sure you include math.h to avoid warning ("incompatible implicit declaration of built in function 'pow' ").
Link math library by using -lm while compiling. This is dependent on Your environment.
For example if you use Windows it's not required to do so, but it is in UNIX based systems.
you can use pow(base, exponent) from #include <math.h>
or create your own:
int myPow(int x,int n)
{
int i; /* Variable used in loop counter */
int number = 1;
for (i = 0; i < n; ++i)
number *= x;
return(number);
}
#include <math.h>
printf ("%d", (int) pow (3, 4));
There's no operator for such usage in C, but a family of functions:
double pow (double base , double exponent);
float powf (float base , float exponent);
long double powl (long double base, long double exponent);
Note that the later two are only part of standard C since C99.
If you get a warning like:
"incompatible implicit declaration of built in function 'pow' "
That's because you forgot #include <math.h>.
For another approach, note that all the standard library functions work with floating point types. You can implement an integer type function like this:
unsigned power(unsigned base, unsigned degree)
{
unsigned result = 1;
unsigned term = base;
while (degree)
{
if (degree & 1)
result *= term;
term *= term;
degree = degree >> 1;
}
return result;
}
This effectively does repeated multiples, but cuts down on that a bit by using the bit representation. For low integer powers this is quite effective.
just use pow(a,b),which is exactly 3**4 in python
Actually in C, you don't have an power operator. You will need to manually run a loop to get the result. Even the exp function just operates in that way only. But if you need to use that function, include the following header
#include <math.h>
then you can use pow().

Resources