• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal divide operation not preserving the sign

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but not once you round.

If you keep everything as an approximation then find.

But once you round 0.0 and +0.0 and -0,0 has no meaning.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Fahlbusch wrote:but not once you round.

If you keep everything as an approximation then find.

But once you round 0.0 and +0.0 and -0,0 has no meaning.


Not sure I follow. Are you saying that the distinction between -0.0 and +0.0 is never useful?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic