Initializing 2D array with math declaration [duplicate] - c

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().

Related

Double variable doesn't return the right values of a division [duplicate]

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 5 years ago.
input example : 356
356/100, is suppused to be 3.56
But I'm getting 3.0000000000, I'm using ideone online compiler for C.
#include <stdio.h>
#include <math.h>
int main() {
int n;
double frac;
scanf("%d", &n);
frac = (n/100);
printf("%lf", frac);
return 0;
}
That's because here frac = (n/100); you are doing plain integer arithmetic (as n is declared as an int and 100 is interpreted as an int (any whole number is taken to be an int unless specified otherwise)). What you need to do is say explicitly that you want to do an arithmetic operation with digits after decimal point. One way is to use a cast: frac = ((double) n/100);
If you don't use the cast, the division will be performed as you expect, but then the digits after the decimal point will be dropped. Since frac is declared as a double, 0s would get tacked on to the end.
In C, the result of division of two integer numbers (e.g. int, short, long) is also an integer (it is counter-intuitive, but it is implemented this way for performance reasons). As a result, the result of 5/2 is 2 and not 2.5. This rule is only for integer numbers. So, if you need to get a floating-point result, at least one of the numbers in a division operation must be of a floating-point type.
In case of your code, if you use 100.0 instead of 100, you will get the desired result. Also you can use casts or define n as double.
This should work:
#include <stdio.h>
#include <math.h>
int main() {
int n; // You can define n as double but don't forget to modify scanf accordingly.
double frac;
scanf("%d", &n);
frac = (n/((double)100)); // Or, frac = (n/100.0)
printf("%lf", frac);
return 0;
}
You cannot call division using integers to be double without declaring it.
For example
int / int will result int.
Try declaring n as double
double n;
scanf("%lf", &n);
frag = n/100;
input data type is an integer.
just change it to double or float to fix this problem.
int main() {
double n;
double frac;
scanf("%d", &n);
frac = (n/100);
printf("%lf", frac);
return 0;
}

Does pow() work for int data type in C? [duplicate]

This question already has answers here:
Strange behaviour of the pow function
(5 answers)
Closed 7 years ago.
I was simply writing a program to calculate the power of an integer. But the output was not as expected. It worked for all the integer numbers except for the power of 5.
My code is:
#include <stdio.h>
#include <math.h>
int main(void)
{
int a,b;
printf("Enter the number.");
scanf("\n%d",&a);
b=pow(a,2);
printf("\n%d",b);
}
The output is something like this:
"Enter the number. 2
4
"Enter the number. 5
24
"Enter the number. 4
16
"Enter the number. 10
99
Can't we use pow() function for int data type??
Floating point precision is doing its job here. The actual working of pow is using log
pow(a, 2) ==> exp(log(a) * 2)
Look at math.h library which says:
###<math.h>
/* Excess precision when using a 64-bit mantissa for FPU math ops can
cause unexpected results with some of the MSVCRT math functions. For
example, unless the function return value is stored (truncating to
53-bit mantissa), calls to pow with both x and y as integral values
sometimes produce a non-integral result. ... */
Just add 0.5 to the return value of pow and then convert it to int.
b = (int)(pow(a,2) + 0.5);
So, the answer to your question
Does pow() work for int data type in C?
Not always. For integer exponentiation you could implement your own function (this will work for 0 and +ve exp only):
unsigned uint_pow(unsigned base, unsigned exp)
{
unsigned result = 1;
while (exp)
{
if (exp % 2)
result *= base;
exp /= 2;
base *= base;
}
return result;
}
there is no int based pow. What you are suffering from is floating point truncation.
an int based pow is too constrained (the range of inputs would quickly overflow an int). In many cases int based pow, like in your case where its powers of 2 can be done efficiently other ways.
printf("%a", pow(10, 2)) and see what you get; I expect you'll see you don't quite get 100. Call lround if you want to round instead of truncating.
The C library function double pow(double x, double y)
It takes double type

Unexpected result from a division

I tried to implement the following calculation:
#include <stdio.h>
#include <math.h>
int main(){
float sum = 0;
int c = -4;
float x1 = 136.67;
float x2 = 2.38;
sum = c / (pow(abs(x1 - x2), 2));
printf("%f %f %f",sum,x1,x2);
return 0;
}
But the value of sum after the execution turns out to be -4. What is wrong?
abs is declared in <stdlib.h>. It takes an int argument and returns an int result.
The missing #include <stdlib.h> is likely to cause problems as well. You're calling abs with a floating-point argument; without a visible declaration, the compiler will probably assume that it takes a double argument, resulting in incorrect code. (That's under C90 rules; under C99 rules, the call is a constraint violation, requiring a compile-time diagnostic.)
You want the fabs function defined in <math.h>.
abs expects an integer, and you are providing a float. It should work if you apply an explicit type cast:
sum = c / (pow(abs((int)(x1 - x2)), 2));

What is the library for mathematical calculation in 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.

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