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; }
Safe Division
Moderator: Coders of Rage
- dandymcgee
- 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
Saw this today. I don't even.. I'm speechless.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- bbguimaraes
- Chaos Rift Junior
- Posts: 294
- Joined: Wed Apr 11, 2012 4:34 pm
- Programming Language of Choice: c++
- Location: Brazil
- Contact:
Re: Safe Division
The best part is "p_fVal1 == 0". Gotta save those FPU cycles.
- Falco Girgis
- 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
I don't want to live on this planet anymore...
-
- 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
Oh dear...
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Thu Jun 23, 2011 11:12 am
Re: Safe Division
I've been looking for just this! Thanks!
- dandymcgee
- 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
No problem, just don't forget to add to the end:Rebornxeno wrote:I've been looking for just this! Thanks!
system("pause");
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- Falco Girgis
- 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
Don't forget a few of these as well...
Code: Select all
__asm__ __volatile__ ("nop");
- YourNerdyJoe
- 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
Today was a good day, then I saw this.
- dandymcgee
- 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
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!
- YourNerdyJoe
- 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
lol probably just thought "hey it has 'safe' in the name so it must be good"dandymcgee wrote:Found this same function copy/pasted in another project today. *dies a little bit more inside*
Re: Safe Division
Why set fReturn to 0 when it's going to divide by zero anyways? I rate this 0/0.
- bbguimaraes
- Chaos Rift Junior
- Posts: 294
- Joined: Wed Apr 11, 2012 4:34 pm
- Programming Language of Choice: c++
- Location: Brazil
- Contact:
Re: Safe Division
Floating point exception (core dumped)guding12 wrote:Why set fReturn to 0 when it's going to divide by zero anyways? I rate this
- ChrisGorsky
- 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
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.