Page 1 of 1

Safe Division

Posted: Thu Jun 27, 2013 1:53 pm
by dandymcgee
Saw this today. I don't even.. I'm speechless.
private double SafeDivide(double p_fVal1, double p_fVal2)
{//will not fail if dividing by zero

    double fReturn = 0;

    try
    {

        if ((p_fVal1 == 0) || (p_fVal2 == 0))
        {
            fReturn = 0;
        }

        fReturn = p_fVal1 / p_fVal2;

    }
    catch
    {
    }
    return fReturn;
}

Re: Safe Division

Posted: Thu Jun 27, 2013 2:02 pm
by bbguimaraes
The best part is "p_fVal1 == 0". Gotta save those FPU cycles.

Re: Safe Division

Posted: Thu Jun 27, 2013 3:44 pm
by Falco Girgis
I don't want to live on this planet anymore... :nono:

Re: Safe Division

Posted: Thu Jun 27, 2013 3:45 pm
by Tim Wilson
Oh dear... :cry:

Re: Safe Division

Posted: Thu Jun 27, 2013 4:03 pm
by Rebornxeno
I've been looking for just this! Thanks!

Re: Safe Division

Posted: Thu Jun 27, 2013 5:12 pm
by dandymcgee
Rebornxeno wrote:I've been looking for just this! Thanks!
No problem, just don't forget to add to the end:
system("pause");

Re: Safe Division

Posted: Thu Jun 27, 2013 8:52 pm
by Falco Girgis
Don't forget a few of these as well...

Code: Select all

__asm__ __volatile__ ("nop");

Re: Safe Division

Posted: Fri Jun 28, 2013 1:52 pm
by YourNerdyJoe
Today was a good day, then I saw this.

Re: Safe Division

Posted: Tue Oct 14, 2014 2:25 pm
by dandymcgee
Found this same function copy/pasted in another project today. *dies a little bit more inside*

Re: Safe Division

Posted: Thu Oct 23, 2014 3:20 pm
by YourNerdyJoe
dandymcgee wrote:Found this same function copy/pasted in another project today. *dies a little bit more inside*
lol probably just thought "hey it has 'safe' in the name so it must be good"

Re: Safe Division

Posted: Thu Oct 30, 2014 10:21 pm
by guding12
Why set fReturn to 0 when it's going to divide by zero anyways? I rate this 0/0.

Re: Safe Division

Posted: Fri Oct 31, 2014 7:04 am
by bbguimaraes
guding12 wrote:Why set fReturn to 0 when it's going to divide by zero anyways? I rate this
Floating point exception (core dumped)

Re: Safe Division

Posted: Wed Jan 14, 2015 11:54 pm
by ChrisGorsky
That just made my night...
dandymcgee wrote:Saw this today. I don't even.. I'm speechless.
private double SafeDivide(double p_fVal1, double p_fVal2)
{//will not fail if dividing by zero

    double fReturn = 0;

    try
    {

        if ((p_fVal1 == 0) || (p_fVal2 == 0))
        {
            fReturn = 0;
        }

        fReturn = p_fVal1 / p_fVal2;

    }
    catch
    {
    }
    return fReturn;
}