0 people like it.

Calculating the angle between the hour and minute hand redux

This snippet introduces a further subtlety into the previous posting, whereby the intra-hour movement of the hour hand is captured.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
let clockAngle (h, m) =
    let hourAngle = (float (h%12) + (float m)/60.0) / 12.0
    let minuteAngle = (float m)/60.0

    let diffDeg = abs (hourAngle - minuteAngle) * 360.0

    if diffDeg > 180.0 then 360.0 - diffDeg else diffDeg

clockAngle (12,0) // 12:00 should be 0
clockAngle (12,5) // 12:05 should be 27.5 deg
clockAngle (12,59) // 12:59 should be 35.5 deg
clockAngle (1, 0) // 1:00 should be 30 deg
clockAngle (12, 60) // aka 1:00!! should be 30 deg
val clockAngle : h:int * m:int -> float

Full name: Script.clockAngle
val h : int
val m : int
val hourAngle : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val minuteAngle : float
val diffDeg : float
val abs : value:'T -> 'T (requires member Abs)

Full name: Microsoft.FSharp.Core.Operators.abs
Raw view Test code New version

More information

Link:http://fssnip.net/6p
Posted:12 years ago
Author:HP
Tags: math , trig