setting values and percentages


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,663


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    result 50*4095/100 is 2047
    result (4095/100)*50 is 2000

    the calc follows typical bodmas rules (brackets, of , divide .....) see precedence in manual

    the only 'suprising' bit is that the indermediate result (50*4095) is 204750 way too big for a word var. the pbp compiler code does indeed use 32 bit intermediate results.
    the */ and ** operators allow you access these 32 bit results (providing you follow the rules of course)

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,663


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    however
    if you have a word var say numx
    and x =4095
    then 50*numx/100 the result is 81
    the word var causes a intermediate stage overflow (bits 0-15 of 204750 =8142)
    I still say integer math is tricky and results need to be double checked

  3. #3
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by richard View Post
    however
    if you have a word var say numx
    and x =4095
    then 50*numx/100 the result is 81
    the word var causes a intermediate stage overflow (bits 0-15 of 204750 =8142)
    I do not understand this.
    What is the relationship between numx and x?
    Why does an intermediate stage overflow occur and what is an intermediate stage overflow?

  4. #4
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by EarlyBird2 View Post
    I do not understand this.

    Why does an intermediate stage overflow occur and what is an intermediate stage overflow?
    Could you explain please?

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,663


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    What is the relationship between numx and x?
    poor typing
    if you have a word var say numx
    and numx =4095
    then 50*numx/100 the result is 81
    the word var causes a intermediate stage overflow (bits 0-15 of 204750 =8142)
    I still say integer math is tricky and results need to be double checked

    intermediate overflow
    50*4095 =204750 max value for word 65535 therefore an overflow occurs and the result becomes 8142
    Last edited by richard; - 3rd July 2014 at 10:44. Reason: more info

  6. #6
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by richard View Post
    intermediate overflow
    50*4095 =204750 max value for word 65535 therefore an overflow occurs and the result becomes 8142
    But the result is not stored in numx it is stored in another word variable called result?

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,663


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    simply stated
    if there is a var in a multilstage calculation then any intermediate results must be able to be stored in that var , its just the way it works.
    try it yourself
    the */ ,** ops are the only workaround

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,663


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    you might have missed the point

    50*4095/100 is 2047 this works (all constants)

    numx=4095
    50*numx/100 is 81 this does not

    numy=50
    numy*4095/100 is 81 this does not

    numz=100
    50*4095/numz is 81 this does not

  9. #9
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Now I understand what you are saying. It does seem odd to me that there is a 32 bit register for maths calculation which can not be used if there is a word or byte variable in the calculation because the word/byte variable could overflow. A bit more thinking is required on my part, I think.

    Does this mean that using ** and */ uses 32 bit and * only 16 bit?
    Last edited by EarlyBird2; - 3rd July 2014 at 11:48. Reason: more thinking

Similar Threads

  1. POT part values
    By Michael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th January 2009, 10:41
  2. Port Values
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd January 2007, 20:44
  3. t in POT, c's values
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th August 2006, 23:30
  4. Numeric Values
    By Armando Herjim in forum General
    Replies: 8
    Last Post: - 28th June 2006, 01:54
  5. Wrong values with COSINE!
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 8th May 2006, 16:59

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