How To Avoid Divide By Zero Error In C#
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company attempted to divide by zero in c# Business Learn more about hiring developers or posting ads with us Stack Overflow Questions
C# Catch Divide By Zero Exception
Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, c# divide by just like you, helping each other. Join them; it only takes a minute: Sign up How to prevent division by zero? up vote 3 down vote favorite 1 ads = ads.Where(x => (x.Amount - x.Price) / stack memory exception c# (x.Amount / 100) >= filter.Persent); if x.Amount == 0 I have error "Divide by zero error encountered." like me in this request is to avoid? update: this helped, but I do not like the decision: ads = ads.Where(x => (x.Amount - x.Price) / ((x.Amount / 100)==0?0.1:(x.Amount / 100)) >= filter.Persent); there is another way? c# linq divide-by-zero share|improve this question edited Oct 1 '12 at 5:53 Aziz Shaikh 11.6k73954 asked Apr 20 '12 at
C# Divide By Zero Infinity
9:16 Mediator 4,6231874138 What's the type of Amount? –CodesInChaos Apr 20 '12 at 9:18 Can't you put it in an if statement and only run that code when x.Amount > 0 ? –Adam Apr 20 '12 at 9:18 1 I think you should wonder why do you have items with 0 Amount :P –Adrian Iftode Apr 20 '12 at 9:20 1 In what business is Amount - Price a meaningful quantity? –Henk Holterman Apr 20 '12 at 9:36 @Adrian Iftode, this field can be set to 0 –Mediator Apr 20 '12 at 9:57 | show 1 more comment 2 Answers 2 active oldest votes up vote 10 down vote accepted ads = ads.Where(x => x.Amount != 0 && (x.Amount - x.Price) / (x.Amount / 100) >= filter.Persent); share|improve this answer answered Apr 20 '12 at 9:18 Jon 272k40476596 x.Amount > 0 seems just a tad safer. –Henk Holterman Apr 20 '12 at 9:37 it certainly would not help –Mediator Apr 20 '12 at 9:55 1 @simplydenis: What would not help? –Jon Apr 20 '12 at 10:00 1 @simple denis why wouldn't it help? do you need to include items with amout == 0? in that case what should D be in (-x.Price / D) >= fil
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of a function procedure call can be used anywhere that a variable of the same data type could be used. this site About Us Learn more about Stack Overflow the company Business Learn zero divided by zero c# more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Divide By Zero Error Encountered In C#
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Try http://stackoverflow.com/questions/10243382/how-to-prevent-division-by-zero catch in c# for divide by zero error up vote 0 down vote favorite How to do a try catch in c# so that I execute a sql query inside the try catch Sometimes the value of count is 0, and it throws a error divide by zero error. So when it throws the divide by zero error I have to execute a http://stackoverflow.com/questions/8093919/try-catch-in-c-sharp-for-divide-by-zero-error sql statement to delete that statement and and the loop has to continue to get the value of the next record. How can i do it. double value = (read * 100 / count); c# error-handling share|improve this question asked Nov 11 '11 at 12:21 Mark 1,069143575 Can you show some more code about the sql statement? –JP Hellemons Nov 11 '11 at 12:23 Why is value a double when the result of your calculation is an int? –CodesInChaos Nov 11 '11 at 12:31 catch(DivideByZeroException ex), but as many other write, theres no reason to use try catch... use if instead –sasjaq Nov 11 '11 at 12:34 add a comment| 7 Answers 7 active oldest votes up vote 21 down vote accepted Why doing a try/catch when you can simply test whether the value of count is equal to 0: if (count != 0) { // perform the division only if count is different than 0, // otherwise we know that it will throw an exception // so why even attempting it? double value = (read * 100 / count); } share|improve
.NET Framework > Visual C# Question 0 Sign in to vote I am building an app that generates a lot of percentages, so of course I have to deal with divide by zero issues. I can create a utility method that takes in https://social.msdn.microsoft.com/Forums/vstudio/en-US/ce8072bf-3e53-4cd7-a90d-67a0ae20735c/exception-handler-for-divide-by-zero?forum=csharpgeneral the values and returns a valid value if a divide by zero is encountered, but I was hoping to implement something global. Take this example: double completedOnTime = (double)completedOnTime / completedTotal; There's actually 2 things I'd like to do here. One https://dotnetthoughts.wordpress.com/2007/10/20/doubles-decimals-and-dividing-by-zero/ probably isn't possible, and the other isn't looking good, either. First, if I don't cast one of the operands to double, I get a truncated integer (the operands are both integers). Since I'm assigning the division result to a double variable, divide by it certainly would be nice if C# figured out that I wanted a double value from the operation, and not just convert the truncated integer to a double afterwards. I do a lot of programming in ASP.NET MVC with C#, and there's a lot of programming by convention going on there, and I'll admit it's spoiled me a bit. It's "understood" that you mean to do some things by default, so there's no need to spell very obvious things out. I can't divide by zero think of many instances in which you'd want to assign a truncated integer result to a double variable. But that's a side-rant, and not my question (unless someone knows how to get around this - I'm always looking for ways to clean up and minimize my code). What I really want is to automatically return zero in any case that a divide by zero is encountered. What I do NOT want to do is write a separate try/catch handler for every single place in my app that a percentage is calculated. As I mentioned above, I could write a utility method and pass all my values through it to calculate percentages, and do the check/return there. That will still require adding references and either instantiating a utility class or calling a static utility method. Sometimes this is still an ugly solution, like the DaysInMonth method of the DateTime class. I wrote an extension method to get around it, because I found the calls unecessarily verbose. // The normal way of calling this method: DateTime someDate = DateTime.Now; int daysInMonth = DateTime.DaysInMonth(someDate.Year, someDate.Month); // My extension method: ///
by zero" often comes up as a search criteria when this blog is hit. (I use division by zero a lot when I'm writing about errors and exception handling. It's easy to create, and it's easy to understand.) I couldn't quite figure out exactly why anybody would be querying on it directly. I think I've figured it out, though. Last week, I was typing up a division example for the blog. For whatever reason, I used double instead of decimal for my input parameters and return value.
private static double Divide(double i, double j) { return (i / j); }
When I passed in values of 5 and 0 for this method, I was expecting a divide by zero exception. Instead, my console app ran just fine and printed the word Infinity on my screen. I was totally caught off guard by this result. If you pull out an old calculus book, you'll find that mathematicians often will say that the value of 1/x, as x approaches 0, is infinity (a standard limit), but that 1/0 is undefined. (You can't take one object and break it into groups of zero.) When I run the same code above using decimals, instead of doubles, I get a divide by zero exception. This is what I would expect. With a little bit of thought and poking around, I think that there is a method behind the madness. Doubles are floating point types. These types are specifically engineered never to throw an exception. Instead, they return values such as infinity, positive infinity, negative infinity, and not a number. Why? I suspect that the reason has to do with precision. The double type supports numbers as small as +-5*10-324 and as large as +-1.7*10308. Its precision, however is only 15 to 16 digits. Due to the way that floating points are handled, you can't ever be totally sure exactly what value your Double contains for very large or small numbers. For example, the following code prints out a value of 9.99988867182683E-321 in my output window when I run it:
private static void DoublePrecision() { do