Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Basic Math Help (http://www.chiefdelphi.com/forums/showthread.php?t=54122)

jdejoannis 16-02-2007 14:33

Re: Basic Math Help
 
Quote:

Originally Posted by Salik Syed (Post 579331)
There is no reason to use floating point numbers,
Why not do this:
Code:

current_height=
(294010* pow( (49*(Get_Analog_Value(rc_ana_in01)) - 44),-10895)  )  ) / 10000 );

You will need to use a unsigned int for the first param of the pow function
to keep it from overflowing

No, that only works for linear functions. For example:

y = 1.5x = (15x)/10

but not

y = x^2 != x^(20)/10

Hmm, one way around this is to take the log of both sides:

log y = 2 log x = (20 log x) / 10

or y = 10^((20 log x) / 10)

Be nice to your CPU and give it lookup table and don't be a precision junkie ;)

Jason

Salik Syed 17-02-2007 17:56

Re: Basic Math Help
 
Oh yeah... wow. haha ... i totally didn't take into account that POW might be non-linear. That was dumb.

Shinigami2057 17-02-2007 18:54

Re: Basic Math Help
 
Quote:

Originally Posted by Salik Syed (Post 580302)
Oh yeah... wow. haha ... i totally didn't take into account that POW might be non-linear. That was dumb.

Also, for future reference, the pow function takes a double, it does not avoid floating-point arithmetic.

From the pow(3) man page:
Code:

      double pow(double x, double y);
      float powf(float x, float y);
      long double powl(long double x, long double y);


Salik Syed 17-02-2007 21:51

Re: Basic Math Help
 
Oh okay.... I thought POW was just some function they made up

jdejoannis 19-02-2007 13:46

Re: Basic Math Help
 
Quote:

Originally Posted by Shinigami2057 (Post 580326)
Also, for future reference, the pow function takes a double, it does not avoid floating-point arithmetic.

From the pow(3) man page:
Code:

      double pow(double x, double y);
      float powf(float x, float y);
      long double powl(long double x, long double y);


Actually the MPLAB C18 Libraries Documentation defines the standard math functions, eg. sin,tan,pow in single precision.

psquared89 21-02-2007 14:24

Re: Basic Math Help
 
I'd suggest avoiding floats like the plague on these controllers; the floating point precision just is NOT there.

JimGRobot 23-02-2007 21:30

Re: Basic Math Help
 
Your question has prompted a number of different replies, and touches a wide range of topics... Of couse, I will toss in my 2 cents.

The floating point math should be working, so my first question is, "How do you know what the resulting value really is?" I ask this because the [C18] printf function is very lame about displaying floating numbers. I usually scale the number I am interested in, and cast it to an "int" in the printf function call.

In C18 a float and double are both implemented using IEEE-754 floating point standard; they are the same.

One thing to remember about floating point isn't its accuracy, but its wide dynamic range that make it handy...

The chip used in the RC has a built-in 8x8 multiply unit, which makes all math operations faster. It turns out that scaling to long data types, and multiplying and dividing with integers can actually take longer to execute than a simple, single floating point calculation.

I urge everyone to do a little test. At the start of your main code set an RC digital output high, then after it is finished clear the bit. Put an oscillscope on the output pin and see how long your code is really taking to execute. We have some fairly heavy-weight objects in our code, and it takes about one millisecond to execute. I don't think that anyone will run out of computing power for the FIRST application.

Regarding your specific problem, I would recommend unbundling the one huge equation into several smaller ones using intermediate variables along the way. This way you can use [scaled int] printf's on the values to see how the calculation progresses.

I hope this helps,
Jim


All times are GMT -5. The time now is 15:17.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi