Calculation not working properly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?ZG9uZGlnaXRlY2g=?=

    Calculation not working properly

    I having problems getting this function to return the correct value.
    Basically I have a curveDataCalc method that loops as it should, but just
    doesn't seem to be working right. I've stepped through the function and the
    variables are being passed through correctly its just the clocksPerQREV and
    calcAngleTime are never being calculated and are always zero. This causes the
    other values to incorrectly calculated, ultimated giving me the wrong
    advanceAngle values. Maybe I am missing a conditional statement, but I can't
    seem to figure it out right now. Any suggestions would be helpful.


    // CALCULATE ADVANCE ANGLE USING curveDataCalc METHOD
    double[] advAngle = new double[64];
    for (int p = 0; p < 64; p++)
    {
    advAngle[p] = curveDataCalc(r pmArray[p], multiplier[p],
    offset[p]);
    }
    }
    #endregion


    public double curveDataCalc(i nt rpmArray, int multiplier, int offset)
    {
    //CONSTANTS
    int calcAngle = 90;
    int leadAngle = 72;
    double clockPeriod = 0.000004; //timer clock
    period = 4uS
    int clocksPerMin = 15000000; //# of clock cycles
    per minute

    //CALCULATED VALUES
    double clocksPerQREV = clocksPerMin*(c alcAngle/360);
    double calcAngleTime = (clocksPerQREV/(rpmArray*100)) ;
    double delayTime = ((calcAngleTime *multiplier)/256) - offset;
    double delayAngle = 6*(delayTime*cl ockPeriod*(rpmA rray*100));
    double advanceAngle = leadAngle - delayAngle;
    return advanceAngle;
    }
  • Arnie

    #2
    Re: Calculation not working properly

    //CONSTANTS
    int calcAngle = 90;
    //CALCULATED VALUES
    double clocksPerQREV = clocksPerMin*(c alcAngle/360);
    Well, for a start, calcAngle which is 90, divided by 360 in integer
    division, will always be zero

    - Arnie


    Comment

    • Peter Duniho

      #3
      Re: Calculation not working properly

      On Tue, 21 Oct 2008 10:28:01 -0700, dondigitech
      <dondigitech@di scussions.micro soft.comwrote:
      I having problems getting this function to return the correct value.
      Basically I have a curveDataCalc method that loops as it should, but just
      doesn't seem to be working right. I've stepped through the function and
      the
      variables are being passed through correctly its just the clocksPerQREV
      and
      calcAngleTime are never being calculated and are always zero. [...]
      Try changing "calcAngle/360" to "calcAngle/360.0". Make sure you don't
      have any other similar examples of an integer division where you really
      wanted a floating point division (if you find any, fix them so that they
      do floating point division by making sure at least one operand is floating
      point).

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: Calculation not working properly

        Arnie wrote:
        > //CONSTANTS
        > int calcAngle = 90;
        > //CALCULATED VALUES
        > double clocksPerQREV = clocksPerMin*(c alcAngle/360);
        >
        Well, for a start, calcAngle which is 90, divided by 360 in integer
        division, will always be zero
        So take out the parentheses if you want to round to integer, or use 360.0 if
        you want fractional results.
        >
        - Arnie

        Comment

        • =?Utf-8?B?QXJ0aHVyIFBhcmtlcg==?=

          #5
          RE: Calculation not working properly



          "dondigitec h" wrote:
          I having problems getting this function to return the correct value.
          Basically I have a curveDataCalc method that loops as it should, but just
          doesn't seem to be working right. I've stepped through the function and the
          variables are being passed through correctly its just the clocksPerQREV and
          calcAngleTime are never being calculated and are always zero. This causes the
          other values to incorrectly calculated, ultimated giving me the wrong
          advanceAngle values. <snip>
          Just make all the variables involved in the calculation double, instead of
          int.

          Comment

          • MC

            #6
            Re: Calculation not working properly

            I think what's getting you is that when you put the operator / between two integers, it does integer division; thus (to take a simple example) 3/4 comes out 0 rather than 0.75. For floating-point division, at least one of the arguments needs to be a floating-point type.

            Comment

            Working...