[squeakdev] [ENH] coercing a number to an interval
Igor Stasenko
siguctua at gmail.com
Sat Apr 24 18:21:40 UTC 2010
I agree, its useful sometimes. Not sure about naming.
We having an Intervals, so i think we could use 'interval' instead of 'range':
x coerceToInterval: (90 to: 90)
then given method will be just for convenience:
Number>>coerceToIntervalFrom: a to: b
^ self coerceToInterval: (a to: b)
Hmm... #coerceToInterval: sounds like converting something into an
instance of Interval..
so, maybe there could be some other names, like
#putOnInterval:
#withinInterval:
359 withinInterval: (90 to: 90)
On 24 April 2010 21:04, Enrico Spinielli
<enrico.spinielli at googlemail.com> wrote:
> Hi all,
> while working with some code dealing with angles (latitudes and longitudes)
> I had to fit
> my data to predefined intervals, i.e. longitudes expressed degrees are
> usually dealt with in the range [180..180).
> It happens that for example calculating the position of a planet using some
> astronomical
> algorithms that the result is an angle like 359 and you want to represent it
> in the
> [90..90) range:
> 359 coerceToRangeFrom: 90 to: 90 > 1
> of course it works with fractions as well
> (12/7) coerceToRangeFrom: (1/3) to: (4/5) > (82/105)
>
> To do so I added a new method to Number
>
> coerceToRangeFrom:to:
>
> under the category 'truncation and rounding'
> and can be found in fileout format in attachment.
> The definition I applied was inspired by the errata [1] of
> Dershowiz N., Reingold E.M. "Calendrical Calculations," 3rd ed, 2008
> where the authors extend the definition of 'modulus' (\\ in Squeak) to an
> interval
>
> x mod [a .. b) = a + [(x − a) mod (b − a)] (a)
> x mod (a .. b] = b − [(b − x) mod (b − a)] (b)
> note that
> x mod [a .. b) = x mod (b .. a]
> so only one of (a) or (b) is necessary
> and that
> x mod y
> can be viewed as a shorthand for
> x mod [0 .. y)
>
> Note: the notation [a..b) was suggested by Knuth.
>
> There are many places in the image where this coercion/fitting to an
> interval is
> performed but without revealing the purpose, here just few easy examples:
> DateAndTime>>hour12
> Player > angles coercion
> Kedama > angles coercion
> SequenceableCollection>>atWrap:
> ...
> Core developers, should you find it useful please integrate in Trunk,
> maybe find a better name as well!
> Thanks
> Bye
> Enrico
> [1] http://emr.cs.iit.edu/home/reingold/calendarbook/thirdedition/errata.shtml
> 
> Enrico Spinielli
> "Do Androids dream of electric sheep?"— Philip K. Dick
> "Hear and forget; see and remember;do and understand."—Mitchel Resnick
>
>
>
>

Best regards,
Igor Stasenko AKA sig.
More information about the Squeakdev
mailing list
