Skip to content

gamma function: add constants for 128-bit reals.#4047

Closed
redstar wants to merge 1 commit intodlang:masterfrom
redstar:gamma
Closed

gamma function: add constants for 128-bit reals.#4047
redstar wants to merge 1 commit intodlang:masterfrom
redstar:gamma

Conversation

@redstar
Copy link
Contributor

@redstar redstar commented Mar 3, 2016

No description provided.

@tsbockman
Copy link
Contributor

Is there a platform now where we can actually test D code with 128-bit reals?

@redstar
Copy link
Contributor Author

redstar commented Mar 7, 2016

I am working on an AArch64 port of LDC. This platform uses 128-bit reals.

@tsbockman
Copy link
Contributor

Cool.

There has been some discussion by maintainers of std.math about doing, essentially, a re-write to cleanse it of unnecessary C-isms, over-use of inline assembler, etc. I had hoped this could be done before adding 128-bit real and cent support, to avoid duplication of effort.

But, on the other hand, already having a working 128-bit platform to test against would be really helpful...

@redstar
Copy link
Contributor Author

redstar commented Mar 9, 2016

It seems easy to add support for 128-bit reals. Only this PR, #4036 and an implementation of exp() are required.

@tsbockman
Copy link
Contributor

At a minimum, there are many other places in std.math where 80-bit constants need to be upgraded to 128-bits (assuming you want the extra precision to actually be useful, anyway).

Also, a number of functions in std.math only really support real: using 128-bit floating-point operations for 32- and 64-bit values is likely to be unacceptably slow. Overloads will need to be created.

I also expect that once we're able to actually test the 128-bit code paths in std.math, problems will be revealed; I have personally fixed at least one such issue (which I only noticed because the code didn't quite work right for 80-bit, either).

Having said all that, a lot of my concern over 128-bit real actually has do with my previous investigation of what it would take to get imbExtended working. That's a far less sane format than the ieeeQuadruple one that you're working on, though, so I don't think you should be too worried.

@redstar
Copy link
Contributor Author

redstar commented Mar 12, 2016

My first goal is to compile all modules on AArch64. Without it I cannot run the test suite automatically (which will certainly reveal tons of other errors).

@tsbockman
Copy link
Contributor

My first goal is to compile all modules on AArch64. Without it I cannot run the test suite automatically (which will certainly reveal tons of other errors).

Makes sense. Just remember that the std.math test suite, itself, will likely be somewhat broken until the constants have been expanded for 128-bit some places.

@DmitryOlshansky
Copy link
Member

Can't really check the constants.. Anyhow LGTM
As this is related to math - @9il ?

@9il 9il added the math label Mar 23, 2016
@9il
Copy link
Member

9il commented Mar 23, 2016

Can't really check the constants..

We should add sqrt2p constant to std.math, thought (hex representation from wolfram alpha is required).

static if (floatTraits!(real).realFormat == RealFormat.ieeeExtended) {
static if (floatTraits!(real).realFormat == RealFormat.ieeeQuadruple) {
enum real MAXLOG = 0x1.62e42fefa39ef35793c7673007e6p+13; // log(real.max)
enum real MINLOG = -0x1.6546282207802c89d24d65e96274p+13; // log(real.min_normal*real.epsilon) = log(smallest denormal)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@redstar Could you please explain what is this constants and how they was computed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to wikipedia and the gnu's libquadmath the max value with quadruple precision is
1.18973149535723176508575932662800702e4932, thus:

11356.52340629414394949193107797076489126

afaict the current representation has less digits?

real.min is 3.36210314311209350626267781732175260e-4932 and thus

-11355.13711193302405887309661372784853821

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wilzbach

afaict the current representation has less digits?

That's because those are in hexadecimal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsbockman yep I know, I tried to convert the real string, but obviously failed with low precision on my 64bit system.
Hence I wanted to point out that results with wolfram alpha differ, btw for max it gives me:

0x2c5c.85fdf473de6af278ece600fcbdab54a087542d48a06307294043f0285fee8215026a32a8c36b4f6906fde3d9c2c481034dfb0912176b3604e2b217c387b932e90332ca820b965b56bf4b36fd61860709a2b353606c41270a00931efe794d0e24013d67dd3fdb5125d25a5d48a125dcef5f0f23479ae4fdb6bb8893c201

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just reverse engineered this, there are obviously too many things you can do wrong when calculating this value...

void main()
{
    import std.algorithm, std.stdio, std.conv;
    string mantissa = "1.01100010111001000010111111101111101000111001111011110011010100111001001111000111011001110011000000000111111001011101110101011";
    write(mantissa[0 .. 2]); // always 1.
    mantissa = mantissa[2 .. $];
    for (size_t i = 0; i < mantissa.length/4; i++)
    {
        writef!"%x"(to!ubyte(mantissa[0..4], 2));
        mantissa = mantissa[4 .. $];
    }
    writeln();
}

To summarize: I hate the internal/math modules. It delayed the ARM port years ago, now it delayed the AArch64 port for 2 years. Nobody maintains this code, nobody knows what it's supposed to do, the referenced original C code doesn't match this code, so it isn't useful either.... I doubt this code is even correct. For example, in gammafunction.d we have MAXLOG for extended/double format. But in errorfunction.d we use only the extended MAXLOG for all float types. Is this correct? I guess not, I don't know and I don't know who could know. The whole math module situation is a mess. ping @andralex I think we need some long term solution for these modules....

For now, as this is the last thing stopping GDC (and probably LDC) from compiling phobos on AArch64 I'll post a revived version of this pull request tomorrow.

@wilzbach
Copy link
Contributor

Ping @redstar

private {

enum real SQRT2PI = 2.50662827463100050242E0L; // sqrt(2pi)
enum real SQRT2PI = 2.5066282746310005024157652848110452798109E0L; // sqrt(2pi)
Copy link
Contributor

@wilzbach wilzbach Apr 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://www.wolframalpha.com/input/?i=sqrt(2pi)

2.5066282746310005024157652848110452798109E0L
2.506628274631000502415765284811045253006986740609938316629

(latter is from Wolfram Alpha)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wolfram allows to get hex representation, see constants in std.math

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice :)
http://www.wolframalpha.com/input/?i=sqrt(2pi)+in+base+16
Cut wherever you want:

0x2.81b263fec4e0b2caf9483f5ce459dc5f19f3ea6416000b50dc2f412ddeeb948b5068337b65698726847d560b8818fb98307f0f37d42a17c1c2b86baec9ffa

@JackStouffer
Copy link
Contributor

Closing due to author inactivity. @redstar please ping me if you want to reopen.

@jpf91
Copy link
Contributor

jpf91 commented Dec 19, 2017

New pull request: #5947

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants