Open pwl opened 8 years ago
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.
There is also an alternative here (ArbFloats.jl
); cc @Jeffrey-Sarnoff.
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.
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.
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:
sin
modulo 2pi, pull our exponent and significand of argument of exp
).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.
The exponent operator ^ also doesn't work, any plan to support that?
yes -- DoubleDouble is being reconceived as DoubleFloats .. when that is released, ^ will be supported.
Awesome looking forward to it ^_^
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.