473,405 Members | 2,262 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,405 software developers and data experts.

overflow and implementation concerns

Hi

Here's a couple of functions that could do with a bit of a review! The
intent is pretty obvious but am worried about (whether I can do
anything about):

- the overflow in the multiplication steps
- the consequences of the cast to int in the 2nd function
- incorrect typing e.g. 2nd function returning an int - maybe should
be a double as well??

static double Distance(PointF pointOne, PointF pointTwo)
{
double dx = pointOne.X - pointTwo.X;
double dy = pointOne.Y - pointTwo.Y;
return Math.Sqrt(dx*dx + dy*dy);
}

static int Distance(Point pointOne, Point pointTwo)
{
double dx = pointOne.X - pointTwo.X;
double dy = pointOne.Y - pointTwo.Y;
return (int)Math.Sqrt(dx*dx + dy*dy);
}

Any help appreciated!

Emma Middlebrook
em**************@fastmail.fm
Nov 15 '05 #1
1 1008
Emma,

In my opinion, you should only have one Distance method, which takes a
double and returns a double (or you can use Decimals, if you really have a
set of large numbers). The reason for this is that the square root of an
integral number is not always going to produce an integral number.

As for what you can do about overflow, the only way you can handle this
is by having the return value be a type that can handle the possible values,
given the input. Given that ultimately you are performing a square root
operation, the double return type should be fine.

However, you do have to worry about squaring the x and y coordinates of
the deltas to get the distance. If you have any OverflowExceptions being
thrown, it will be there.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"emma middlebrook" <em**************@fastmail.fm> wrote in message
news:e2*************************@posting.google.co m...
Hi

Here's a couple of functions that could do with a bit of a review! The
intent is pretty obvious but am worried about (whether I can do
anything about):

- the overflow in the multiplication steps
- the consequences of the cast to int in the 2nd function
- incorrect typing e.g. 2nd function returning an int - maybe should
be a double as well??

static double Distance(PointF pointOne, PointF pointTwo)
{
double dx = pointOne.X - pointTwo.X;
double dy = pointOne.Y - pointTwo.Y;
return Math.Sqrt(dx*dx + dy*dy);
}

static int Distance(Point pointOne, Point pointTwo)
{
double dx = pointOne.X - pointTwo.X;
double dy = pointOne.Y - pointTwo.Y;
return (int)Math.Sqrt(dx*dx + dy*dy);
}

Any help appreciated!

Emma Middlebrook
em**************@fastmail.fm

Nov 15 '05 #2

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

Similar topics

44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
9
by: Enrico 'Trippo' Porreca | last post by:
I believe there can be an integer overflow, without a silent wrap-around, in the following example: int a = INT_MAX; a++; Am I right? Could this lead to an abnormal program termination in...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
10
by: Alexander Malkis | last post by:
What is the biggest float number f so that -f doesn't overflow and is still representable as a float. -- Best regards, Alex. PS. To email me, remove "loeschedies" from the email address...
7
by: Christian Christmann | last post by:
Hi, I would like to hear your opinion about the C standards. When I have a signed char a = 130; I explicitly generate an overflow since the maximum value a singned char can accommodate...
1
by: Steve | last post by:
My question is how to handle and where to handle integer overflow exceptions. I am developing a C# application and I am debating on whether we should turn on the "Check for arithmetic overflow"...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
8
by: starffly | last post by:
In my program, the caculated value is supposed to be no more than the constant named MAXINT,otherwise, overflow error will be informed.however, I cannot test if the value exceeds MAXINT within the...
30
by: Angel Tsankov | last post by:
Hello! Does the C++ standard define what happens when the size argument of void* operator new(size_t size) cannot represent the total number of bytes to be allocated? For example: struct S {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
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,...

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.