Math.Round Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gonza

    Math.Round Question

    Hi group, i have a method in my class with the following code:

    private double GetCompletedPer centage(TimeSpa n totalTime, TimeSpan
    budgetedTime)
    {
    double totalMinutes = totalTime.Total Minutes;
    double budgetedMinutes = budgetedTime.To talMinutes;

    return Math.Round((tot alMinutes * 100) / budgetedMinutes ,2);
    }

    Sometimes, budgetedMinutes is zero, but the result of the operation in
    infinite?? why is that?? Can I check against a constant or something to
    see if it's infinite?? I know, i can check if budgetedMinutes == then
    return something, but i'm curious.

    Thanks in advance

  • Thomas T. Veldhouse

    #2
    Re: Math.Round Question

    Gonza <gonzalow@gmail .comwrote:
    Hi group, i have a method in my class with the following code:
    >
    private double GetCompletedPer centage(TimeSpa n totalTime, TimeSpan
    budgetedTime)
    {
    double totalMinutes = totalTime.Total Minutes;
    double budgetedMinutes = budgetedTime.To talMinutes;
    >
    return Math.Round((tot alMinutes * 100) / budgetedMinutes ,2);
    }
    >
    Sometimes, budgetedMinutes is zero, but the result of the operation in
    infinite?? why is that?? Can I check against a constant or something to
    see if it's infinite?? I know, i can check if budgetedMinutes == then
    return something, but i'm curious.
    Anytime you divide any number by zero you get infinity. Simple math.

    --
    Thomas T. Veldhouse
    Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

    Comment

    • Christof Nordiek

      #3
      Re: Math.Round Question

      Hi,

      you can check the IsInfinity property:

      double result = Math.Round(.... );
      if (result.IsInfin ity)
      ....
      else
      ....


      "Gonza" <gonzalow@gmail .comschrieb im Newsbeitrag
      news:1160571400 .545653.282660@ c28g2000cwb.goo glegroups.com.. .
      Hi group, i have a method in my class with the following code:
      >
      private double GetCompletedPer centage(TimeSpa n totalTime, TimeSpan
      budgetedTime)
      {
      double totalMinutes = totalTime.Total Minutes;
      double budgetedMinutes = budgetedTime.To talMinutes;
      >
      return Math.Round((tot alMinutes * 100) / budgetedMinutes ,2);
      }
      >
      Sometimes, budgetedMinutes is zero, but the result of the operation in
      infinite?? why is that?? Can I check against a constant or something to
      see if it's infinite?? I know, i can check if budgetedMinutes == then
      return something, but i'm curious.
      >
      Thanks in advance
      >

      Comment

      • Michael C

        #4
        Re: Math.Round Question

        "Gonza" <gonzalow@gmail .comwrote in message
        news:1160571400 .545653.282660@ c28g2000cwb.goo glegroups.com.. .
        Sometimes, budgetedMinutes is zero, but the result of the operation in
        infinite?? why is that??
        How many free burgers can you eat for a dollar? :-)
        Can I check against a constant or something to
        see if it's infinite?? I know, i can check if budgetedMinutes == then
        return something, but i'm curious.
        You can use checks like this but you are much better off checking if the
        divisor is zero.

        double x = 1.0 / 0.0;
        bool a1 = double.IsNaN(x) ;
        bool b1 = double.IsInfini ty(x);
        bool c1 = double.IsNegati veInfinity(x);
        bool d1 = double.IsPositi veInfinity(x);
        bool e1 = (x == double.Negative Infinity);
        bool f1 = (x == double.Positive Infinity);
        bool g1 = (x == double.NaN);

        x = -1.0 / 0.0;
        bool a2 = double.IsNaN(x) ;
        bool b2 = double.IsInfini ty(x);
        bool c2 = double.IsNegati veInfinity(x);
        bool d2 = double.IsPositi veInfinity(x);
        bool e2 = (x == double.NaN);
        bool f2 = (x == double.Negative Infinity);
        bool g2 = (x == double.Positive Infinity);

        Michael


        Comment

        • Christof Nordiek

          #5
          Re: Math.Round Question

          Or better:

          if (budgetMinute2 == 0)
          ....
          else
          return Math.Round(...) ;


          "Christof Nordiek" <cn@nospam.desc hrieb im Newsbeitrag
          news:eY7pJhT7GH A.1256@TK2MSFTN GP04.phx.gbl...
          Hi,
          >
          you can check the IsInfinity property:
          >
          double result = Math.Round(.... );
          if (result.IsInfin ity)
          ....
          else
          ....
          >
          >
          "Gonza" <gonzalow@gmail .comschrieb im Newsbeitrag
          news:1160571400 .545653.282660@ c28g2000cwb.goo glegroups.com.. .
          >Hi group, i have a method in my class with the following code:
          >>
          >private double GetCompletedPer centage(TimeSpa n totalTime, TimeSpan
          >budgetedTime )
          >{
          > double totalMinutes = totalTime.Total Minutes;
          > double budgetedMinutes = budgetedTime.To talMinutes;
          >>
          > return Math.Round((tot alMinutes * 100) / budgetedMinutes ,2);
          >}
          >>
          >Sometimes, budgetedMinutes is zero, but the result of the operation in
          >infinite?? why is that?? Can I check against a constant or something to
          >see if it's infinite?? I know, i can check if budgetedMinutes == then
          >return something, but i'm curious.
          >>
          >Thanks in advance
          >>
          >
          >

          Comment

          • Michael C

            #6
            Re: Math.Round Question

            "Christof Nordiek" <cn@nospam.dewr ote in message
            news:eY7pJhT7GH A.1256@TK2MSFTN GP04.phx.gbl...
            Hi,
            >
            you can check the IsInfinity property:
            >
            double result = Math.Round(.... );
            if (result.IsInfin ity)
            IsInfinity is static.

            Michael


            Comment

            • Michael C

              #7
              Re: Math.Round Question

              "Gonza" <gonzalow@gmail .comwrote in message
              news:1160571400 .545653.282660@ c28g2000cwb.goo glegroups.com.. .

              Coooool :-) I've just realised I can simplify some existing code. I've got
              some code like this to get the angle of a rise over a run:

              if(run == 0)
              {
              return (rise < 0 ? 90 : -90)
              }
              else
              {
              return Atan(rise / run) * 180 / pi;
              }


              because double can have values of -infinity or +infinity I can simplify the
              code to

              Atan(rise/run);

              Thanks for the tip!! Reading these groups it seams I learn something new
              every day :-)

              Are there any other similar things I'm missing?

              Michael


              Comment

              • Christof Nordiek

                #8
                Re: Math.Round Question

                Hi Michael,

                you also could use

                Atan2(rise, run)

                (though it would give different result for negative run).

                "Michael C" <nospam@nospam. comschrieb im Newsbeitrag
                news:OnKq1Jb7GH A.1244@TK2MSFTN GP03.phx.gbl...
                "Gonza" <gonzalow@gmail .comwrote in message
                news:1160571400 .545653.282660@ c28g2000cwb.goo glegroups.com.. .
                >
                Coooool :-) I've just realised I can simplify some existing code. I've got
                some code like this to get the angle of a rise over a run:
                >
                if(run == 0)
                {
                return (rise < 0 ? 90 : -90)
                }
                else
                {
                return Atan(rise / run) * 180 / pi;
                }
                >
                >
                because double can have values of -infinity or +infinity I can simplify
                the code to
                >
                Atan(rise/run);
                >
                Thanks for the tip!! Reading these groups it seams I learn something new
                every day :-)
                >
                Are there any other similar things I'm missing?
                >
                Michael
                >

                Comment

                • Michael C

                  #9
                  Re: Math.Round Question

                  "Christof Nordiek" <cn@nospam.dewr ote in message
                  news:OylISjc7GH A.3280@TK2MSFTN GP02.phx.gbl...
                  Hi Michael,
                  >
                  you also could use
                  >
                  Atan2(rise, run)
                  Cool, that's exactly what I need.
                  (though it would give different result for negative run).
                  That simplifies things though. Before I had 2 different checks to see what
                  quadrant the result was in, now I only need to add 2pi if the result is less
                  than zero. Thanks for the tip.

                  I also noticed
                  Math.DivRem;
                  Math.IEEERemain der;
                  Math.BigMul;

                  Not sure how useful BigMul is but the other 2 could be useful, saves doing a
                  mod and a divide in some cases.

                  Michael


                  Comment

                  Working...