Km to MPH conversion - what am I doing wrong?
KM=29
KM1=125
MPH = ((KM*256)+(KM1))*(1006/1619)/100
MPH1=((KM*256)+(KM1))*(1006/1619)//100
LCDOUT $FE, 128, DEC MPH,".",DEC MPH1
It returns a result of 0.0. I'm assuming it does this because the ones place is zero. I can do it the other way where it's 6.21...... but when the PIC does the math, the result is 45.12 MPH when it should be 46.61 MPH. As another example, if KM is 42 and KM1 is 221, the PIC calculates 61.21 MPH and it should be 63.39 MPH. Then if the speed is over 65 MPH, it rolls over due to a word sized variable.
I have two problems but I'm more concerned with the accuracy. Is there a way to get the reading accurate?
Re: Km to MPH conversion - what am I doing wrong?
Well I think there is a easier way to do what you want to do. doing a google search I have found that 1 kmh is equal to .6213 mph. what is km and km1?
Re: Km to MPH conversion - what am I doing wrong?
Hi Chris,
My guess is with such large numbers that you would probably have to do such math with PBPL on a 18Fxxxx device.
Re: Km to MPH conversion - what am I doing wrong?
Quote:
Originally Posted by
c_moore
doing a google search I have found that 1 kmh is equal to .6213 mph.
Thank you for that revelation. What do you think the result of (1006/1619) is?
Quote:
Originally Posted by
c_moore
Well I think there is a easier way to do what you want to do.
You're probably right. However, if I knew what that easier way was, I wouldn't have posted the question in the first place. I guess you're going to leave that proverbial carrot right in front of my face. Or, do you really know what that easier way is?
Quote:
My guess is with such large numbers that you would probably have to do such math with PBPL on a 18Fxxxx device.
I'm using an 18F4550.
Re: Km to MPH conversion - what am I doing wrong?
Have you seen the PBP manual, section 3.1.2?
Re: Km to MPH conversion - what am I doing wrong?
Yes, that was one of the first things I tried. One question though. You say 3.1.2 and in my manual that's page 17 where they reference command line options. I went to page 45, which is section 5 and it shows the statement references. Is 3.1.2 relevant to this problem?
Anyhow, they show how to go from MPH to km (*/), but not the other way around. Maybe I'm just not plugging the numbers in correctly. I thought about using the DIG command but that seemed overly complex to get a simple conversion.
Re: Km to MPH conversion - what am I doing wrong?
miles = km */ 159
Should work.
Re: Km to MPH conversion - what am I doing wrong?
Quote:
Thank you for that revelation. What do you think the result of (1006/1619) is?
You don't have to be a smart ass. Why don't you just multiply you km value by 6213/1000? it seems easy to me. maybe I am missing something.
Re: Km to MPH conversion - what am I doing wrong?
Quote:
You don't have to be a smart ass.
I made my comment because of yours:
Quote:
doing a google search I have found that 1 kmh is equal to .6213 mph.
It's as if you said, "I spent 30 seconds on Google and found out that 1 kmh is equal to .6213. You couldn't have tried to research this problem at all."
In response to your current question, of
Quote:
Why don't you just multiply you km value by 6213/1000? it seems easy to me. maybe I am missing something.
Yes, you are missing something. Did you read my first post?
Quote:
I can do it the other way where it's 6.21.....Then if the speed is over 65 MPH, it rolls over due to a word sized variable.
Did you ever try to do 98 X 256 X 6.213 on an 18F4550?
Re: Km to MPH conversion - what am I doing wrong?
what is the range of the km and mp you need?
If up to 105 Km is OK with you then a word size variable can do it I think.
105(km)*621=65205. Scaling by 100 gives 65.205 Miles.
Or 65205/621 gives back Km. Also if the Miles are scaled up by a factor of 100.
Ioannis
Re: Km to MPH conversion - what am I doing wrong?
Mackrackit's solution may work. I hope to try it today.
Re: Km to MPH conversion - what am I doing wrong?
Didn't you need the reverse?
Ioannis
Re: Km to MPH conversion - what am I doing wrong?
Quote:
Originally Posted by
Ioannis
Didn't you need the reverse?
Ioannis
No.
Quote:
miles = km */ 159
Should work.
Like a champ! It's .13 MPH off at 100 MPH and .09 MPH off at 1 MPH. I'm guessing because of the additional numbers needed for a true KM to MPH conversion? Who cares, I don't need it to be that accurate anyway. Thanks again for the help!
Re: Km to MPH conversion - what am I doing wrong?
Hi,
You said you don't need it but you could try miles=km**40718, it should get you a little bit closer still.
/Henrik.