system
March 26, 2010, 5:37pm
1
I have a question, I'm parsing NMEA data and would like to convert the SOG in knots to mph. I want the data to be in integer format (don't need anything past the decimal point) and thought about using the map function map(X,0,200,0,230). Am I correct in assuming this is a very simple way to do the conversion or am I missing something? Is there an easier way?
Wayne
system
March 26, 2010, 6:30pm
2
Multiplying by 2.3 would be faster, but map will work.
It should work well so long as you're not taveling in a fast jet. At about 1141 MPH your calculated value will start to diverge from the actual value.
You should read through this...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1257716581
system
March 26, 2010, 9:27pm
4
I think you mean 1.151, but since you mention it, if I multiple an integer is the resultant value still an integer or is reclassified as a float?
It is an integer of the largest type in the expression after promoting to int...
Multiply two bytes - result is a byte.
Multiply two bytes - result is an int.
Multiply a byte and a short - result is an int.
Multiply a byte and a long - result is a long.
[Corrected byte times byte. The result is an int. Sorry about the mistake.]
system
March 26, 2010, 10:05pm
6
#include <iostream>
#define k 6076
#define m 5280
using namespace std;
int main()
{
int kts = 25;
int mph;
mph = int( ( (long) kts * (long) k)/ (long) m);
cout << mph <<"\n";
return 0;
}
prints 28
i compiled and ran it with g++ rather than on Arduino
edit correct result with floats is 28.76----
system
March 27, 2010, 11:47am
7
And here's how you would do rounding to the nearest mph:
#include <iostream>
#define k 6076L
#define m 5280L
#define half_m 2640L
using namespace std;
int main()
{
int kts = 25;
int mph;
mph = (int)((((long)kts * k) + half_m) / m);
cout << mph << "\n";
return 0;
}
Regards,
-Mike
system
March 27, 2010, 1:46pm
8
Mike i cut and pasted that, compiled and ran it and it prints 28.
(6078*25 +2640)/5280 = 29.27840909091
changed it to this.
#include <iostream>
#define k 6076L
#define m 5280L
#define half_m 2640L
using namespace std;
int main()
{
int kts = 25;
int mph;
long tmp;
tmp = (long)kts * k;
cout << tmp <<"\n";
tmp = tmp + half_m;
cout << tmp <<"\n";
mph = (int)(tmp / m);
cout << mph << "\n";
return 0;
}
prints:
151900
154540
29
this---
#include <iostream>
#define k 6076L
#define m 5280L
#define half_m 2640L
using namespace std;
int main()
{
int kts = 25;
int mph;
mph = (int)( ( (long)kts * k + half_m ) / m);
cout << mph << "\n";
return 0;
}
prints 29
I sure couldn't see what was wrong until i did the intermediate prints. Too many (((( for me to sight read.