Rational numbers in D

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Jun 9 10:50:23 PDT 2012


On 09.06.2012 21:47, Joseph Rushton Wakeling wrote:
> On Saturday, 9 June 2012 at 01:11:49 UTC, Stewart Gordon wrote:
>> Including it in D seems very unlikely.
>>
>> Including it in Phobos, OTOH, is another matter.
>
> My bad phrasing. Of course in Phobos.
>
>
> On 09/06/12 08:25, SomeDude wrote:
>> I've never had any use for rational numbers as have 99% of developers
>> I believe.
>> Maybe I'm overlooking something, but I see only two possible usages of
>> them:
>> math packages and length quotations in the english system. Because of
>> this, I'd
>> rather see them in Deimos than in Phobos.
>
> In fact the application I had in mind was for music software, where you
> want to be able to store pitch changes in fractions of a whole tone. In
> that context it's important that adding up those changes be guaranteed
> to produce exact results.
>
> e.g. if you take middle C and raise it by 1 whole tone _exactly_ you
> arrive at the note D. If you raise it by 0.99999999999999999999999999995
> instead, how does your software reliably handle this?

Prescaling works just fine. That is use plain integer and assume it 
holds value in the smallest fraction possible. Then define functions 
that do scale and round it to tone, note etc.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list