Safe Division

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Safe Division

Post 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;
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Safe Division

Post by bbguimaraes »

The best part is "p_fVal1 == 0". Gotta save those FPU cycles.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Safe Division

Post by Falco Girgis »

I don't want to live on this planet anymore... :nono:
Tim Wilson
ES Beta Backer
ES Beta Backer
Posts: 9
Joined: Tue Jan 22, 2013 2:20 pm
Programming Language of Choice: C++ & Python
Location: Western Pennsylvania

Re: Safe Division

Post by Tim Wilson »

Oh dear... :cry:
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Safe Division

Post by Rebornxeno »

I've been looking for just this! Thanks!
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Safe Division

Post 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");
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Safe Division

Post by Falco Girgis »

Don't forget a few of these as well...

Code: Select all

__asm__ __volatile__ ("nop");
User avatar
YourNerdyJoe
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 79
Joined: Sun Oct 02, 2011 3:28 pm
Current Project: Top secret (not really) Top-Down Shooter for GBA
Favorite Gaming Platforms: GBA, Gamecube, PC, 3DS
Programming Language of Choice: C/C++
Contact:

Re: Safe Division

Post by YourNerdyJoe »

Today was a good day, then I saw this.
See that?.....
Exactly
https://yournerdyjoe.github.io/
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Safe Division

Post by dandymcgee »

Found this same function copy/pasted in another project today. *dies a little bit more inside*
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
YourNerdyJoe
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 79
Joined: Sun Oct 02, 2011 3:28 pm
Current Project: Top secret (not really) Top-Down Shooter for GBA
Favorite Gaming Platforms: GBA, Gamecube, PC, 3DS
Programming Language of Choice: C/C++
Contact:

Re: Safe Division

Post 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"
See that?.....
Exactly
https://yournerdyjoe.github.io/
guding12
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Thu Oct 30, 2014 10:13 pm

Re: Safe Division

Post by guding12 »

Why set fReturn to 0 when it's going to divide by zero anyways? I rate this 0/0.
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

Re: Safe Division

Post 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)
User avatar
ChrisGorsky
ES Beta Backer
ES Beta Backer
Posts: 7
Joined: Tue Jan 13, 2015 9:37 pm
Programming Language of Choice: C++
Location: Long Island, New York

Re: Safe Division

Post 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;
}
Programmer, Tae Kwon Do enthusiast, fashion expert.
Post Reply