DoubleDouble.jl icon indicating copy to clipboard operation
DoubleDouble.jl copied to clipboard

Basic math functions

Open pwl opened this issue 8 years ago • 8 comments

Are there any plans to make this a drop-in replacement from Float64 or BigFloat? At this stage this is impossible because most of the missing basic math functions like sin, exp, etc.

pwl avatar May 06 '16 13:05 pwl

Unfortunately, writing so-called "elementary" functions like sin and exp is actually very hard. (I am by no means an expert, but if I understand correctly it basically consists of using Taylor series or Chebyshev expansions with enough terms to get to the desired precision, using different series expansions in overlapping parts of the complex plane.)

As a stop-gap measure, we could implement wrappers around the functionality provided by BigFloat (i.e. the MPFR package), as was done e.g. in https://github.com/dpsanders/CRlibm.jl.

It would be great if you could make a Pull Request for this. You just need to take the wrap_MPFR function from CRlibm.jl and make a version that uses DoubleDouble instead, and add it as a new file here. Let me know if you need any help.

dpsanders avatar May 06 '16 13:05 dpsanders

There is also an alternative here (ArbFloats.jl); cc @Jeffrey-Sarnoff.

dpsanders avatar May 06 '16 13:05 dpsanders

Wouldn't a wrapper to MPFR defeat the purpose of using DoubleDouble? I was looking at the original sources of MPFR to get an idea of how complicated writing a sine function from scratch would be but it looks incomprehensible. I guess that's the reason it's not implemented in DoubleDouble yet:-). I wish I had time to work that out but I have a lot on my plate right now.

I am taking a look at ArbFloats right now, they are supposedly slightly faster then BigFloat.

EDIT: sorry, I pressed the wrong key and submitted this early.

pwl avatar May 06 '16 14:05 pwl

On a second thought the Taylor expansions for most of the elementary functions are pretty simple by themselves. I wonder, where does the complicated nature of MPFR functions come from? I guess part of it is due to C not being a very expressive language and a part comes from some optimization tricks. So maybe it's not as difficult as it looks.

pwl avatar May 06 '16 14:05 pwl

Are there any plans to make this a drop-in replacement from Float64 or BigFloat?

Yes, that is the plan. But as you can probably tell by the commit log, this package is a long way down my priority list, so I wouldn't hold your breath. Of course, if you can offer funding or some other enticement, this might happen sooner...

If you look at the source code, implementations of common elementary functions such as sin and exp are fairly straightforward at the high level:

  1. A "range reduction" step to break the problem down to a small range (e.g. reduce argument of sin modulo 2pi, pull our exponent and significand of argument of exp).
  2. Use a polynomial approximation (typically some sort of minimax approximation, which are often close to Taylor's series)
  3. Combine the results of 1 and 2.

MPFR is complicated as it has to be able to do all that for all different precisions, whereas we have the luxury of knowing ahead of time how accurate these approximations need to be (we also have the luxury of being able to use MPFR as a reference answer, which is incredibly convenient).

A good reference for this stuff is the Handbook of Floating Point Arithmetic.

simonbyrne avatar May 06 '16 14:05 simonbyrne

The exponent operator ^ also doesn't work, any plan to support that?

mohamed82008 avatar Feb 07 '18 12:02 mohamed82008

yes -- DoubleDouble is being reconceived as DoubleFloats .. when that is released, ^ will be supported.

JeffreySarnoff avatar Feb 07 '18 13:02 JeffreySarnoff

Awesome looking forward to it ^_^

mohamed82008 avatar Feb 07 '18 20:02 mohamed82008