From edbd191f74996e6469cf5d45171046c38851e208 Mon Sep 17 00:00:00 2001 From: apense Date: Wed, 24 Jun 2015 03:22:04 -0400 Subject: [PATCH 1/2] 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 --- lib/pure/math.nim | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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 ## `_. +## +## 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 From bfcbe64778f5bd9580aac9a2ae8c735257a2bade Mon Sep 17 00:00:00 2001 From: apense Date: Wed, 24 Jun 2015 14:49:15 -0400 Subject: [PATCH 2/2] Specific float32/float64 procs I still used generics, but made them choose from `float32` or `float64`. I can rewrite in separate, explicit procs if is wanted --- lib/pure/math.nim | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 2dc66bc25..f83ca6064 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -16,10 +16,6 @@ ## 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 @@ -326,13 +322,13 @@ else: {.pop.} -proc degToRad*[T](d: T): float {.inline.} = +proc degToRad*[T: float32|float64](d: T): T {.inline.} = ## Convert from degrees to radians - result = float(d) * RadPerDeg + result = T(d) * RadPerDeg -proc radToDeg*[T](d: T): float {.inline.} = +proc radToDeg*[T: float32|float64](d: T): T {.inline.} = ## Convert from radians to degrees - result = float(d) / RadPerDeg + result = T(d) / RadPerDeg proc `mod`*(x, y: float): float = result = if y == 0.0: x else: x - y * (x/y).floor