Angles (geometric), normalization and conversion: Difference between revisions

m
m (→‎{{header|Haskell}}: minor details)
m (→‎{{header|Haskell}}: minor details)
Line 1,440:
The solution of this seemingly trivial task could be elegantly done by type classes. Each angle unit is represented as a distinct type, preventing from implicit combination of different units. Moreover, adding new units doesn't imply writing new transformers or normalizers.
 
Isomorphims between all angular types are defined via representation of fullas turns, according to the fact that they all form the same topological space, isomorphic to '''S'''¹open interval [0(-1, 1).
 
<lang haskell>{-# LANGUAGE RankNTypes #-}
Line 1,485:
instance Show Rad where
show (Rad r) = printf "∠ %.3f" r
instance Angle Rad where
Line 1,509:
instance Show Grad where
show (Grad g) = printf "%.3gg" . valueg
instance Angle Grad where
Line 1,549:
 
λ> angle pi :: Rad
∠ 3∠3.141
 
λ> angle 1000 :: Gon
Line 1,561:
 
λ> (from :: Gon -> Rad) 100
∠ 1∠1.571
 
λ> (from :: Rad -> Gon) $ (pi/2)
Line 1,620:
<pre>λ> main
converting to radians
[∠ -2.000,∠ -1.000,∠ 0∠0.000,∠ 1∠1.000,∠ 2∠2.000,∠ 6∠6.283,∠ 3∠3.434,∠ 0∠0.747,∠ 0∠0.858,∠ 3∠3.159,∠ 2∠2.717,∠ 5∠5.926]
[∠ -0.035,∠ -0.017,∠ 0∠0.000,∠ 0∠0.017,∠ 0∠0.035,∠ 0∠0.110,∠ 0∠0.279,∠ 1∠1.000,∠ 6∠6.266,∠ 0∠0.681,∠ 4∠4.869,∠ 4∠4.887]
[∠ -0.031,∠ -0.016,∠ 0∠0.000,∠ 0∠0.016,∠ 0∠0.031,∠ 0∠0.099,∠ 0∠0.251,∠ 0∠0.900,∠ 5∠5.639,∠ 6∠6.267,∠ 6∠6.267,∠ 0∠0.000]
[∠ -0.002,∠ -0.001,∠ 0∠0.000,∠ 0∠0.001,∠ 0∠0.002,∠ 0∠0.006,∠ 0∠0.016,∠ 0∠0.056,∠ 0∠0.352,∠ 0∠0.392,∠ 6∠6.282,∠ 1∠1.571]
[∠ -1.107,∠ -0.785,∠ 0∠0.000,∠ 0∠0.785,∠ 1∠1.107,∠ 1∠1.413,∠ 1∠1.508,∠ 1∠1.553,∠ 1∠1.568,∠ 1∠1.568,∠ 1∠1.571,∠ 1∠1.571]
 
converting to degrees
Anonymous user