Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11164 > unrolled thread

Rounding values

Started byrefreegrata <refreegrata@yahoo.com>
First post2012-01-10 04:43 -0800
Last post2012-01-10 06:09 -0800
Articles 6 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Rounding values refreegrata <refreegrata@yahoo.com> - 2012-01-10 04:43 -0800
    Re: Rounding values Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-10 08:06 -0500
      Re: Rounding values Roedy Green <see_website@mindprod.com.invalid> - 2012-01-10 06:53 -0800
        Re: Rounding values Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-10 11:38 -0500
    Re: Rounding values Tom McGlynn <taqmcg@gmail.com> - 2012-01-10 05:17 -0800
      Re: Rounding values refreegrata <refreegrata@yahoo.com> - 2012-01-10 06:09 -0800

#11164 — Rounding values

Fromrefreegrata <refreegrata@yahoo.com>
Date2012-01-10 04:43 -0800
SubjectRounding values
Message-ID<913cfe41-9fe7-4a3e-8ce3-52d468d92a30@r5g2000yqc.googlegroups.com>
Hi list. I'm a newbie with Java, just reading about BigDecimals.
I have a question. I can't get a correct rounding operation.

For example:
----------------------------------------------------------------------------------------------
new BigDecimal(0.705).setScale(2,RoundingMode.HALF_UP);
is 0.70
----------------------------------------------------------------------------------------------
And
----------------------------------------------------------------------------------------------
new BigDecimal(0.705).round(new MathContext(2, RoundingMode.HALF_UP));
is also 0.70
----------------------------------------------------------------------------------------------

However, with other values, like 1.705, the value is rounding to 1.71;
I don't know if this happens only with values between zero to one. I
still can't replicate the problem with greatest values. Maybe don't
happens, or maybe happens with other values too. To me is a problem
don't be sure about this.

Are better ways to rounding?

Cheers. Sorry for my english, because still is too ugly and rough.

P.D.: Using Java SE 6.

[toc] | [next] | [standalone]


#11166

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-01-10 08:06 -0500
Message-ID<jehd5k$1sd$1@dont-email.me>
In reply to#11164
On 1/10/2012 7:43 AM, refreegrata wrote:
> Hi list. I'm a newbie with Java, just reading about BigDecimals.
> I have a question. I can't get a correct rounding operation.
>
> For example:
> ----------------------------------------------------------------------------------------------
> new BigDecimal(0.705).setScale(2,RoundingMode.HALF_UP);
> is 0.70
> ----------------------------------------------------------------------------------------------
> And
> ----------------------------------------------------------------------------------------------
> new BigDecimal(0.705).round(new MathContext(2, RoundingMode.HALF_UP));
> is also 0.70
> ----------------------------------------------------------------------------------------------
>
> However, with other values, like 1.705, the value is rounding to 1.71;
> I don't know if this happens only with values between zero to one. I
> still can't replicate the problem with greatest values. Maybe don't
> happens, or maybe happens with other values too. To me is a problem
> don't be sure about this.

     Try `new BigDecimal("0.705")' and similarly; note the "" marks.

     I suspect what you're seeing is not caused by BigDecimal itself,
but by small approximation errors in the representation of the `double'
values.  The system uses binary (base 2) for `double' numbers, and
just as there is no finite decimal representation of 3/7 there is no
finite binary representation of 705/1000 = 141/200.  Unable to provide
an exact 705/1000, the system does its best by giving you a nearby
value with as small an error as it can manage, but it will not be
exactly halfway between 700/1000 and 710/1000.  The damage has been
done before BigDecimal even arrives on the scene.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#11173

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-01-10 06:53 -0800
Message-ID<j6kog7h5328nbkllj688esg24hqpbvlhoo@4ax.com>
In reply to#11166
On Tue, 10 Jan 2012 08:06:58 -0500, Eric Sosman
<esosman@ieee-dot-org.invalid> wrote, quoted or indirectly quoted
someone who said :

>     I suspect what you're seeing is not caused by BigDecimal itself,
>but by small approximation errors in the representation of the `double'
>values.

for elaboration on this point, see
http://mindprod.com/jgloss/floatingpoint.html
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
If you can't remember the name of some method, 
consider changing it to something you can remember.
 

[toc] | [prev] | [next] | [standalone]


#11180

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-01-10 11:38 -0500
Message-ID<jehpib$b7j$1@dont-email.me>
In reply to#11173
On 1/10/2012 9:53 AM, Roedy Green wrote:
> On Tue, 10 Jan 2012 08:06:58 -0500, Eric Sosman
> <esosman@ieee-dot-org.invalid>  wrote, quoted or indirectly quoted
> someone who said :
>
>>      I suspect what you're seeing is not caused by BigDecimal itself,
>> but by small approximation errors in the representation of the `double'
>> values.
>
> for elaboration on this point, see
> http://mindprod.com/jgloss/floatingpoint.html

     An elaboration that begins with "My general rule is, if at
all possible, avoid floating point" may not be the best guide to
the subject.  Would you trust dining advice from someone whose
general rule was to avoid food?

	"Novice computer users solve this problem [floating-point
	inaccuracy] by implicitly trusting in the computer as an
	infallible authority; they tend to believe that all the
	digits of a printed answer are significant. Disillusioned
	computer users have just the opposite approach; they are
	constantly afraid that their answers are meaningless."
	-- DEK, TAOCP 4.2.2

Don't be a novice, but don't be so bitterly disillusioned, either.
Learn how the tools behave, and use them accordingly.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#11167

FromTom McGlynn <taqmcg@gmail.com>
Date2012-01-10 05:17 -0800
Message-ID<2f5f8143-3e24-435d-b0b3-a60235f8f811@n30g2000yqd.googlegroups.com>
In reply to#11164
On Jan 10, 7:43 am, refreegrata <refreegr...@yahoo.com> wrote:
> Hi list. I'm a newbie with Java, just reading about BigDecimals.
> I have a question. I can't get a correct rounding operation.
>
> For example:
> ----------------------------------------------------------------------------------------------
> new BigDecimal(0.705).setScale(2,RoundingMode.HALF_UP);
> is 0.70
> ----------------------------------------------------------------------------------------------
> And
> ----------------------------------------------------------------------------------------------
> new BigDecimal(0.705).round(new MathContext(2, RoundingMode.HALF_UP));
> is also 0.70
> ----------------------------------------------------------------------------------------------
>
> However, with other values, like 1.705, the value is rounding to 1.71;
> I don't know if this happens only with values between zero to one. I
> still can't replicate the problem with greatest values. Maybe don't
> happens, or maybe happens with other values too. To me is a problem
> don't be sure about this.
>
> Are better ways to rounding?
>
> Cheers. Sorry for my english, because still is too ugly and rough.
>
> P.D.: Using Java SE 6.


When the Java compiler sees the token 0.705  (or 1.705) it converts
that value into a double which may differ from the input by about 1
part in 10^15 -- most
decimal fractions cannot be represented exactly as doubles, so the
nearest available double value is used. You then convert this double
into a BigDecimal and finally round it.  But your BigDecimal value is
not 0.705, it's actually something like 0.70499999999999... and so
properly gets rounded down.

To get the behavior you expect, get rid of the intermediate double by
converting
directly from a string to BigDecimal.  E.g.,

   BigDecimal x = new BigDecimal("0.705");

Now the BigDecimal value should be exactly what you specified.

Regards,
   Tom McGynnn

[toc] | [prev] | [next] | [standalone]


#11168

Fromrefreegrata <refreegrata@yahoo.com>
Date2012-01-10 06:09 -0800
Message-ID<9ff38231-5c4e-460f-b495-57d85b89ba3f@q17g2000yqh.googlegroups.com>
In reply to#11167
Thanks guys, Within double quotes (") the problem is solved, and now I
understand why. Without the quotes is a double turned into a
BigDecimal.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web