summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorapense <apense@users.noreply.github.com>2015-06-24 03:22:04 -0400
committerapense <apense@users.noreply.github.com>2015-06-24 03:22:04 -0400
commitedbd191f74996e6469cf5d45171046c38851e208 (patch)
treea3121f020b9cef95391aa2736d3afaa76f48398e /lib/pure
parent5f371e1504a7a0cc18262b4b7e2ad55de3ab8325 (diff)
downloadNim-edbd191f74996e6469cf5d45171046c38851e208.tar.gz
Conversion between radians and degrees.Fixes #2881
provides a new constant for conversion (not public, but that can be changed if it's wanted). the functions are pretty simple, honestly. top-level comment so documentation for each function could remain. Fixes #2881
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/math.nim17
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index 494dfc4c8..2dc66bc25 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -12,6 +12,14 @@
 ## Basic math routines for Nim.
 ## This module is available for the `JavaScript target
 ## <backends.html#the-javascript-target>`_.
+## 
+## Note that the trigonometric functions naturally operate on radians.
+## The helper functions `degToRad` and `radToDeg` provide conversion
+## between radians and degrees.
+## 
+## Note that the trigonometric functions naturally operate on radians.
+## The helper functions `degToRad` and `radToDeg` provide conversion
+## between radians and degrees.
 
 include "system/inclrtl"
 {.push debugger:off .} # the user does not want to trace a part
@@ -38,6 +46,7 @@ const
                                            ## meaningful digits
                                            ## after the decimal point 
                                            ## for Nim's ``float`` type.
+  RadPerDeg = PI / 180.0 ## number of radians per degree
 
 type
   FloatClass* = enum ## describes the class a floating point value belongs to.
@@ -317,6 +326,14 @@ else:
 
 {.pop.}
 
+proc degToRad*[T](d: T): float {.inline.} =
+  ## Convert from degrees to radians
+  result = float(d) * RadPerDeg
+
+proc radToDeg*[T](d: T): float {.inline.} =
+  ## Convert from radians to degrees
+  result = float(d) / RadPerDeg
+
 proc `mod`*(x, y: float): float =
   result = if y == 0.0: x else: x - y * (x/y).floor