0
double a = 10.0;
double b =3; 
double c = a/b;
System.out.println(c*b);//answer = 10.0

BigDecimal bigDecimal1 = BigDecimal.valueOf(c);
BigDecimal bigDecimal2 = new BigDecimal("3");
System.out.println(bigDecimal1.multiply(bigDecimal2));//answer = 10.0000000000000005

I'm trying to make a calculator, but there's a problem with 10/3*3 I don't want to just calculate 10/3*3 this formula, I want to return this formula plus 0.2323232323232 of the float. So use the BigDecimal class. There's something wrong with it I couldn't get the exact result, that's what I wanted 10, Rather than get 10.0000000000000005

WeFUN
  • 1
  • 2
  • `There's something wrong with it I couldn't get the exact result, that's what I wanted 10, and got 10` So, where is the problem? – Procrastinator Oct 10 '17 at 10:11
  • sorry,I want to get 10(only used BigDecimal) – WeFUN Oct 10 '17 at 10:17
  • The core problem is that there is no decimal or binary fraction that is exactly equal to 10/3. You can get arbitrary close in BigDecimal, but not be exact. If you need exact representation of arbitrary fractions, you need a rational number package. – Patricia Shanahan Oct 10 '17 at 10:25
  • Is there no ready-made class library? – WeFUN Oct 10 '17 at 10:33
  • 1
    Last time I checked, my calculator could not represent 10.0 / 3 exactly either. Unfortunate finite floating-point representations are broken. What you really need is an infinite representation, and a machine with infinite memory to hold it ......... :-) – Stephen C Oct 10 '17 at 10:53
  • @Stephen C I think try trying to catch exceptions(ArithmeticException) This method divide public BigDecimal divide(BigDecimal divisor) Maybe it can solve the problem. – WeFUN Oct 10 '17 at 12:43
  • 1
    Nope. What you are running up against here is **Mathematics**. There is no finite floating point representation for 10.0 / 3 in either base 10 or base 2. – Stephen C Oct 10 '17 at 13:04
  • Well, you're right to follow your logic. Your serious (mathematics) makes me lol (my English-- machine translation). – WeFUN Oct 10 '17 at 13:22

1 Answers1

1

I believe your problem may be here

double c = a/b;
...
BigDecimal bigDecimal1 = BigDecimal.valueOf(c);

You're expecting that a double can perfectly represent 10/3, and I doubt it can

Maybe try something like this, which always represents numbers as BigDecimal

new BigDecimal("10").divide(new BigDecimal("3"))

At which point you'll notice that 10/3 is not representable as a decimal

Non-terminating decimal expansion; no exact representable decimal result

You need to decide how much precision you want, and then use rounding

new BigDecimal("10")
    .setScale(10)
    .divide(new BigDecimal("3"), BigDecimal.ROUND_HALF_EVEN)

Or you could use a rational number library, as suggested by Patricia. Perhaps see Is there a commonly used rational numbers library in Java?

ptomli
  • 11,730
  • 4
  • 40
  • 68
  • System.out.println(new BigDecimal("10").setScale(10).divide(new BigDecimal("3"), BigDecimal.ROUND_HALF_EVEN).multiply(new BigDecimal("3"))); i dont get 10 ,I get 9.9999999999 Is there any solution? – WeFUN Oct 10 '17 at 10:27
  • @WeFUN The solution to needing exact, not just closely approximate, representation of arbitrary fractions is to use a rational number package. – Patricia Shanahan Oct 10 '17 at 10:42