# BigDecimal divide operation not preserving the sign

posted 3 years ago

Hi All,

I have an operation like below. As we see the numerator is -ve and denominator is +ve, so the division operation should have resulted in -ve. However, when I run the below code I get 0.0. I understand that 0 does not have any sign, and since I'm rounding up the value to 2 positions after decimal, I end up getting 0.0. If I increase the scale more than 2, then I get -ve. But my requirement is to scale to 2 positions, but yet preserve the sign. As a work around, I can determine the sign explicitly looking at the signs of numerator and denominator, but wondering if there is any other way with in BigDecimal api.

Also

Why BigDecimal is behaving differently from Double

Thanks

I have an operation like below. As we see the numerator is -ve and denominator is +ve, so the division operation should have resulted in -ve. However, when I run the below code I get 0.0. I understand that 0 does not have any sign, and since I'm rounding up the value to 2 positions after decimal, I end up getting 0.0. If I increase the scale more than 2, then I get -ve. But my requirement is to scale to 2 positions, but yet preserve the sign. As a work around, I can determine the sign explicitly looking at the signs of numerator and denominator, but wondering if there is any other way with in BigDecimal api.

Also

Why BigDecimal is behaving differently from Double

Thanks

Regards

KumarRaja

Mike Simmons

Ranch Hand

Posts: 3090

14

posted 3 years ago

BigDecimal has no way to represent the concept of -0.0. It also can't represent values like Double.POSITIVE_INFINITY or Double.NaN - these throw exceptions when you try to construct them. But a double -0.0 just gets converted into a BigDecimal 0, because for most purposes, that's the same thing. What sort of application are you writing where someone thinks it's important to distinguish between 0.0 and -0.0? If that's really necessary, you'll need to either (a) not use BigDecimal, or (b) maintain the sign of the result separately from the BigDecimal value.

posted 3 years ago

there used to be Cyber computers that would have a + bit assigned for positive values. it would have another bit for - and it would have a third bit for assumed positive but we dont know.

After many, many years of use they found it doesn't make sense.

So you round to 0.0 if you do there is no less than zero.

So either print the closest value or don't worry.

And by the way what is the difference between 0.0 and -0.0?

And how many folks are going to be confused with seeing -0.0?

-steve

After many, many years of use they found it doesn't make sense.

So you round to 0.0 if you do there is no less than zero.

So either print the closest value or don't worry.

And by the way what is the difference between 0.0 and -0.0?

And how many folks are going to be confused with seeing -0.0?

-steve

posted 3 years ago

The reason floating point types make the distinction is so that when a calculation brings us closer to zero than their precision can represent, we at least know a) that it's not quite zero, and b) which side of zero it's on.

Steve Fahlbusch wrote:

And by the way what is the difference between 0.0 and -0.0?

And how many folks are going to be confused with seeing -0.0?

The reason floating point types make the distinction is so that when a calculation brings us closer to zero than their precision can represent, we at least know a) that it's not quite zero, and b) which side of zero it's on.

Campbell Ritchie

Sheriff

Posts: 50168

79

posted 3 years ago

Java supports three kinds of arithmetic: integer (int, etc), floating‑point (double, etc) and decimal in BigDecimal. Floating‑point arithmetic has certain features (eg -0.0, ±∞) which the other two types do not support. Don’t try to mix floating‑point arithmetic and decimal arithmetic.

posted 3 years ago

I would list them as integer, floating point, and fixed point. The floating point happens to be base-2 and the fixed point happens to be base-10 (decimal), but radix and float/fixed are orthogonal.

Campbell Ritchie wrote:Java supports three kinds of arithmetic: integer (int, etc), floating‑point (double, etc) and decimal in BigDecimal.

I would list them as integer, floating point, and fixed point. The floating point happens to be base-2 and the fixed point happens to be base-10 (decimal), but radix and float/fixed are orthogonal.