File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes BigDecimal divide operation not preserving the sign Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "BigDecimal divide operation not preserving the sign" Watch "BigDecimal divide operation not preserving the sign" New topic
Author

BigDecimal divide operation not preserving the sign

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 516
    
    2

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

Regards
KumarRaja

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Sep 18, 2000
Posts: 555
    
    7

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Sep 18, 2000
Posts: 555
    
    7

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigDecimal divide operation not preserving the sign
 
Similar Threads
Fractions
Wrong output
Don't know where to start???
java program using a class fraction
BigDecimal issue