Hi all,
while working with some code dealing with angles (latitudes and longitudes) I had to fit my data to pre-defined 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 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
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 -- Enrico Spinielli "Do Androids dream of electric sheep?"— Philip K. Dick "Hear and forget; see and remember;do and understand."—Mitchel Resnick Number-coerceToRangeFromto.st (472 bytes) Download Attachment |
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 <[hidden email]> wrote: > Hi all, > while working with some code dealing with angles (latitudes and longitudes) > I had to fit > my data to pre-defined 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/calendar-book/third-edition/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. |
> 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) 359 moduloInterval: (-90 to: 90) ? Stef |
2010/4/24 Stéphane Rollandin <[hidden email]>:
>> 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) > > 359 moduloInterval: (-90 to: 90) ? > +1 > Stef > > > > |
Yes,
naming has been my major problem with this...in fact to be complete the name should take into account that with the above definition the interval is right open verbose, uh? But if we want to implement the complementary definition there is the need for such distinction. Given the following property the implementation is trivial:
coerceRangeToRightIntervalFrom:to: could be a very nice addition for Point (and classes implementing \\?)
to make things like the following possible:
i.e. like having an infinite space and recenter position within the view bounds see
Now the name is hard to select!
I personally do not dislike nRightOpenInterval: Bye Enrico On Sat, Apr 24, 2010 at 21:44, Nicolas Cellier <[hidden email]> wrote: 2010/4/24 Stéphane Rollandin <[hidden email]>: -- Enrico Spinielli "Do Androids dream of electric sheep?"— Philip K. Dick "Hear and forget; see and remember;do and understand."—Mitchel Resnick |
Free forum by Nabble | Edit this page |