Question

my compiler keeps giving me the error that I need to use a digit for this :

xek

where k is supposed to be the power for x using the exponent function e.g:

double x ;
for (int k = 1 ; k < 10; k++){

x = 4ek;
}
Was it helpful?

Solution

C has e notation for floating point numbers. It has special format specifier %e and %E just to print a floating point number in e notation. But neither the exponents nor mantissa can be variable. .

x = 4.0e7; is perfectly fine.

But to have variable power you need to use standard math library functions exp or pow.

x = pow(4, k);

or

x = 4 * exp(k);

The way you are using this in a loop, you can optimize it by keeping last calculated value this way: (Assuming x is initialized to 1 before loop begins.)

x = x * 4;

Every time loop runs x is multiplied by 4 hence basically at kth loop run x = 4*exp(k);.

OTHER TIPS

You need

#include <math.h>
pow(x, k);           // x to the power k
x * exp(k);          // x multiplied by e to the k
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top