0
  • Is there a loss in precision when using BigDecimal multiplied by sin, cos, tan or any other trigonometric methods?
  • Is it valid to use BigDecimal with rational numbers?
  • I know, BigDecimal is used for "precision currency calculations" (Java BigDecimal trigonometric methods). I know, there are projects like https://github.com/eobermuhlner/big-math or http://www.apfloat.org/. Where lies the difference when using double for that without BigDecimal? Because having BigDecimal and converting to double only because I need the exp of it, does this makes sense. Precision is lost when using trigonometric methods. Or not?

The question is: Does it even make sense to use BigDecimal with "rational numbers"?

nimo23
  • 5,170
  • 10
  • 46
  • 75
  • I am sure you are aware that storing any real number in memory is inherently imprecise to an extent. What level of imprecision are you detecting? How precise do you need your calculations? – OldCurmudgeon Nov 23 '17 at 14:30
  • Actually, I am using; MathContext MATH_CONTEXT = new MathContext(40, RoundingMode.HALF_UP); and want to hold that precision. Do I need these kind of external libs or does java takes care of these when using its trigonometric methods? – nimo23 Nov 23 '17 at 14:38
  • "storing any real number in memory is inherently imprecise to an extent"..what is the extent? Does it relate to the processor? – nimo23 Nov 23 '17 at 14:39
  • 1
    It's nothing to do with the processor. How would you store pi in any data structure you like? How would you store 1/3 as a BigDecimal? Obviously, using a specified precision. – DodgyCodeException Nov 23 '17 at 14:48
  • So when I want to retain "new MathContext(40, RoundingMode.HALF_UP);" with trigonometric methods. How should I do this. I must convert to double to use these kind of maths. Hence, original precision is lost. Or? – nimo23 Nov 23 '17 at 14:54
  • 1
    You'll have to write your own trig functions from scratch using methods such as BigDecimal.add and BigDecimal.multiply. Just like how the standard Java.lang.Math trig functions have been implemented. But be aware that your BigDecimal trig functions will be much, much slower than the java.lang.Math methods. – DodgyCodeException Nov 23 '17 at 15:08
  • Thanks. Now its clear: So JDK does not provide "precision settings" for trig functions. This is the reason for libs such as ApFloat or BigMath. – nimo23 Nov 23 '17 at 15:12

1 Answers1

1

JDK does not provide "precision settings" for trigonometric math methods (sin, cos, log, exp, etc.).

This is the reason for libs such as "ApFloat" or "BigMath".

nimo23
  • 5,170
  • 10
  • 46
  • 75