• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to account for divide by zero error in this method?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method:



It returns the percentage difference back.

However, it fails when the values are zeros. I don't want any exceptions thrown, but I want it to return a zero when the values are zero...

How do I do this?
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to return zero? That's mathematically incorrect. Wouldn't it be better to return Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY ?
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:



This is what I did:





Is this still acceptable?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:
However, it fails when the values are zeros. I don't want any exceptions thrown, but I want it to return a zero when the values are zero...


Dividing floating point numbers do not throw an exception in this case, if the denominator is zero, you will either get Infinity, Negative Infinitely, or NaN (not a number) as a result, depending on what is the numerator.

To answer you question, you have a few options. You can check the values, before the division, and take care of the special case. Or you can check the result, after the division, and take care of the special case.

[EDIT: Wow. Beaten to the answer, by lots of people... ]

Henry
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mark Richardson wrote:
However, it fails when the values are zeros. I don't want any exceptions thrown, but I want it to return a zero when the values are zero...


Dividing floating point numbers do not throw an exception in this case, if the denominator is zero, you will either get Infinity, Negative Infinitely, or NaN (not a number) as a result, depending on what is the numerator.

To answer you question, you have a few options. You can check the values, before the division, and take care of the special case. Or you can check the result, after the division, and take care of the special case.

Henry


When I say it fails, I really meant to say that this is going into an Excel worksheet where I'm getting the division by zero error.
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Why do you want to return zero? That's mathematically incorrect. Wouldn't it be better to return Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY ?


Hi Tim,

This small function plugs into something else which puts the values into an excel sheet. So, instead of division by zero error, I really want a zero
 
John Joe
Ranch Hand
Posts: 432
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your datatype are double. It should return 0.0. You want it return as 0 instead of 0.0 ?

Here are the example of mine



In my case, I get 0.0. Maybe you need to post your code?
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:Your datatype are double. It should return 0.0. You want it return as 0 instead of 0.0 ?

Here are the example of mine



In my case, I get 0.0. Maybe you need to post your code?



What I meant is... It can return a 0.0 - that's fine. But no exceptions or NaNs
 
John Joe
Ranch Hand
Posts: 432
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried @Swastik answer ? Did the answer worked ?
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:Have you tried @Swastik answer ? Did the answer worked ?


Yes, I tried that approach and it seems to have worked (I posted my implementation right after his post)
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:
This is what I did:



A few comments:

Use || instead of |. || is the logical OR operator, | is the bitwise OR. There's a subtle difference between the two. Unless you specifically need bitwise OR, use logical OR.

Also, but is really a minor thing, you don't need to put the (0) between parentheses. You can just write: return 0;.
Since you're dealing with double values, I'd prefer to use 0.0 (which is a double literal) instead of 0 (which is an int literal that the compiler automatically converts to double), but that's also just a matter of style.

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And note that your function does NOT convert anything to percent, nor does it return a percent difference between the two inputs.

For example, 110 is 10% greater than 100 (or, stated differently, 110 is 110% of 100).

Your function for f(110., 100.) returns 0.1
Your function for f(100., 110.) returns -.090909...

Neither of these values are percents.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!