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

Content added Content deleted
m (→‎{{header|Haskell}}: minor details)
m (→‎{{header|Haskell}}: minor details)
Line 1,440: 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.
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 full turns, according to the fact that they all form the same topological space, isomorphic to '''S'''¹ [0, 1).
Isomorphims between all angular types are defined via representation as turns, according to the fact that they all form the same topological space, isomorphic to open interval (-1, 1).


<lang haskell>{-# LANGUAGE RankNTypes #-}
<lang haskell>{-# LANGUAGE RankNTypes #-}
Line 1,485: Line 1,485:
instance Show Rad where
instance Show Rad where
show (Rad r) = printf "∠ %.3f" r
show (Rad r) = printf "%.3f" r
instance Angle Rad where
instance Angle Rad where
Line 1,509: Line 1,509:
instance Show Grad where
instance Show Grad where
show = printf "%.3gg" . value
show (Grad g) = printf "%.3gg" g
instance Angle Grad where
instance Angle Grad where
Line 1,549: Line 1,549:


λ> angle pi :: Rad
λ> angle pi :: Rad
∠ 3.141
∠3.141


λ> angle 1000 :: Gon
λ> angle 1000 :: Gon
Line 1,561: Line 1,561:


λ> (from :: Gon -> Rad) 100
λ> (from :: Gon -> Rad) 100
∠ 1.571
∠1.571


λ> (from :: Rad -> Gon) $ (pi/2)
λ> (from :: Rad -> Gon) $ (pi/2)
Line 1,620: Line 1,620:
<pre>λ> main
<pre>λ> main
converting to radians
converting to radians
[∠ -2.000,∠ -1.000,∠ 0.000,∠ 1.000,∠ 2.000,∠ 6.283,∠ 3.434,∠ 0.747,∠ 0.858,∠ 3.159,∠ 2.717,∠ 5.926]
[-2.000,-1.000,∠0.000,∠1.000,∠2.000,∠6.283,∠3.434,∠0.747,∠0.858,∠3.159,∠2.717,∠5.926]
[∠ -0.035,∠ -0.017,∠ 0.000,∠ 0.017,∠ 0.035,∠ 0.110,∠ 0.279,∠ 1.000,∠ 6.266,∠ 0.681,∠ 4.869,∠ 4.887]
[-0.035,-0.017,∠0.000,∠0.017,∠0.035,∠0.110,∠0.279,∠1.000,∠6.266,∠0.681,∠4.869,∠4.887]
[∠ -0.031,∠ -0.016,∠ 0.000,∠ 0.016,∠ 0.031,∠ 0.099,∠ 0.251,∠ 0.900,∠ 5.639,∠ 6.267,∠ 6.267,∠ 0.000]
[-0.031,-0.016,∠0.000,∠0.016,∠0.031,∠0.099,∠0.251,∠0.900,∠5.639,∠6.267,∠6.267,∠0.000]
[∠ -0.002,∠ -0.001,∠ 0.000,∠ 0.001,∠ 0.002,∠ 0.006,∠ 0.016,∠ 0.056,∠ 0.352,∠ 0.392,∠ 6.282,∠ 1.571]
[-0.002,-0.001,∠0.000,∠0.001,∠0.002,∠0.006,∠0.016,∠0.056,∠0.352,∠0.392,∠6.282,∠1.571]
[∠ -1.107,∠ -0.785,∠ 0.000,∠ 0.785,∠ 1.107,∠ 1.413,∠ 1.508,∠ 1.553,∠ 1.568,∠ 1.568,∠ 1.571,∠ 1.571]
[-1.107,-0.785,∠0.000,∠0.785,∠1.107,∠1.413,∠1.508,∠1.553,∠1.568,∠1.568,∠1.571,∠1.571]


converting to degrees
converting to degrees