BigDecimal and trigonometrics

J

javerberg

Does anyone know an easy way, or know of freely any available code /
packages, to use sine and cosine on BigDecimal with full precision?

/Magnus
 
O

Oliver Wong

Does anyone know an easy way, or know of freely any available code /
packages, to use sine and cosine on BigDecimal with full precision?

AFAIK, the sine of a value may be an irrational number, which means it
may take an infinite number of digits to represent. Therefore, what you are
asking for is impossible on a computer with finite memory, nevermind whether
it's running Java or not.

- Oliver
 
C

charles_n_may

You need a MacLaurin series expansion (from calculus) around a known
reference point; then you can get arbitrary precision. I have a hard
time imagining any practical application that would need more than the
precision allowed by a double, though. I've mainly found BigDecimals
valuable in currency calculations.

Try this reference:
http://www.tc.umn.edu/~ringx004/sidebar.html
 
J

Jeffrey Schwab

Oliver said:
AFAIK, the sine of a value may be an irrational number, which means it
may take an infinite number of digits to represent. Therefore, what you are
asking for is impossible on a computer with finite memory, nevermind whether
it's running Java or not.

That's only true if one assumes a traditional floating-point
representation. There are plenty of techniques (continued fractions,
symbolic algebra, etc.) for representing irrational numbers with
infinite precision in finite memory.
 
O

Oliver Wong

Jeffrey Schwab said:
That's only true if one assumes a traditional floating-point
representation. There are plenty of techniques (continued fractions,
symbolic algebra, etc.) for representing irrational numbers with infinite
precision in finite memory.

You are, of course, correct. My bad.

That being said, if the OP wishes to use BigDecimal, I think we can
safely assume (s)he wants traditional floating-point representation.

- Oliver
 
R

Roedy Green

That's only true if one assumes a traditional floating-point
representation. There are plenty of techniques (continued fractions,
symbolic algebra, etc.) for representing irrational numbers with
infinite precision in finite memory.

I think you are confusing rational repeaters (which can be accurately
represented by a rational fraction) with irrationals that at best
could be described as a limit of a series, e.g. pi, e.

Remember the proof that there are non-rational numbers, discovered,
IIRC by the ancient Greeks. There is no way you can map all
rationals, much less all irrationals with perfect accuracy into a
finite address space. There an infinite but countably many rationals
and uncountably many irrationals. (One of my pet peeves is
newscasters who use the term "uncountable" to mean "a large number" or
"more than I can count on my fingers".)

"Countable" to mathematicians means there exists a 1-1 mapping between
the set and the set of integers.
 
J

javerberg

Thank you for all replies. Unfortunatelly I expressed my self badly.
What I meant was "arbitary precision". I would for example like to be
able to calculate sin(0.2 and cosin(0.2) with 30 decimals. It's
possible to do with Taylor expansion, but I hoped there would be some
ready package avaible. Sorry for my bad English.
/Magnus
 
C

Chris Uppal

Thank you for all replies. Unfortunatelly I expressed my self badly.
What I meant was "arbitary precision". I would for example like to be
able to calculate sin(0.2 and cosin(0.2) with 30 decimals.

Are you forced to use the BigDecimal class ? If not then there are other
arbitrary precision packages around which might do what you want. One example
(which I have never used so I cannot speak for its quality or appropriateness):

http://www.apfloat.org/apfloat_java/

-- chris
 
O

Oliver Wong

Roedy Green said:
I think you are confusing rational repeaters (which can be accurately
represented by a rational fraction) with irrationals that at best
could be described as a limit of a series, e.g. pi, e.

Remember the proof that there are non-rational numbers, discovered,
IIRC by the ancient Greeks. There is no way you can map all
rationals, much less all irrationals with perfect accuracy into a
finite address space. There an infinite but countably many rationals
and uncountably many irrationals. (One of my pet peeves is
newscasters who use the term "uncountable" to mean "a large number" or
"more than I can count on my fingers".)

"Countable" to mathematicians means there exists a 1-1 mapping between
the set and the set of integers.

You are, of course, also correct. But in the specific case of the OP's
problem, assuming (s)he wants the sine (or cosine) of a rational number,
this can always be expressed in finite memory as the string "SIN(x)" where x
is the rational number represented in some finite form (e.g. a finite
decimal expansion, which is sometimes possible, or a fraction of two finite
whole numbers, which is always possible).

- Oliver
 
R

Roedy Green

But in the specific case of the OP's
problem, assuming (s)he wants the sine (or cosine) of a rational number,
this can always be expressed in finite memory as the string "SIN(x)" where x
is the rational number represented in some finite form (e.g. a finite
decimal expansion, which is sometimes possible, or a fraction of two finite
whole numbers, which is always possible).

There are an infinite number of rational numbers. Therefore you cannot
represent them with a finite string much less all possible
transcendental functions of them.

If you limit yourself to all rational numbers expressible as the
division of two 1,000,000-bit integers then the "rationals" can be
represented by finite strings, but that is just the merest wisp of the
total number of rationals.

The problem here is we are wearing two hats -- mathematician and
computer programmer. Computer programmers are always talking about
finite subset approximations of the numbers that mathematicians think
about. Computer programmers sometimes forget that, and that the
mathematical rules don't perfectly apply on that limited set.
 
O

Oliver Wong

Roedy Green said:
There are an infinite number of rational numbers. Therefore you cannot
represent them with a finite string much less all possible
transcendental functions of them.

Yes, there are an infinite number of rational numbers, but every one of
them can be represented with finitely many ASCII characters. And you only
need a finite amount of ASCII characters to represent "SIN(" and ")". That
is why you only need finite memory to represent the sine (or cosine) of any
particular rational number.

And if the OP wants to represent the sine (or cosine) or 10 rational
numbers, this will also require "merely" finite memory. Same with a billion.
In fact, as long as the OP wishes to represent the sine (or cosine) of a
finite number of rational numbers, (s)he will only need finite memory.

If (s)he wants an infinite number of rational numbers, that's a
different story, with its own problems (where would these infinite number of
rational numbers come from? user input? some sort of iterator?)

- Oliver
 
J

Jeffrey Schwab

Roedy said:
I think you are confusing rational repeaters (which can be accurately
represented by a rational fraction) with irrationals that at best
could be described as a limit of a series, e.g. pi, e.

Remember the proof that there are non-rational numbers, discovered,
IIRC by the ancient Greeks. There is no way you can map all
rationals, much less all irrationals with perfect accuracy into a
finite address space. There an infinite but countably many rationals
and uncountably many irrationals. (One of my pet peeves is
newscasters who use the term "uncountable" to mean "a large number" or
"more than I can count on my fingers".)

"Countable" to mathematicians means there exists a 1-1 mapping between
the set and the set of integers.

Can we assume that each rational number we need to represent must have
be arrived at by some sequence of mathematical operations? If so, each
such number can be represented by the sequence of operations that
invokes it.
 
J

Jeffrey Schwab

Roedy said:
I think you are confusing rational repeaters (which can be accurately
represented by a rational fraction) with irrationals

I think I am not.
that at best
could be described as a limit of a series, e.g. pi, e.

Remember the proof that there are non-rational numbers, discovered,
IIRC by the ancient Greeks.

Proven geometrically, in fact, by trying to determine the length of the
hypoteneuse of a right triangle. This sort of irrational number can be
represented in finite space by a continued fraction.

http://www.reference.com/browse/wiki/Continued_fraction
There is no way you can map all
rationals, much less all irrationals with perfect accuracy into a
finite address space. There an infinite but countably many rationals
and uncountably many irrationals.

As proven by Georg Cantor, by a method I still find mind-blowingly
simple and graceful.
(One of my pet peeves is
newscasters who use the term "uncountable" to mean "a large number" or
"more than I can count on my fingers".)

I hear ya. :) It may be of comfort to know that "uncountable" has a
non-mathematical meaning in the English language:

http://dictionary.reference.com/search?q=uncountable
"Countable" to mathematicians means there exists a 1-1 mapping between
the set and the set of integers.

You have a deep understanding! However, you don't need to instantiate
all possible instances of a class in order to represent a subset of
those instances, and similarly, you don't need to represent all
irrational numbers in order to represent a (finite) subset of interest,
as the OP requested.
 
R

Roedy Green

If (s)he wants an infinite number of rational numbers, that's a
different story, with its own problems (where would these infinite number of
rational numbers come from? user input? some sort of iterator?)

There is no upper bound on the RAM needed to represent a single
rational as a string.

There is also the practical manner of the size of the address space
and the limit bigDecimal or bigInteger imposes on the length.

You can only deal with a very small subset of the rationals in Java.

One place you run into these super large numbers is in calculating
combinatorics.
 
R

Roedy Green

You have a deep understanding! However, you don't need to instantiate
all possible instances of a class in order to represent a subset of
those instances, and similarly, you don't need to represent all
irrational numbers in order to represent a (finite) subset of interest,
as the OP requested.

Your orgininal claim was much more extravagant:
That's only true if one assumes a traditional floating-point
representation. There are plenty of techniques (continued fractions,
symbolic algebra, etc.) for representing irrational numbers with
infinite precision in finite memory.

Perhaps the problem is I interpreted your statement to mean
"representing all irrational numbers with infinite precision in finite
memory" where you meant "a few irrational numbers such as e, pi, pi,
sin(a/b), sqrt(2) that commonly occur in calculus"
 
J

Jeffrey Schwab

Roedy said:
Your orgininal claim was much more extravagant:




Perhaps the problem is I interpreted your statement to mean
"representing all irrational numbers with infinite precision in finite
memory" where you meant "a few irrational numbers such as e, pi, pi,
sin(a/b), sqrt(2) that commonly occur in calculus"

Yes, by "irrational numbers," I meant "those irrational numbers you
derive." I did not mean that a computer program could enumerate all
real numbers. Sorry for being unclear!
 
O

Oliver Wong

Jeffrey Schwab said:
Can we assume that each rational number we need to represent must have be
arrived at by some sequence of mathematical operations? If so, each such
number can be represented by the sequence of operations that invokes it.

For any number X (rational or otherwise), we can arrive at it by
starting with X and doing zero operations on it. This doesn't nescessarily
prove that X can be represented with finite memory though.

If X is rational, then we sort of apriori have to assume that the step
of "start with X" requires finite memory, to which we perform 0 operations
(thus adding a overhead of 0 memory) and end up with finite memory
requirements again.

- Oliver
 
J

Jeffrey Schwab

Oliver said:
For any number X (rational or otherwise), we can arrive at it by
starting with X and doing zero operations on it. This doesn't nescessarily
prove that X can be represented with finite memory though.

Of course not. That would be cheating.
If X is rational, then we sort of apriori have to assume that the step
of "start with X" requires finite memory, to which we perform 0 operations
(thus adding a overhead of 0 memory) and end up with finite memory
requirements again.

Yes, that's true. But can you please point me to an irrational number
that cannot be derived by a sequence of mathematical operations on
rational numbers?
 
O

Oliver Wong

Jeffrey Schwab said:
Yes, that's true. But can you please point me to an irrational number
that cannot be derived by a sequence of mathematical operations on
rational numbers?

No, but I think I can prove their existence if I can prove that all
sequences of mathematical operations on rational numbers is countable (since
the there are uncountably many irrational numbers).

Every sequence of mathematical operations on rational numbers can be
represented by some ASCII string (e.g. "1+1")

You can order them by using Java's standard string sorting algorithm.
Associate the first such legal string with the integer 1.
Associate the second such legal string with the integer 2.
And so on.

You now have a 1 to 1 mapping between sequences of mathematical
operations on rational numbers and the set of natural numbers, thus showing
that there are only countably many sequences of mathematical operations on
rational numbers.

Note that I'm assuming the ASCII representation is finite, which I think
is true as long as the number of operators and the number of arguments to
each operator is finite in the sequence (and as long as each operator and
each term can be represented by a finite number of characters, which is true
for rational numbers).

- Oliver
 
R

Roedy Green

But can you please point me to an irrational number
that cannot be derived by a sequence of mathematical operations on
rational numbers?

The time until George Bush's death in microseconds from noon UTC
today.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top