[phobos] Proposed changes to std.math + introduce std.mathspecial

Robert Jacques sandford at jhu.edu
Wed Nov 17 15:33:06 PST 2010


On Wed, 17 Nov 2010 17:46:57 -0500, Don Clugston  
<dclugston at googlemail.com> wrote:
>>> (1) Add two necessary functions to std.math:
>>>
>>> // Rounds x to the nearest int or long using the currently selected
>>> rounding mode
>>> // (MUCH faster than cast(int)).
>>
>> Why isn't this the default, i.e. why doesn't cast(int) do this?
>
> Because C stuffed up in a major way, and we have to live with it.
> Intel has tried THREE TIMES to fix C's mistake, by making hardware
> changes and adding new instructions to their CPUs.
>
>> Please use full words when abbr is not absolutely obvious and/or the  
>> gain in typing is small (esp for rarely used functions).
>>  (also sgngamma --> signgamma)
>
> I didn't invent the name. sgn is the signum function. See, for example,
> http://en.wikipedia.org/wiki/Sign_function
>
>> To an uninitiated reader this may look like "random int" and "random  
>> long".
>
> Ugh, you're right.
>
>> Could we define this instead?
>> N roundTo(N)(real x) if (isIntegral!N);
>
> That sounds a bit too formal. I would expect that to perform some
> exact form of rounding (certainly not round-to-nearest, which seems
> irrational to our uninitiated reader). I would also expect it to do
> something sensible when the value is > int.max.
> This function should really be an intrinsic, and it only makes sense
> for int and long.
>
> fastRoundInt(real x)
> fastRoundLong(real x)
> ?
> A bit clumsy, but should be unambiguous.
>

Personally, I've already switched to using roundTo!T in my code. The  
function is over in std.conv, behaves as expected and supports a wide  
range of types. In contrast std.math has a multitude of functions (lround,  
lrint, rndtol) some of which have _never_ been implemented (lround) and  
others appear to be aliases of each other (lrint, rndtol). (This may just  
be incorrect documentation as rndtonl and rndtol are described  
differently). This is to say nothing of the floating point rounding  
functions (nearbyint, rint, round, rndtonl). Given that we have control  
over the floating point rounding mode, is there a reason we need anything  
more than round and roundTo!T?


More information about the phobos mailing list