• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing complex Functions for BigDecimals  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The {Math} class has methods {cos()}, {sin()}, {tan()}, {acos()}, {asin()}, {atan()}, {log()}, and {pow()}. I'm trying to write a Java class that implements these functions for {BigDecimal}s. There's a Taylor series that converges pretty quickly for the cosine and the sine, but I don't know of any as quick way to calculate any of the other six functions. To calculate the tangent does {Math} just calculate the sine and cosine and divide the former by the latter? And does anyone know how {Math} calculates the latter six functions so that I can implement those calculations for {BigDecimal}s?

Kevin S
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the JDK install gives you the source code for pretty much everything. I think it comes as src.zip, which you can open and look at to your heart's content.

-edit- I think...

-edit 2 - well...i looked...at least at the sin function...it doesn't actually help:

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which I did... the code for Math.tan just calls StrictMath.tan; and the code for StrictMath.tan (and all the other methods too) is declared "native". And that's it for the Java code.

I've heard that the code base for the JVM is open-source now, but whether that applies to the various native code bases I don't know.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Which I did...
yeah..we were apparently both doing it at the same time...
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether or not is still is but for a long time when it comes to approximations for just about any functions you can think of the Bible used to be "Handbook of Mathematical Functions" by Abramowitz and Stegun . It contains a whole section on the elementary transcendental functions giving numerical approximations, series and continued fractions. I know that there is now a PDF version of Abramowitz and Stegun floating around so you probably don't have to buy it but I purchased mine in 1971 and it was £5 very very well spent.



 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember
tanθ = sinθ ÷ cosθ
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Remember
tanθ = sinθ ÷ cosθ

the OP asked if that was the best solution...
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Apfloat
Then, if you really need a BigDecimal you can convert the result from org.apfloat.Apfloat to java.math.BigDecimal.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!