Doing Simple Math - getting the wrong answer


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2005
    Location
    Bellevue
    Posts
    125

    Angry Doing Simple Math - getting the wrong answer

    Probably a 'newbie' error, but it has me cornered...

    I have a routine that is trying to take degrees C and convert to degrees F.. However the math is not working.

    Here is how it works:

    1.) I take data into an array and read that data tha deals with temperature and convert from hex to decimal

    ' read data for temperature Temp
    SSD4= (ssmax[24]>>4)
    SSD3= (ssmax[24] & $f)
    SSD2= (ssmax[23]>>4)
    SSD1= (ssmax[23] & $f)

    ' convert to Decimal:

    Dec_temp=(ssd4*4096)+(ssd3*256)+(ssd2*16)+ssd1

    2) I read these values out into a screen to show degrees (C), then write it out to the screen:

    serout2 cpinout, 84,["Temp(C): ",#Dec_temp dig 3, #Dec_temp dig 2,#Dec_temp dig 1, ".",dec1 Dec_temp]

    THIS WORKS FINE.. I get the proper 'Degrees C'...

    "Temp(C): 025.5 " shows up in HyperTerminal. No problem..

    BUT! Here is where it gets funky.. What if I wanted to see Degrees F?

    3) I multiply:
    Dec_tempf=(Dec_temp*(9/5) + 32) ' convert to farenheit

    And output with this:

    serout2 cpinout, 84,["Temp(F): ",#Dec_tempf dig 3, #Dec_tempf dig 2,#Dec_tempf dig 1, ".",dec1 Dec_tempf]

    The display The same serout2, but now in (F)

    It shows "Temp(F): 025.5" - What the heck?? That is the same as (C)???

    So then I decide to look at the numbers underneath:

    First - theBase # is "223" This is the value in #Dec_temp

    Then look at the math:
    Testmath1 - (Dec_temp*(9/5)+32) = 255 -- this is wrong

    Or even look at **:
    Testmath2 - (Dec_temp**(9/5)+32) 32 -- still odd..

    Can anyone solve this puzzle?

    Thanks

    Tom

  2. #2
    anj's Avatar
    anj Guest


    Did you find this post helpful? Yes | No

    Default

    First - theBase # is "223" This is the value in #Dec_temp

    Then look at the math:
    Testmath1 - (Dec_temp*(9/5)+32) = 255 -- this is wrong
    PBP uses full precedence of math operators, and also uses braces to force out of order of calculation.
    Secondly it also only works in integers.

    As such (9/5) = 1.8 = 1 ( and is calculated first )
    hence Dec_tempf = 223 * 1 +32 = 255

    try ( Dec_temp*( 90 / 5 ) + 320 ) / 10
    ie ( Dec_temp*18 + 320 ) / 10

    Also, if you just leave it at ( Dec_temp*18 + 320 ) you can use simple math to get the basic temp and the first decimal.

    Andrew

  3. #3
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    Hi Tom,

    PBP can't handle fractions very well, the "**" and "*/" operators however makes it possible. 9/5 is 1.8 which PBP will trunctate to 1, not very useful. The "**" operator can handle the fractional part, this operaton makes an "invisible" division by 65536. 0.8*65536=52428.8 which we round up to 52429. Since your Dec_temp variable contains the temerature in tenths of a degree your formula needs to be ......

    Dec_tempf=Dec_temp*(9/5) + 320 'Farenheit * 10

    We rewrite this to ...
    Dec_tempf=Dec_temp*1.8 + 320 'Farenheit * 10

    Which is the same as .....
    Dec_tempf=Dec_temp*1+Dec_temp*0.8 + 320 'Farenheit * 10

    Using "**", we end up with ......
    Dec_tempf=Dec_temp + Dec_temp**52429 + 320

    Another thing, you can replace ......

    ' read data for temperature Temp
    SSD4= (ssmax[24]>>4)
    SSD3= (ssmax[24] & $f)
    SSD2= (ssmax[23]>>4)
    SSD1= (ssmax[23] & $f)

    ' convert to Decimal:

    Dec_temp=(ssd4*4096)+(ssd3*256)+(ssd2*16)+ssd1

    ...... with ......

    Dec_temp.highbyte = ssmax[24]
    Dec_temp.lowbyte = ssmax[23]

    /Ingvar

  4. #4
    Join Date
    Oct 2004
    Location
    Italy
    Posts
    695


    Did you find this post helpful? Yes | No

    Default

    Fahrenheit = Celsius * (212 - 32)/100 + 32


    Example 30 Celsius to Fahrenheit:


    30 * 180 / 100 + 32 = 86 Fahrenheit


    Best regards,

    Luciano

Similar Threads

  1. Simple Maths Going Wrong
    By Bill Legge in forum mel PIC BASIC Pro
    Replies: 43
    Last Post: - 27th May 2010, 09:01
  2. what's wrong 16F877A simple code?
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th October 2009, 01:11
  3. PBPL Math...new math takes more cycles...Always?
    By skimask in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th February 2008, 10:22
  4. not quite understanding the MATH function
    By Rhatidbwoy in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th January 2006, 20:20
  5. Simple Math
    By brenda in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 17th August 2005, 17:47

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