Loading ...
Sorry, an error occurred while loading the content.

[Clip] A DMS function - Converting Degrees to Decimals

Expand Messages
  • RS
    Hi Bong and All, ... I used to have a beautiful single-line formula I made many years ago to convert Degrees, Minutes and Seconds to decimals and back. It was
    Message 1 of 1 , Aug 26, 1999
    • 0 Attachment
      Hi Bong and All,

      At 18:03 1999-08-25 -0500, you wrote:

      >I need guidelines on a clip that would add minutes and seconds:
      >1:24 + 0:45 = 2:09


      I used to have a beautiful single-line formula I made many years ago to
      convert Degrees, Minutes and Seconds to decimals and back. It was a
      mathematical formula, just one line. I was looking for it recently, to use
      in a program and later for a Clip, but wasn't able to find it.

      I know how difficult (impossible on my experience at that time) it is to
      find an algorithm that does this much needed conversion and, above all, one
      that works. (I found a few in books; none of them worked.)

      Texas calculators used to have a DMS (Degrees-Minutes-Seconds) function
      that did the conversion; they probably still do. My Casio fx-4500P also
      does it, but with the annoyance that you can only access this function
      through a key - you cannot use it in a calculator program. So I was forced
      to design my own DMS function.

      I wanted to add the resulting elapsed time to a Clip I posted recently to
      this List which evaluated the time taken by an ^!IfTrue and an ^!If
      statement to determine which was faster. I knew there was somewhere in my
      files or my notes an adaptation of this formula which I made a couple years
      ago for Visual Basic. But since I couldn't find it easily, and, anyway, it
      was going to require some time to convert it to NoteTab ( a task that
      usually seems easy but always turns out to be otherwise) I just made the
      Clip to display the Starting and Ending time, leaving everybody to manually
      calculate the difference.

      The current discussion on the List on how to convert Hours,Minutes and
      Seconds gave me the impulse I was lacking before, to finally find and
      unearth my VB routine.

      The trickiest part is not only converting Degrees to Decimals, but doing
      the opposite, i.e. Decimals to Degrees. You might say that the final
      algorithm (the routine) looks pretty simple, and in fact it is. The
      difficult part was arriving to it!

      An extra problem was posed by the fact that the number to receive by the
      function was formatted as HH:MM:SS, instead of HH.MMSS as it was in my
      original function. And I had to take into account that the parameter passed
      might have only minutes (i.e. HH:MM) and no seconds; or even worse, might
      be only the Hours (HH), no minutes, no seconds.

      Also, the results when converting back to Degrees from Decimals, must take
      into consideration that the Minutes or Seconds value might be a single
      digit (i.e. less than 10) and it will look ugly or even create further
      complications if the result looked like "12:5:17", while the desired output
      should be "12:05:17".

      The functions also required testing with different input and output values,
      a few of which I list next, to make sure they worked under different
      conditions.

      Degrees Decimals
      ------- --------
      1:05:35 = 1.093056
      1:37:00 = 1.616667
      1:37 = 1.616667
      1:00:32 = 1.008889

      You'll notice that the DMS function retrieves the first number as 1.0931
      (instead of 1.093056). That's on purpose: the function rounds the value to
      4 digits, which should increase accuracy when a large series of operations
      is performed with those numbers.

      The use of the functions is straightforward. You call the DMS function with
      the Degree value you want to convert:

      ^!Clip DMS "15:25:38"
      or
      ^!Clip DMS ^%x%

      The result will be in the ^%DMS% variable. The same applies to the InvDMS,
      which converts Decimals back to Degrees, with the result returned in the
      ^%InvDMS% variable.

      There are three sample clips after the routines for you to try them: "DMS
      Test" and "InvDMS Test", plus an "Add Degrees" clip to show them in action.



      ;======== Start ========
      H="DMS"
      ; RS - 08/25/99 1:36:55 PM
      ; Edited with ClipWriter 1.0
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ; DMS function
      ;Converts value in DMS (Degrees-Minutes-Seconds)
      ;to Decimal value.

      ^!Set h=^&

      ^!Set i=^$StrPos(":";^%h%;True)$
      ^!Set m=0
      ^!Set s=0
      ^!If ^%i% = 0 convert


      ^!Set m=^$StrDelete(^%h%;1;^%i%)$
      ^!Dec i
      ^!Set h=^$StrCopy(^%h%;1;^%i%)$

      ^!Set i=^$StrPos(":";^%m%;True)$
      ^!If ^%i% = 0 convert
      ^!Inc i
      ^!Set s=^$StrCopy(^%m%;^%i;40)$
      ^!Dec i 2
      ^!Set m=^$StrCopy(^%m%;1;^%i%)$



      :convert
      ^!Set DMS=^$Calc((^%h% * 3600 + ^%m% * 60 + ^%s%)/3600;4)$

      ;clean:
      ^!ClearVariable h
      ^!ClearVariable m
      ^!ClearVariable s
      ^!ClearVariable i




      H="InvDMS"
      ; RS - 08/25/99 1:48:13 PM
      ; Edited with ClipWriter 1.0
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ; InvDMS function
      ;Converts decimal value to DMS
      ;(Degrees-Minutes-Seconds)


      ^!Set n=^&

      ;integer:
      ^!Set i=^$StrPos(".";^%n%;True)$
      ^!Dec i
      ^!Set h=^$StrCopy(^%n%;1;^%i%)$

      ;decimals:
      ^!Set x=^$StrDelete(^%n%;1;^%i%)$
      ^!Set x=^$Calc(^%x% * 60;4)$

      ;minutes:
      ^!Set i=^$StrPos(".";^%x%;True)$
      ^!Dec i
      ^!Set m=^$StrCopy(^%x%;1;^%i%)$

      ;seconds:
      ^!Set s=0
      ^!If ^%i% = 0 Next1
      ^!Set x=^$StrDelete(^%x%;1;^%i%)$
      ^!Set s=^$Calc((^%x% * 60) + .5;2)$
      ;just 2 digits:
      ^!Set i=^$StrPos(".";^%s%;True)$
      ^!Dec i
      ^!Set s=^$StrCopy(^%s%;1;^%i%)$


      :Next1
      ;format:
      ^!If ^%m% > 9 skip
      ^!Set m=0^%m%
      ^!If ^%s% > 9 Next2
      ^!Set s=0^%s%

      :Next2
      ^!Set InvDMS=^%h%:^%m%:^%s%

      ;clean:
      ^!ClearVariable h
      ^!ClearVariable i
      ^!ClearVariable m
      ^!ClearVariable n
      ^!ClearVariable s
      ^!ClearVariable x



      H="Test InvDMS"
      ; RS - 08/25/99 1:39:38 PM
      ; Edited with ClipWriter 1.0
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ^!Set a=^?[]
      ^!Clip "InvDMS" ^%a%
      ^!Info ^%InvDMS%

      H="Test DMS"
      ; RS - 08/25/99 1:39:38 PM
      ; Edited with ClipWriter 1.0
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ^!Set a=^?[]
      ^!Clip "DMS" ^%a%
      ^!Info ^%DMS%

      H="Add Degrees"
      ; RS - 08/25/99 1:40:13 PM
      ; Edited with ClipWriter 1.0
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ^!Set x=^?[Value 1:]
      ^!Clip "DMS" ^%x%
      ^!Set x= ^%DMS%

      ^!Set y=^?[Value 2:]
      ^!Clip "DMS" ^%y%
      ^!Set y= ^%DMS%

      ^!Set sum= ^$Calc(^%x% + ^%y%;4)$
      ^!Clip InvDms ^%sum%
      ^!Info ^%InvDMS%
      ;========= End =========
    Your message has been successfully submitted and would be delivered to recipients shortly.