setting values and percentages


Closed Thread
Results 1 to 38 of 38

Hybrid View

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


    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 11:44. Reason: more info

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

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,682


    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

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,682


    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

  5. #5
    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 12:48. Reason: more thinking

  6. #6
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Look at the Statement keyword "DIV32". It is possible to compute all of these statements:
    numx=4095
    50*numx/100

    numy=50
    numy*4095/100

    numz=100
    50*4095/numz
    Dave Purola,
    N8NTA
    EN82fn

  7. #7
    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
    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?
    After more thought and a bit of direction to look at DIV32 by Dave.

    Multiplication stores a 32 bit result internally but the normal divide only works on the lower 16 bits which causes the 'overflow' problem highlighted by Richard. DIV32 was created specifically to overcome the 16 bit division limitation and as Dave says is a solution here.

    Scampy asked about */ and how that works. It works by removing the division in the calculation so there is no 16 bit divide, there is an 8 bit shift or a divide by 256 of the 32 bits whichever way one wishes to visualise the process. Knowing that */ will apply a division by 256 one has to take account of this in the program.

    In this case

    B_max = (4095/100)*maxbright

    take 4095/100 and multiply by 256 giving 10483.2 which is rounded up to 10484. Resulting in this

    B_max = maxbright*/10484 which results in an integer value of 2047 when maxbright is 50.
    Last edited by EarlyBird2; - 4th July 2014 at 08:44.

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


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    I wrote this little bit of code to experiment with this thread its interesting to note that to */ method fails when your percentage goes past 1600 in lieu of 50
    but the div32 method is quit sound up until the result exceeds 2^16 (65535 )
    you need to remember that */ is the middle 16 bits and if you overflow that your still in trouble
    Code:
    ****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 7/2/2014                                          *
    '*  Version : 1.0                                               *
    '*  Notes   : 16f684                                                  *
    '*          :                                                   *
    '****************************************************************
      
    #CONFIG
         __config _INTRC_OSC_NOCLKOUT & _CP_OFF & _WDT_ON  &  _PWRTE_ON  &  _MCLRE_OFF 
    #ENDCONFIG
     include "alldigital.pbp"
     
     
     
     
     
     
                  OPTION_REG.7=0
                  trisa = %11111110
                 
                  DEFINE OSC 4
                  so var porta.0
                  dq var portc.1
                  Nposn var word
                  ss var word
                  s2 var word    
                  s1 var word
                  
                   
        high so         
        ss=4095 
        s1=1600
        s2=100      
             
        pause 4000       
        serout2 so,84,[ "ready ",13,10] 
        lop: 
        pause 2000
        nposn=s1*ss
        nposn = div32 s2          
        serout2 so,84,["1 " ,#Nposn,13,10   ]   
        nposn=1600*4095/100          ; you need to adjust this line too
        serout2 so,84,["2 " ,#Nposn,13,10   ]
        nposn =  s1  */ 10484
        serout2 so,84,["3 ", #Nposn,13,10   ]
        nposn=s1*ss/100
        serout2 so,84,["4 ", #Nposn,13,10   ] 
        
        goto lop

Similar Threads

  1. POT part values
    By Michael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th January 2009, 11:41
  2. Port Values
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd January 2007, 21:44
  3. t in POT, c's values
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th August 2006, 00:30
  4. Numeric Values
    By Armando Herjim in forum General
    Replies: 8
    Last Post: - 28th June 2006, 02:54
  5. Wrong values with COSINE!
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 8th May 2006, 17: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