Using Longs and floats in equations

Hi, i am trying to do something that i thought was going to be quite simple but really does not seem to be now.... i am just trying to run the below calculation, i have tried lots of different combinations with brackets but no matter what i do it seems not to like it... or is it not possible to have a number to the power of a decimal (float) number...? any help would be greatly appreciated!!

int changingInt = 3;
float constantNum = 1.56;
uint32_t constantNumLong;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  
  constantNumLong = uint32_t(constantNum); 

  Serial.println(771932220UL+((120UL-771932200UL)/(1+pow((uint32_t(changingInt)/17433UL),constantNumLong))));

  //Serial.println(771932220UL+((120UL-771932200UL)/(1+(uint32_t(changingInt)/17433UL)^constantNumLong)));
  //Serial.println(771932220UL+((120UL-771932200UL)/(1+(uint32_t(changingInt)/17433UL)^(1.56)UL)));
}

void loop() {
  // put your main code here, to run repeatedly:

}

^ is the XOR operator. XOR on a float is pretty meaningless.

Any number raised to the power 1 is . . .?

For integer powers, use multiply instead. pow() uses logarithms and exponentials, so it is slow and inaccurate.

What result do you expect when using unsigned long for this constant: (120UL-771932200UL)?

Putting a float constantNum = 1.56; into a constantNumLong = uint32_t(constantNum); results in a ??? being stored?

I found its best to do floats like so: float constantNum = 1.56f; and 1.56f, things seem to workout better in the long run.

Have a look at this part:

pow((uint32_t(changingInt)/17433UL),constantNumLong)

changingInt is 3

then you divide it by 17433

They're both int types, so the compiler uses integer arithmetic so the answer is zero.

constantNumLong is 1 at this stage.

so you're doing 0 to the power 1, which is going to be approximately zero (approximately because the pow function uses floats).

Maybe it'd be easier if you told us what you're trying to achieve because whatever it is I think you're on the wrong track with all these type casts.

Hmm ok, can see now that there was no chance that it was going to work the way that it was...

Also i had gotten myself into the mind set that all of this had to happen in "Long format" but it is just the answer that i need to be in Long as i need to subtract it from millis later in the code.

so the equation that i am trying to calculate is this one in the pic, where the 3 will change based on sensor data....

Screenshot 2020-09-15 at 19.28.48.png

Screenshot 2020-09-15 at 19.28.48.png

Please have a try at it, correcting the problems that have been solved in the above replies, then post the resulting code.

That equation can be rewritten as

771932220*(1.0 - 0.999999819/(1.0 + pow(3.0/17433.0,1.56)))

and cannot be accurately computed on an Arduino with single precision floats (six to seven digits of precision). There is undoubtedly a much better approach, but you will need to explain what you are trying to accomplish.

Also please tell what board you are using.

Thanks for the feedback, and i have not stopped trying to solve it since i posted the question, just so you don't think that i am just wanting for the answer to fall in my lap :slight_smile:

I am using a Mega.

Basically i have sensor data coming in and the higher changingInt (3 in the above example) number gets the more the result needs to change, but it is not a lineal system.

So basically (as i am sure you all know) the root of my problem (as far as i could see it) was when i was trying to divide 3 by 17433. But the little trick that i did find is that you if put the equation into Wolframalpha.com it gives you alternative forms to that equation (or equations that are very very close to it) by looking through these i was able to find one that did not have any very small numbers in it (see below) now this works as it should and i get the right numbers out, thanks for the help! :slight_smile:

Oh and i am still using the pow( ) function but can't really see how i can do it without it....

float changingInt = 3;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println((578987000 + 771932000 *pow(changingInt,1.56))/(4135620 + pow(changingInt,1.56)));

}

void loop() {
  // put your main code here, to run repeatedly:

}

It might be useful to state what this equation relates to and how the output of your sensor varies ( resolution , max/min values) ; there may be a better way .

If that “3” varies from 1 to 1000, you will barely see any change in the equation solution , the answer would always be 140