473,783 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Oct 11 '06 #1
8 4010
Gonza <go******@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

Oct 11 '06 #2
Hi,

you can check the IsInfinity property:

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

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

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

you can check the IsInfinity property:

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


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

you can check the IsInfinity property:

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

Michael
Oct 12 '06 #6
"Gonza" <go******@gmail .comwrote in message
news:11******** **************@ 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
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******** ******@TK2MSFTN GP03.phx.gbl...
"Gonza" <go******@gmail .comwrote in message
news:11******** **************@ 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

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

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

Similar topics

2
3954
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 - (5/15) * 90) * (System.Math.PI / 180);
6
7100
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 = Math.Round( original, 2 ); But I'm not happy with the results. E.g. if original is
12
1689
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 but only because I have figured into the equation (manually) the approximate number of leap years. The code reads: Dim TS As New TimeSpan Dim NumDays As Integer
10
12961
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 round(123.465,2) returns
1
1559
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 (S.length<2) S='0'+S var temp2 = S.substr(0, T=(S.length-2)) + '.' + S.substr(T, 2); temp2 = 1.02
10
16036
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) 0.74
6
9113
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' is odd However, if format.NumberDecimalDigits is 1 decimal d = 3.45M; d.ToString( "F", format ); //Return 3.5 - this is different from Math.Round;
4
10897
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
5619
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 to complete this part to move onto the next. <HTML> <HEAD> <TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.