 # Problem with arduino maths

Hi everyone, i am using the math.h or the first time and when the arduino does the math the answers of the arduino and the calculator have atleast a difference of 2. Thats a big difference which i dont want.Below is the code i used for doing the math.

``````#include <math.h>

double u;
double t;
double a;
double f;
int m = 4;
int v = 0;
double g = 9.8;
int d = 1;
int AOF = 70;

void setup(){
Serial.begin(9600);
u = sqrt((g*d)/sin(2*AOF));
Serial.print("Velocity = ");
Serial.println(u);
Serial.println();
t = 2*(u*sin(AOF))/g;
Serial.print("Time = ");
Serial.println(t);
Serial.println();
a = -1*((v-u)/t);
Serial.print("Acceleration = ");
Serial.println(a);
Serial.println();
f = m*a;
Serial.print("Force = ");
Serial.println(f);
}

void loop(){
}
``````

These are the answers i get form the arduino-
velocity = 3.16
time = 0.5
acceleration = 6.33
force = 25.33

These are the answers from the calc -
velocity = 3.95
time = 0.7
acceleration = 5.somthing (dont remember)
force = 23.something

I coudnt find out wat i was doing wrong, plz give me ur advices on how to get the arduino answer as close as i can to the calculators or is this the best arduino can do. Plz need help.

Everything higher level than geometry uses radians. You should, too.

Does the sin() function take degree or radians? Which one are you using?

The sin function takes degrees. In my case it 70 degrees that is the Angle Of Fire (AOF).

amruth11: The sin function takes degrees. In my case it 70 degrees that is the Angle Of Fire (AOF).

The fact that doing the calculations assuming it takes radians makes them match the ones with an arduino seem to refute your claim.

If you really want to use degrees, there's a radians() function which converts.

amruth11: The sin function takes degrees.

You might be interested in the first sentence of this page: http://arduino.cc/en/Reference/Sin

Ok, i will try using radians.

I want to find sin(140) and the calculator returns 0.6, how do i do that in arduino. :~

I want the degrees converted to radians and thats what sin() does but the answer is wrong, why. Need help with this. :~

amruth11:
I want the degrees converted to radians and thats what sin() does but the answer is wrong, why. Need help with this. :~

Thats not what sin() does. Sin() takes the angle in Radians as the input. It also takes it as a float, so why not just convert it yourself?

Your calculator is using Degrees. sin() uses Radians. (As is explained in the reference page.)

``````  float answer = sin(140.0);
``````

Returns 0.98. My calculator, in Radians-mode, returns the same thing.

If I assume you mean "140 degrees" then converting to radian:

`````` float answer = sin(140.0*(3.14/180.0));
``````

Returns 0.64.

Also note that my integers are in float/decimal. "140.0" instead of "140".

Thx james for solving my problem, thats the answer i wanted, so sin() takes int radians and give degrees. Thx XD

amruth11: Thx james for solving my problem, thats the answer i wanted, so sin() takes int radians and give degrees. Thx XD

So close...

It actually takes a float.

so sin() takes int radians and give degrees.

No, it takes radians, and gives the sine of the supplied angle. A sine is a ratio.

You should also know that Arduino float and Arduino double are both 32-bit Floating Point.

In other words, the "double" keyword is a lie, as it is not really double-precision floating point.

What is the reasoning behind using "double" as a (misleading) alias for "float", anyway?

It allows you to compile software unchanged, but gives lower precision results.

AWOL:
It allows you to compile software unchanged, but gives lower precision results.

Why not just use

``````#define double float
``````

or something of that sort? (if you really are that desperate for it to compile)

odometer: Why not just use

``````#define double float
``````

or something of that sort? (if you really are that desperate for it to compile)

The funny thing about trade-offs is that not everyone will be happy with the choices. Otherwise, there wouldn't be a tradeoff.