Are there any errors in this BigDecimal calculation?

I am new to BigDecimal.
I tried math calculations using Java code. When I try to do the same calculations using a Google calculator, it gives me slightly different results. Which one is the most accurate ?. If the Google Calculator is more accurate, then how to improve the Java code to match it?

In the Google calculator:

134.23576185216913 - (134.23576185216913 × √ (1 - (200000000 ^ 2 ÷ 299800000 ^ 2))) = 34.2357618522

In Java:

      BigDecimal t1 = new BigDecimal(134.23576185216913);
      BigDecimal one = new BigDecimal(1);
      BigDecimal speed = new BigDecimal(200000000).pow(2);
      BigDecimal lightsSpeed = new BigDecimal(299800000).pow(2);

      MathContext mc = new MathContext(2, RoundingMode.HALF_UP);
      System.out.println(t1.subtract((t1.multiply(SquareRoot.bigSqrt(one.subtract(speed.divide(lightsSpeed,mc)))))));

The bigSqrt method from here

Result:

: 34,683856455950090090043113321827989040137639817587194953722595479211000578082422197809603912048352550091209304856567024386365147605287692511973841869226169664820176852861655846233912503305984796190828666994508412096618025960010635831985100961879858807909056676663493607611105279976694118991120829708264297254294821755138400476425886154174804687500
BUILD SUCCESSFUL ( : 0 )

+4
2

:

MathContext mc = new MathContext(2, RoundingMode.HALF_UP);

. MathContext mc = new MathContext(15, RoundingMode.HALF_UP); (), - .

:

System.out.println(speed.divide(lightsSpeed,new MathContext(2, RoundingMode.HALF_UP)));
System.out.println(speed.divide(lightsSpeed,new MathContext(15, RoundingMode.HALF_UP)));

:

0,45
0,445037630156818

+7

, . , BigDecimal , . SquareRoot , , -, .

0

Source: https://habr.com/ru/post/1540240/


All Articles