Ok, Im writing a program, which uses numbers that i require to be 100ths accurate (ie: 000.00)
Obviously trying to do any type of floating point math is a PITA so I just decided I'll multiply everything x 100 and let the display put a decimal point where needed.
Here is my code:
FuelLevel VAR WORD ' Store Current Fuel Level
GalLeft Var WORD ' Gallons Left to consume
GalUse Var WORD ' Gallons Used
MPG VAR WORD ' Holds Miles / Gallon
Range Var WORD ' Holds Range Value
Miles Var Word ' Miles Traveled
Gallons Con 1600 ' Aprox 16.00 Gallons per tank
Miles = 3280 ' Simulated Milage of 32.8 Miles
FuelLevel = 1475 ' Debug Fuel Level = 14.75 Gallons
' __________________________________________________ ________________________________________
Start:
Gosub CalcGalLeft ' Calculate gallons left
Gosub CalcGalUse ' Calculate the gallons used
Gosub CalcMPG ' Calculate MPG
Gosub UpdateDisplay ' Update the display
Goto Start
END
' ------------------------------------------ Sub Routines -----------------------------------
CalcGalLeft:
GalLeft = Gallons - (Gallons - FuelLevel) ' 16.00 - (16.00 - 14.75) = 14.75
' Note: this is same as FuelLevel to begin with so why do I have this here???
Return
CalcGalUse:
GalUse = Gallons - FuelLEvel ' 16.00 - 14.75 = 01.25
Return
CalcMPG:
MPG = Miles / GalUse ' MPG = 32.80 / 01.25 = 26.24
Return
UpdateDisplay:
debug "Level: ", DEC2 GalLeft/100,".",DEC2 GalLeft, 13,10
DEBUG "MPG: ", DEC2 MPG/100,".",DEC2 MPG, 13,10
DEBUG "Range: ", DEC3 Range,".",Dec2 Range, 13,10
DEBUG "Miles: ", DEC4 Miles/100,".",Dec2 Miles, 13,10,13,10
Return
This is the output:
Level: 14.75
MPG: 00.26 <----------- This *SHOULD* be 26.24
Range: 046.46
Miles: 0032.80
MPG should be displayed as 26.24.... but its only showing .26. I cant find whats wrong... Sometimes all you need is another set of eyes![]()
Bookmarks