Go Down

Topic: Concatenate Float w/ a String (Read 4 times) previous topic - next topic

PaulS

Why is FtoChar() defined as returning a pointer to char when it does no such thing?

pow() is a float function, not an int function. It is a bit like using an elephant gun to swat mosquitoes, anyway.

You can not expect exact integer results.

DerekErb

Whoops. You're right PaulS I originally had returned the char as a pointer rather than passing the char as an argument. That didn't work as I wanted and when I put the char in as the argument I forgot to change the returning variable to void.

I do realise that pow() is a float (double) function. However I am unaware of any other function in Arduino which would allow me to return 10 to the power of X. Obviously I can replace it with a for loop multiplying 10 by itself X number of times. But that just seems a lot less elegant and less logical to me.

If I use pow(10,2), pow(10,4), pow(10,6), ... I always get full integer results even without doing an int(pow(10,2)), ...

However if I use pow(10,iDigs) I always get a "rounded down" result regardless of the value of iDigs.

Thanks.

wildbill

Using pow with constants means that the compiler can pre-calculate it for you, presumably using non floating point math. Anything the arduino does with pow at runtime will be subject to rounding error.

DerekErb

Thanks to the notes here I've replaced my pow(10,iDigs) call to a for loop as follows:

Code: [Select]

    // Separator = 10 to the power of number of decimal places
    long dSep = 10;
    for (int i = 1; i < iDigs; i++) {
      dSep *= 10;
    }


This allows me to safely pass the number of decimal places as an argument to the function.

Any other ideas?

Arrch

#9
Feb 11, 2013, 04:09 pm Last Edit: Feb 11, 2013, 04:12 pm by Arrch Reason: 1

Any other ideas?



You could make an ipow function:

Code: [Select]
int ipow(int base, int exp)
{
 int result = 1;
 for (int i=0; i<exp; i++)
 {
   result *= base;
 }
 return result;
}



Here's the solution to the floating point problem: http://www.hobbytent.com/viewtopic.php?f=1&t=158#p29


I wouldn't exactly call it a "problem", more of a "limitation". And that seems like an awful lot of work for something that can be done with a couple lines of code.

Go Up