Incorrect calculations... please help.


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Posts
    81

    Default Incorrect calculations... please help.

    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

  2. #2
    Join Date
    Aug 2005
    Location
    Vermont
    Posts
    15


    Did you find this post helpful? Yes | No

    Default calculations

    Let's do the math....
    GalUse = 1600 - 1475 'which equals 125, this is fine

    MPG = 3280/125 'which equals 26.24 and the .24 is lost because PBP doesn't handle floating point. Therefore in your debug statement MPG/100 is equal to 26/100 which is less than 0 and is displayed as 0. DEC2 MPG displays 26 which is correct.

    Peter
    Last edited by Mith; - 29th January 2007 at 18:36.

  3. #3
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    AH!, OK... Now I see it. Thanks....

    If anyone ever follows this thread or stumbles accross it, here was my solution:

    CalcMPG:
    MPG = Miles * 100 ' Add 2 decimal places to the miles Variable
    MPG = DIV32 GalUse ' perfome a divide on the 32bit result
    Return

    My output now looks like

    Level: 014.75
    MPG: 026.24
    Range: 387.04
    Miles: 032.80

    which is 100% what it should be. So now here comes the fun part... scaling this all into a 24 segment bargraph for LEVEL, MPG and RANGE...

    Thanks again Mith, Like I said some times it just takes a different set of eyeballs to see what someone else cant...
    Last edited by bearpawz; - 29th January 2007 at 20:01.

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default How to measure fuel

    Hi Bearpawz,
    What are you measuring the fuel consumption with? And are you measuring the miles traveled with a VSS signal?
    Just curious.
    JS
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Incorrect adc channel bits generated
    By grahamg in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th February 2010, 18:56
  2. DS1307 returns incorrect data ???
    By gtvmarty in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 8th October 2009, 09:54
  3. clock/timer calculations
    By virolay in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 4th January 2008, 20:20
  4. Calculations for using faster Clocks.
    By Tissy in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th April 2005, 14:24
  5. timing calculations
    By scorpion in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th November 2004, 22:58

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts