Doing Simple Math - getting the wrong answer


Closed Thread
Results 1 to 4 of 4

Hybrid View

  1. #1
    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

  2. #2
    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

  3. #3
    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, 10:01
  2. what's wrong 16F877A simple code?
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th October 2009, 02: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, 11:22
  4. not quite understanding the MATH function
    By Rhatidbwoy in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th January 2006, 21:20
  5. Simple Math
    By brenda in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 17th August 2005, 18: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