Math.Round Question

  • Thread starter Thread starter Gonza
  • Start date Start date
G

Gonza

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

private double GetCompletedPercentage(TimeSpan totalTime, TimeSpan
budgetedTime)
{
double totalMinutes = totalTime.TotalMinutes;
double budgetedMinutes = budgetedTime.TotalMinutes;

return Math.Round((totalMinutes * 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
 
Gonza said:
Hi group, i have a method in my class with the following code:

private double GetCompletedPercentage(TimeSpan totalTime, TimeSpan
budgetedTime)
{
double totalMinutes = totalTime.TotalMinutes;
double budgetedMinutes = budgetedTime.TotalMinutes;

return Math.Round((totalMinutes * 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.
 
Hi,

you can check the IsInfinity property:

double result = Math.Round(....);
if (result.IsInfinity)
....
else
....
 
Gonza said:
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.IsInfinity(x);
bool c1 = double.IsNegativeInfinity(x);
bool d1 = double.IsPositiveInfinity(x);
bool e1 = (x == double.NegativeInfinity);
bool f1 = (x == double.PositiveInfinity);
bool g1 = (x == double.NaN);

x = -1.0 / 0.0;
bool a2 = double.IsNaN(x);
bool b2 = double.IsInfinity(x);
bool c2 = double.IsNegativeInfinity(x);
bool d2 = double.IsPositiveInfinity(x);
bool e2 = (x == double.NaN);
bool f2 = (x == double.NegativeInfinity);
bool g2 = (x == double.PositiveInfinity);

Michael
 
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
 
Hi Michael,

you also could use

Atan2(rise, run)

(though it would give different result for negative run).
 
Christof Nordiek said:
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.IEEERemainder;
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top