exponent

Hi

I have small problem with a calculation, if i hard code the formula in the program then it works but i need that i work with variables
my formula is
x = 4899e-2;
result x = 48.99

but i won't to have the main value in a variable and also the exponent like this.
int exp;
long val;
x = val e-exp;

is't probably a stupid question but im stuck in it

Don't have my arduino etc set up here at the moment, but the function you want to check out is pow().

Demo C program:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
    int number = 4855;
    int exponent = 2;
    float result;

    result = number * pow(10, -exponent);
    printf("Result: %f\n", result);
    return EXIT_SUCCESS;
}

I have assumed from your comment you want to change the sign on the exponent, hence the unary "-" in front of the exponent. Beware rounding errors - on my PC this converted 4855 to 48.54999.

You may want to look at "pow": POW - power function.

Udo

Tanks for you answer :slight_smile:

Paul J the demo program was just what i needed :slight_smile: and yes you are right about the 48.54999.

x = RegValue * pow(10, -RegExp);

Jesper

Beware rounding errors - on my PC this converted 4855 to 48.54999.

Yes now on the arduino if if the value is 4913 and the exponent i 3 i got 4.91 but it shut be 4.913 :~...

But if i have 2234 and exponent 2 the result is 22.34 and that is right.

I believe the default is to output to 2 decimal places - append a comma and a number indicating the number of decimal places?

Also, it looks like you are using the routine to move the decimal point, so pow() is probably over the top.

Here is a simpler demo program (again aimed at PC - I still don't have my arduino to hand!):

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    double number = 4855;
    double exponent = 2;
    int i;

    for (i=0; i < exponent; i++)
        number = number / 10;
    printf("Result: %f\n", number);
    return EXIT_SUCCESS;
}

If you are also moving the decimal point in the other direction, then you will need to multiply rather than divide by 10.