programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

Mark Richardson
Ranch Hand
Posts: 108
2
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
• 1

Tim Cooke
Marshal
Posts: 4041
239
• 1
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
Swastik Dey wrote:

This is what I did:

Is this still acceptable?

Henry Wong
author
Sheriff
Posts: 23295
125
• 1
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
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
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
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
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

Mark Richardson
Ranch Hand
Posts: 108
2
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
Mark Richardson wrote:
This is what I did:

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
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.