Announcement

Collapse
No announcement yet.

Quick question regarding integer math.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Quick question regarding integer math.

    I know I have to use integer math when working with variables but just want to clarify that I can still do something like: var1 = var2 * .3333 - The integer portion of the answer should be correct but the decimal will be truncated?

  • #2
    You can't write a constant as '.3333'.

    You can write a variety of equivalent calculations:

    Code:
    var1 = var2 / 3
    
    var1 = var2 ** 21843   '(var1 = (var2 * 21843) / 65536)
    
    var1 = var2 */ 85       '(var1 = (var2 * 85) / 256)
    In all of these calculations, the result is the integer portion and the fraction (decimal) will be discarded.
    Charles Leo
    ME Labs, Inc.
    http://melabs.com

    Comment


    • #3
      Thanks, Charles

      What I actually have is:

      Atemp = Atemp + TempHistory[Hcount]*(Mincount/60)

      Mincount can be between 0 and 60 so divided by 60 it will always be a fraction. So if Mincount = 20 it would equate to .333.... This seems to work within the equation. So I understand what your saying is the to use .3333 is not allowed because it is a constant but occurring in the course of a calculation it would work truncating everything after the decimal point?

      Comment


      • #4
        You must consider each operation separately. Because of your placement of parentheses, (Mincount/60) will always be performed first, and will always yield 0 because it's always a fraction.

        You should do the multiplication first in order to keep a workable value:

        Atemp = Atemp + ((TempHistory[Hcount]*Mincount)/60)

        The problem then becomes the maximum result of the multiplication, which can't be allowed to exceed the capability of the largest variable type. If you're compiling with LONG variables enabled, this usually isn't an issue. If compiling with only WORDs, then TempHistory[Hcount] can't exceed 1092, because 1093 * 60 = 65580.

        The question is, what is the maximum possible value for TempHistory[Hcount]?
        Charles Leo
        ME Labs, Inc.
        http://melabs.com

        Comment


        • #5
          Also, are you compiling for a PIC18 device?
          Charles Leo
          ME Labs, Inc.
          http://melabs.com

          Comment


          • #6
            TempHistory is word but Atemp is long. Using a 16F1825.

            Comment


            • #7
              I'll need some clarification. LONG variables aren't available for the 16F1825.

              Are you saying the value in TempHistory could be as high as 65535? This would probably require the DIV32 operator in the 16F1825. A PIC18 part would use a different method.
              Charles Leo
              ME Labs, Inc.
              http://melabs.com

              Comment


              • #8
                My mistake - they are both words.

                Comment


                • #9
                  The DIV32 operator should yield a good result for the multiplication and division.

                  Code:
                  TempWord = TempHistory[Hcount] * Mincount
                  TempWord = DIV32 60
                  Atemp = Atemp + TempWord
                  Charles Leo
                  ME Labs, Inc.
                  http://melabs.com

                  Comment


                  • #10
                    Thanks. On another issue how would I do something like 9000/7.2? I would start by shifting the decimal points of the numbers 1 to the right which would work out except 90000 exceeds the max of a word.


                    I think I have it. (9000/72)*10?

                    Comment


                    • #11
                      I'll need to know variable types and ranges of values.
                      Charles Leo
                      ME Labs, Inc.
                      http://melabs.com

                      Comment


                      • #12
                        I'm using it like this where Atemp is a word that's 0-10000 and MinCount is a byte that can be 1-60 and Loopcount is a byte that's always less than 24.

                        I want:: Atemp = Atemp / ((Loopcount -1) + MinCount/60)

                        Use: Atemp = Atemp / ((Loopcount -1)*60 + MinCount

                        Comment


                        • #13
                          The 'want' and 'use' formulas are not mathematically equivalent.

                          The 'want' formula could use a scaling of 60 to remove the error attached to the MinCount division. This may be what you were getting at with the 'use' formula:

                          Code:
                          Temp1 = ((Loopcount - 1) * 60) + MinCount
                          Temp2 = Atemp * 60
                          Atemp = DIV32 Temp1
                          Charles Leo
                          ME Labs, Inc.
                          http://melabs.com

                          Comment


                          • #14
                            Atemp will be in the thousands so there would be a problem with temp2 being a word in your example wouldn't it? Or does the register still hold the real 32 bit answer?

                            Comment


                            • #15
                              The DIV32 operator makes use of internal variables that hold a 31-bit result. The PBP manual describes DIV32.
                              Charles Leo
                              ME Labs, Inc.
                              http://melabs.com

                              Comment

                              Working...
                              X