Altinet
  • Home
  • Blog
  • Contact





Divide by zero and how to elegantly avoid it

On 09 May, 2014
Uncategorized
By : Bruno Samardzic
No Comments
Views : 3684

One of those late-into-production problems i usually encounter is the dreaded DivideByZero exceptions, and it’s usually quite annoying since then you usually have to do some kind of “if” or “?” statement and it gets daunting pretty fast. It got to a point where i felt a bit guilty every time i used a division. So i decided not to do divisions any more (well, atleast not directly).
I made a small extension method:


        public static decimal Inverse(this decimal d)
        {
            return d == 0 ? 0 : 1 / d;
        }

And instead of using divison, now you use multiplication. Much safer!


a=b/c;

//same way
a=b*c.Inverse()

That’s it!



Previous Post Next Post 

About The Author

Bruno Samardzic


Number of Posts : 45
All Posts by : Bruno Samardzic

Leave a Comment

Click here to cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>





Recent Posts

  • Angular vs React: round 2
  • Why programming is hard
  • Orchard Workflows, undocumented gem
  • The React Redux
  • React.js invoicing CRUD application, part 2: Description

Recent Comments

  • Angshuman on Animation for starred grid rows and columns using xaml
  • Advanced Excel Training in Lucknow on Angular vs React: round 2
  • Reginald Sophomore on My example of Angularjs directive
  • Slobodan on Angular and Breeze – story so far.
  • Bruno Samardzic on Reaction to Ember-forged Durandal Angularly Knocking out my Backbone, or my JS MVC framework research conclusion.

Contact

Altinet d.o.o.

OIB: 97429091194

Čulinečka cesta 146, Zagreb

Tel: +385 (1) 2946 819

Mob: +385 (98) 210 756

IBAN: HR4323400091110406470

Latest tweets