Path: csiph.com!aioe.org!hzzNxxMX5IPvnEV4b74Cww.user.46.165.242.91.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.programming Subject: Re: A little puzzle. Date: Sat, 26 Nov 2022 19:36:30 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <875yf8nijb.fsf@bsb.me.uk> <865yf79l66.fsf@linuxsc.com> <87wn7nj9mb.fsf@bsb.me.uk> <86sfi98xnx.fsf@linuxsc.com> <87leo1i5bo.fsf@bsb.me.uk> <86k03k7yqe.fsf@linuxsc.com> <87o7stisxy.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: gioia.aioe.org; logging-data="29635"; posting-host="hzzNxxMX5IPvnEV4b74Cww.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.9.2 Xref: csiph.com comp.programming:15971 On 2022-11-26 17:25, Ben Bacarisse wrote: > Tim Rentsch writes: > >> Consider a plane flying easterly (and for simplicity, directly >> above the equator). We measure the plane's progress by its >> longitude, measured to the nearest second of arc (1/3600 of a >> degree), so from -180 degrees to 179 degrees 59 minutes 59 >> seconds (or equivalently from -648000 seconds to 647999 seconds). >> We would like to know if a given longitude is between the plane's >> starting longitude and ending longitude. (Note that the >> longitude measurements below are given in degrees, minutes, and >> seconds, but we may assume that internally they are represented >> as an integer number of seconds.) >> >> For example, if the plane flies >> >> from Los Angeles, US (longitude -118 14' 37") >> to New York, US (longitude -73 56' 7") >> >> does it cross over the longitude lines of >> >> Denver, US (longitude -104 59' 30"), or >> Moscow, Russia (longitude 37 37' 6")? >> >> Answer: the plane does cross the longitude line of Denver but not >> that of Moscow. >> >> Similarly, if the plane flies >> >> from Beijing, China (longitude 116 23' 0") >> to Los Angeles, US (longitude -118 14' 37") >> >> does it cross over >> >> Tokyo, Japan (longitude 139 50' 32"), >> Waikiki, Hawaii, US (longitude -157 50' 4"), or >> Moscow, Russia (longitude 37 37' 6")? >> >> Answer: the plane does cross the longitude lines of Tokyo and >> Waikiki but not that of Moscow. >> > So I don't think it would matter to help DAK by giving > > with Ada.Text_IO; > use Ada.Text_IO; > > procedure Longitude is > > function Flight_East_Crosses_Longitude > (Start_Seconds, End_Seconds, Longitude : Integer) return Boolean is > begin > -- Your code here > end Flight_East_Crosses_Longitude; > > Beijing : constant Integer := ((+116 * 60) + 23) * 60 + 0; > Los_Angeles : constant Integer := ((-118 * 60) + 14) * 60 + 37; > Tokyo : constant Integer := ((+139 * 60) + 50) * 60 + 32; > Waikiki : constant Integer := ((-157 * 60) + 50) * 60 + 4; > Moscow : constant Integer := ((+037 * 60) + 37) * 60 + 6; > > begin > Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Tokyo)'img); > Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Waikiki)'img); > Put_Line(Flight_East_Crosses_Longitude(Beijing, Los_Angeles, Moscow)'img); > end Longitude; > > Of course, Ada is one of the few languages where one can actually > implement largely type-generic functions, so it's a shame to be this > specific, but as I say, that could come later. Here is an implementation based on an integer type. Usually one would deploy a fixed-point type instead, but I don't want to mud the details. Also a ring buffer would use a modular type, but then it must start at 0, and here we start at -180 * 3600 seconds. -------------------------------------------------- with Ada.Text_IO; use Ada.Text_IO; procedure Test_Longtitude is -- -- Longtitude in seconds -- type Longtitude is range -180 * 3600 .. 180 * 3600 - 1; type Degree is range -180..180; type Minute is range 0..59; type Second is range 0..59; function Compose (D : Degree; M : Minute; S : Second) return Longtitude is Result : constant Integer := (Integer (D) * 60 + Integer (M)) * 60 + Integer (S); begin if Result <= Integer (Longtitude'Last) then return Longtitude (Result); else return Longtitude (Result - 360 * 3600); end if; end Compose; Beijing : constant Longtitude := Compose (+116, 23, 0); Los_Angeles : constant Longtitude := Compose (-118, 14, 37); Denver : constant Longtitude := Compose (-104, 59, 30); New_York : constant Longtitude := Compose ( -73, 56, 7); Tokyo : constant Longtitude := Compose (+139, 50, 32); Waikiki : constant Longtitude := Compose (-157, 50, 4); Moscow : constant Longtitude := Compose (+037, 37, 6); function Flight_East_Crosses_Longitude ( Start, Stop, X : Longtitude ) return Boolean is begin if Start <= Stop then return X in Start..Stop; else return X <= Stop or else X >= Start; end if; end Flight_East_Crosses_Longitude; begin Put_Line ( "Los_Angeles to New_York over Denver " & Flight_East_Crosses_Longitude (Los_Angeles, New_York, Denver)'Image ); Put_Line ( "Los_Angeles to New_York over Moscow " & Flight_East_Crosses_Longitude (Los_Angeles, New_York, Moscow)'Image ); Put_Line ( "Beijing to Los_Angeles over Tokyo " & Flight_East_Crosses_Longitude (Beijing, Los_Angeles, Tokyo)'Image ); Put_Line ( "Beijing to Los_Angeles over Waikiki " & Flight_East_Crosses_Longitude (Beijing, Los_Angeles, Waikiki)'Image ); Put_Line ( "Beijing to Los_Angeles over Moscow " & Flight_East_Crosses_Longitude (Beijing, Los_Angeles, Moscow)'Image ); end Test_Longtitude; ------------------------------ It should print: Los_Angeles to New_York over Denver TRUE Los_Angeles to New_York over Moscow FALSE Beijing to Los_Angeles over Tokyo TRUE Beijing to Los_Angeles over Waikiki TRUE Beijing to Los_Angeles over Moscow FALSE -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de