How to perform this math formula on Arduino?

Hi,
I have only been using Arduino for about 2 months and I have not quite worked out the maths stuff yet. There seem to be loads of restrictions on what you can do in terms of maths on it and this is something I have not come across before.

My formula, although not fully tested, is something like this:

Pi = 3.14159
WDist = 12
Deg = 90
IWDist = 5

2Pi2*Dist/(360/Deg)+IWDist

and should output something like 42.69911184

Apart from Pi all of the inputs will be changing.

Am I right in thinking that the Arduino will get confused with these long numbers? If is there a way to make it work? Can I do all this in one line? And should I use any libraries or is the arithmetic in the Arduino sufficient?

Thanks alot for any help. I am loving the Arduino and if I can get this to work I have a great project ahead of me :slight_smile:

Sam.

The Arduino doesn't know anything about PI, or pie, so, you'll need to define it:

const float Pi = 3.14159; // To however many digits you want.

Am I right in thinking that the Arduino will get confused with these long numbers?

No. The Arduino is perfectly capable of this arithmetic. Floating point math is not fast, so if you minimize the number of operations, the calculation will be quicker. There are 3 constants in this equation. They can be replaced with one:

((PiDistDeg)/90.0) + IWDist

OK thanks. Yes I was going to define Pi. Thanks for simplifying my formula. I thought it was a bit messy somehow but I did not realise it was that bad. I just knocked it up on Excel.

S if I use this formula, how long do you think it would take to calculate? This is part of a driving system for a chassis and if it takes more than say a second it becomes almost pointless.

Also, can I just use it like this?

const float Pi = 3.141593;
int Dist = 12;
int Deg = 90;
int IWDist = 5;
float Result = ((Pi*Dist*Deg)/90.0) + IWDist;

Thanks again,
Sam.

if it takes more than say a second it becomes almost pointless

Comfortably under a millisecond, I'd say.

Brilliant thanks. I hoped it would be quick from my experience with computers but I read it could get long with floating points.

Pi is defined as "PI" so no need to create a constant for this.

ok good to know thanks. Are you absolutely sure? I just looked it up and the const help page shows pi being defined. Also, PaulS said:

The Arduino doesn't know anything about PI, or pie, so, you'll need to define it:

Thanks for the heads up though if that's correct.

Sam.

The following math/trig defines (all predifined by Arduino) are quite useful if you need them:

#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398
#define TWO_PI 6.283185307179586476925286766559
#define DEG_TO_RAD 0.017453292519943295769236907684886
#define RAD_TO_DEG 57.295779513082320876798154814105

#define PI 3.1415926535897932384626433832795

Whoa!

3.141592653589793 is already the most precise you can do on a double precision floating point number.

Due to AVR only doing single precision, this number will be a lot 'shorter'.

Brilliant thanks BenF! Sorry for doubting you. Copying that to a local text file now.

Thanks for all your help guys,
If you're lucky and I get this working I might be able to share a nice little function for driving a tracked chassis with you all.

Thanks again,
Sam.

PS. This is one of the nicest forums ever. For me the prize is between this and the MediaBrowser forum.

oh. Leppie are you saying that this wont work? Or just that it handles pi less accurately than BenF said?

Thanks,
Sam.

It will work.
The headers are global headers, and will work for 32, 64, 80 or probably even 128 bit floating point.

If you want pi to the accuracy of your implementation,
pi = 4.0 * atan (1.0);

The constant as defined by Arduino for PI is the best representation possible with the double (64-bit) data type. Arduino will only support float (32 bit) for runtime calculations, but this is handled transparently by the compiler.

Actually it is preferred to use double precision for floating point constants also in Arduino. This is because the compiler as such will use double precision for its compile time math and any math expressions that can be evaluated at compile time will benefit from the extra precision.

Thanks. Makes sense now. I don't even need it that accurate anyway because it is just steering a chassis but thanks for all the input. As I said earlier hopefully I can share something with you guys when this is done.

Thanks,
Sam.

This is because the compiler as such will use double precision for its compile time math and any math expressions that can be evaluated at compile time will benefit from the extra precision.

Very good point, but all those extra 'useless' digits are a bit excessive :slight_smile: