^^ implementation [Was: Re: Code speed]

bearophile bearophileHUGS at lycos.com
Wed Apr 14 02:58:55 PDT 2010


Don:

Thank you for your answer and comments.

> Because pow() for floating point, when implemented properly, is a HUGE
> function, that ends up dragging almost all of std.math into the
> executable. And I think it's deceptive to do that silently.
> To make it completely built-in, basically all of std.math would need to
> be moved into druntime.

I have just read the source of pow() and indeed it's a complex function that needs several other functions of math.


>Raising to a float power is really a niche feature.<

Used it as x^^0.5 to perform the square root is not a niche feature, square roots are common enough.
And sqrt is an intrinsic, it doesn't need all std.math.

Error messages are not good yet, this program:


import std.c.stdio: printf;
void main() {
    printf("%f\n", 2.5 ^^ 0.5);
}


Generates:

test.d(3): Error: must import std.math to use ^^ operator
test.d(3): Error: undefined identifier module test5.std
test.d(3): Error: no property 'math' for type 'void'
Error: no property 'sqrt' for type 'int'
test.d(3): Error: function expected before (), not __error of type int


Alternative single error:

test.d(4): Error: must import std.math to use "x ^^ floating point".


If I change the code a little the situation doesn't improve a lot:

import std.c.stdio: printf;
import std.math: sqrt;
void main() {
    printf("%f\n", 2.5 ^^ 0.5);
}


test5.d(4): Error: undefined identifier module test5.std
test5.d(4): Error: no property 'math' for type 'void'
Error: no property 'sqrt' for type 'int'
test5.d(4): Error: function expected before (), not __error of type int


So I can see various solutions:

1) Keep the situation as now, but improve the error messages (essentially printing only one error message that's more focused).

2) As the point (1), but in the case of x^^0.5 doesn't require the import math; x^^FP requires the import still.

3) Automatically import the pow() in all modules, even if this causes the import of lot of std.math. So no errors are generated.

4) Automatically import the pow() in a module only if it contains a x^^FP where FP is a floating point != 0.5  So the code in most modules is not affected by std.math.

Regardless what the final decision will be, this topic must be discussed in the D newsgroup, it's not something you have to decide for yourself (despite you are a very programmer, Don). This subthread can even be moved to the main D newsgroup.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list