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

Safe Division

Postby dandymcgee on Thu Jun 27, 2013 2:53 pm

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
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Re: Safe Division

Postby bbguimaraes on Thu Jun 27, 2013 3:02 pm

The best part is "p_fVal1 == 0". Gotta save those FPU cycles.
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
 
Posts: 324
Joined: Wed Apr 11, 2012 5:34 pm
Location: Brazil

Re: Safe Division

Postby Falco Girgis on Thu Jun 27, 2013 4:44 pm

I don't want to live on this planet anymore... :nono:
"So what happens if the Elysian Shadows Kickstarter fails?"
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
 
Posts: 10693
Joined: Thu May 20, 2004 3:04 pm
Location: Studio Vorbis, AL

Re: Safe Division

Postby Tim Wilson on Thu Jun 27, 2013 4:45 pm

Oh dear... :cry:
Tim Wilson
ES Beta Backer
ES Beta Backer
 
Posts: 10
Joined: Tue Jan 22, 2013 4:20 pm
Location: Western Pennsylvania

Re: Safe Division

Postby Rebornxeno on Thu Jun 27, 2013 5:03 pm

I've been looking for just this! Thanks!
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
 
Posts: 85
Joined: Thu Jun 23, 2011 12:12 pm

Re: Safe Division

Postby dandymcgee on Thu Jun 27, 2013 6:12 pm

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
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Re: Safe Division

Postby Falco Girgis on Thu Jun 27, 2013 9:52 pm

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

Code: Select all
__asm__ __volatile__ ("nop");
"So what happens if the Elysian Shadows Kickstarter fails?"
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
 
Posts: 10693
Joined: Thu May 20, 2004 3:04 pm
Location: Studio Vorbis, AL

Re: Safe Division

Postby YourNerdyJoe on Fri Jun 28, 2013 2:52 pm

Today was a good day, then I saw this.
User avatar
YourNerdyJoe
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
 
Posts: 83
Joined: Sun Oct 02, 2011 4:28 pm

Re: Safe Division

Postby dandymcgee on Tue Oct 14, 2014 3:25 pm

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
dandymcgee
ES Beta Backer
ES Beta Backer
 
Posts: 4911
Joined: Tue Apr 29, 2008 4:24 pm
Location: New Hampshire

Re: Safe Division

Postby YourNerdyJoe on Thu Oct 23, 2014 4:20 pm

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"
User avatar
YourNerdyJoe
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
 
Posts: 83
Joined: Sun Oct 02, 2011 4:28 pm

Re: Safe Division

Postby guding12 on Thu Oct 30, 2014 11:21 pm

Why set fReturn to 0 when it's going to divide by zero anyways? I rate this 0/0.
guding12
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 4
Joined: Thu Oct 30, 2014 11:13 pm

Re: Safe Division

Postby bbguimaraes on Fri Oct 31, 2014 8:04 am

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
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
 
Posts: 324
Joined: Wed Apr 11, 2012 5:34 pm
Location: Brazil

Re: Safe Division

Postby ChrisGorsky on Thu Jan 15, 2015 1:54 am

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.
User avatar
ChrisGorsky
ES Beta Backer
ES Beta Backer
 
Posts: 7
Joined: Tue Jan 13, 2015 11:37 pm
Location: Long Island, New York


Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron