473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Math.Round Question

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

Oct 11 '06 #1
8 3991
Gonza <go******@gmail.comwrote:
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.

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

Oct 11 '06 #2
Hi,

you can check the IsInfinity property:

double result = Math.Round(....);
if (result.IsInfinity)
....
else
....
"Gonza" <go******@gmail.comschrieb im Newsbeitrag
news:11**********************@c28g2000cwb.googlegr oups.com...
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

Oct 11 '06 #3
"Gonza" <go******@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.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.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
Oct 11 '06 #4
Or better:

if (budgetMinute2 == 0)
....
else
return Math.Round(...);
"Christof Nordiek" <cn@nospam.deschrieb im Newsbeitrag
news:eY**************@TK2MSFTNGP04.phx.gbl...
Hi,

you can check the IsInfinity property:

double result = Math.Round(....);
if (result.IsInfinity)
....
else
....
"Gonza" <go******@gmail.comschrieb im Newsbeitrag
news:11**********************@c28g2000cwb.googlegr oups.com...
>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


Oct 11 '06 #5
"Christof Nordiek" <cn@nospam.dewrote in message
news:eY**************@TK2MSFTNGP04.phx.gbl...
Hi,

you can check the IsInfinity property:

double result = Math.Round(....);
if (result.IsInfinity)
IsInfinity is static.

Michael
Oct 12 '06 #6
"Gonza" <go******@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.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
Oct 12 '06 #7
Hi Michael,

you also could use

Atan2(rise, run)

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

"Michael C" <no****@nospam.comschrieb im Newsbeitrag
news:On**************@TK2MSFTNGP03.phx.gbl...
"Gonza" <go******@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.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

Oct 12 '06 #8
"Christof Nordiek" <cn@nospam.dewrote in message
news:Oy**************@TK2MSFTNGP02.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.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
Oct 12 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nicolas | last post by:
Why does for the same code I got different result the VB code gave me what I want why not the csharp? Thank you for your help... CSHARP CODE int sx, sy; double sdegrees; sdegrees = (90 -...
6
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result =...
12
by: Woody Splawn | last post by:
I am trying to determine the age of a person based on two dates, the Date of Birth and Today(). I have a function that does this but it is kludgey and is giving me an age that is pretty close...
10
by: tmeister | last post by:
I'm trying to determine the best approach for rounding in an application I'm building. Unfortunately it appears as though SQL Server and VB.NET round in different ways. SQL Server select...
1
by: Richard Cranium | last post by:
Using the number 1.015 ***************************** the following script returns 1.02, which is correct var T; var S=new String(Math.round(parseFloat(1.015).toFixed(2)*100)); while...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
6
by: Zeng | last post by:
Math.Round has good behavior as following: Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is even Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
by: DaiOz | last post by:
Hi guys im doing a course which includes some JavaScript and I need help with the following question please, The code I have so far is below but I don't think its right please help as I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.