Code speed

Don nospam at nospam.com
Fri Apr 16 00:57:52 PDT 2010


Ary Borenszweig wrote:
> Don wrote:
>> Ary Borenszweig wrote:
>>> Don wrote:
>>>> Lars T. Kyllingstad wrote:
>>>>> Don wrote:
>>>>>> bearophile wrote:
>>>>>>> So far I've just given a light reading of the code. Notes:
>>>>>>
>>>>>>> - pow(x, 2) and sqrt(y) can be written as x ^^ 2 and y ^^ 0.5 
>>>>>>> (but you have to import std.math anyway, because of a bug).
>>>>>>
>>>>>> That's not a bug. It's intentional. x ^^ y will probably always 
>>>>>> require import std.math, if y is a floating point number.
>>>>>
>>>>> Really?  Why is that?  I find that kind of disappointing, I always 
>>>>> believed it to be a temporary solution.
>>>>>
>>>>> I think the inconsistency with the other operators will make this a 
>>>>> major WTF for people new to the language.  Why should a^^b require 
>>>>> an explicit import while a*b doesn't?
>>>>
>>>> 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. Feel free to try to change my mind, of 
>>>> course.
>>>
>>> Is there a better way to do pow() for floating point numbers without 
>>> importing std.math?
>>>
>>> I see this:
>>>
>>> 1. You want to do x ^^ fp.
>>> 2. The compiler complains saying "if you want to do that, import 
>>> std.math. I'm telling you this just to let you know you'll be 
>>> importing the whole module".
>>>
>>> Alternative 1 for user:
>>> User says "Ok, I import std.math"
>>>
>>> Alternative 2 for user:
>>> User says "No way I'm importing std.math just to make a pow. But... I 
>>> still *need* to make that pow... what is your advice, mr. compiler?"
>>
>> That's a good point, it should be possible to use a static import as 
>> well. I do think it's pretty odd to be doing floating point without 
>> importing std.math, though. I mean, abs() is absolutely fundamental.
> 
> But if you do a static import the whole module gets linked in, right?
> 
> My point is, if you are going to pow, you will need std.math, so it'll 
> always be a burden to import it by hand when using it. ^^

But you're assuming that you're using ^^ without using anything else 
from std.math. I think that's a very obscure case.
For example, any code which is ported from C or C++, or D1, that uses 
pow, will already be importing std.math. Cases where you see that you 
could use ^^ that isn't already using pow() (eg, where you see z = x*x + 
y*y), you will need to add an import.



More information about the Digitalmars-d-learn mailing list