PICAXE 18X math with large numbers

hkd64

New Member
I am designing a stockcar lap timer that stores lap info and dumps it to a computer. My problem is calculating the MPH.

Lap time is 15.195 seconds
Track length is .333 miles

MPH should be 78.90 mph

My formula is the standard speed formula 'time / 3600 to convert to hours then divide that into the length of the track. I have tried many multiplications and or divisions to get rid of the decimal and not exceed 65536 length of a word. the best I can get is a straight 78.00 mph. I have searched the forums and the manual with no luck. Any help would be greatly appreciated.
 

benryves

Senior Member
Do you want a decimal result or is an integer good enough?

Integer division truncates ("round to zero") rather than to the nearest integer. You can use the modulus (remainder) to round up if appropriate.

If the speed is calculated as .333 / (t / 3600), this can be simplified to 1198.8 / t. To get more precision we can multiply each side by fifty (this keeps the large number on the left below 65535):
Code:
W0=59940 ; 1198.8 * 50
W1=760   ; 15.195 * 50
W2=W0/W1
This yields a result of 78, as expected (truncated). However, if we find the remainder of that division, we can use that to round upwards if the part of the result that would be after the decimal point is 0.5 or above:
Code:
W0=59940 ; 1198.8 * 50
W1=760   ; 15.195 * 50
W2=W0/W1

; Adjust result using the remainder.
W3=W0%W1
W1=W1/2
If W3 >= W1 Then
	Inc W2
EndIf
This corrects the result to 79.

You can also use the remainder to calculate the part after the decimal point, if you don't mind losing a little precision:
Code:
W0=47952 ; 1198.8 * 40
W1=608   ; 15.195 * 40

W2=W0/W1
W3=W0%W1*100/W1
This returns the integral part in W2 and the fractional part to two (truncated) decimal places in W3; in this case, 78.86 (not great).

How you do this depends on the precision you require and range of values involved! In the above examples, if W1 (time) is too small or too large you'll get incorrect values. For robustness I'd be tempted to invest in an I²C floating-point coprocessor, or just use the PC that is receiving the data to perform the calculation.
 
Last edited:

westaust55

Moderator
welcome to the PICAXE forum.

You indicate lap time and track length to 3 decimal places.
How are you storing these values? multiplied by 10, 100, 1000 etc

PICAE only calculates in integer values including intermediate values on a program line.

benryves has given you a reasonable method but someone may be able to provide a better accuracy if you give some further details.
program listing you currently have or as a minimum how you are holding the time and distance data.
 

hkd64

New Member
Sorry, I did not explain my problem in enough detail. My numbers are derived from a 5 digit BCD counter. The track length will be input from a key board. Both are entered without decimal points. The problem I am running into is the distance divided by time is always less than 1. Picaxe will not compute this. Your response has me lookin in a different direction. I may have to change the time to 4 digits and use feet/seconds. If I figure it out the answer I will post the results.
 

hkd64

New Member
Thanks Westaust55 for the info. As explained to benryves my system will be getting info from a 5 digit BCD counter with the distance input from a key board. No decimals in the numbers. For the MPH I could change to 4 digits instead of 5 for a acceptable accuracy. I am new to the Picaxe numbering system but have confidence a solution is out there.
 

BCJKiwi

Senior Member
Perhaps you could give us the actual numbers you are dealing with and what they represent ( max and min values for each).

e.g.
In Post 1 track length is given as .333 miles. What number are you actually using to represent .333 miles?

we will have a better idea of practical options with this info.
 

hkd64

New Member
Due to the picaxe not able to do decimal math I am using the numbers as first posted with no decimal point. The distance of .333 miles I use 333 and for 15.195 seconds I use 15195. These numbers are inputed from a keyboard and BCD counter. They are not part of the program except for testing puposes where they are set as constants.
 

BCJKiwi

Senior Member
OK, so
what is the shortest track/longest track.
What is the shortset time/longest time
do you need 3dec places on the seconds?
 

westaust55

Moderator
I will throw this bit of PICAXE maths into the ring . . . .
Should tolerate a distance up to 0.655 miles and durations to 65.535 seconds

Obvioyusly there is scope to reuse variables but left as is for clarity


Code:
SYMBOL miles = w0
SYMBOL duration = w1
SYMBOL miles100 = w2
SYMBOL durat_36 = w3
SYMBOL durat_360 = b13
SYMBOL fraction = b12
SYMBOL invfract = b11
SYMBOL adjust = b10
SYMBOL firstest = w4
SYMBOL mph = w4

miles = 333
duration = 15195
durat_36 = duration / 36
durat_360 = durat36 / 10
fraction = durat_36 // 10
invfract = 10 / fraction
initial = miles100 / durat_36
adjust = 50 + initial / invfract / durat_360  ; 50 is a "magic" number for some rounding up
final = initial – adjust
This (final) is 10 time the answer so
Final / 10 to extract the whole part and
Final // 10 for 1 decimal place.
 

hkd64

New Member
Thanks everyone for your responses to my problem. I have learned a lot about picaxe math. I was hopeing not to set limits on the inputs but I see why I must. My lap counter for this year will only be used at one track which is .333 miles long. The times will vary between 10 and 15 seconds. It looks like westaus55's answer will get me there. Again thank you all.
 
Top