10
ads = ads.Where(x => (x.Amount - x.Price) / (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?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Mediator
  • 14,951
  • 35
  • 113
  • 191

2 Answers2

30

Of course, you can always implement a generic safe division method and use it all the way

using System;

namespace Stackoverflow
{
    static public class NumericExtensions
    {
        static public decimal SafeDivision(this decimal Numerator, decimal Denominator)
        {
            return (Denominator == 0) ? 0 : Numerator / Denominator;
        }
    }

}

I have chosen decimal type because it addresses all non nullable numeric types that I am aware of.

Usage:

var Numerator = 100;
var Denominator = 0;

var SampleResult1 = NumericExtensions.SafeDivision(Numerator , Denominator );

var SampleResult2 = Numerator.SafeDivision(Denominator);
Julio Nobre
  • 4,196
  • 3
  • 46
  • 49
  • 1
    +1 for the thought, but I don't think it should return zero if the denominator is zero.... the actual value is closer to +-infinity unless the numerator is also zero, in which case it is undefined. – user420667 Oct 10 '13 at 19:45
  • 1
    Yes, user420667, I agree with you. While the suggested solution may not fit all real-world situations, I think it fits most of them. Nonetheless, one should always weight the consequences on using a fallback value. Anyway, thanks for your thought :D – Julio Nobre Oct 11 '13 at 00:31
  • 1
    I personally would return Numerator as the fallback value because many times as a programmer, you are grabbing counts from the DB that you want to divide. If COUNT(N) = 8 and COUNT(D) = 0, just because D has not happened yet, returning the value 8 is closer to 8 / 1, which is your closest real number. – ScubaSteve Jan 23 '15 at 18:28
10
ads = ads.Where(x => x.Amount != 0 &&
                    (x.Amount - x.Price) / (x.Amount / 100) >= filter.Persent);
Jon
  • 428,835
  • 81
  • 738
  • 806
  • `x.Amount > 0` seems just a tad safer. – H H Apr 20 '12 at 09:37
  • 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) >= filter.Persent (btw shouldn't that be percent as in %?) – Rune FS Apr 20 '12 at 10:01
  • @HenkHolterman who's to say which rules guides the business after all Amount - Price makes sense in this particular case why would a negative Amount then? (though in general I agree with you) – Rune FS Apr 20 '12 at 10:02
  • 1
    @simplydenis: What error? (I have seen the update to the question: *do not do that, it looks really bad*. Such solutions indicate that the problem has not been understood well enough, or that you are trying to do something which does not make sense. What is your goal here? Don't forget about the [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Jon Apr 20 '12 at 10:32