Implemented function to split floating point numbers at the decimal place (equivalent to C's modf function). Fixes #4195.

This commit is contained in:
A. S. Budden
2016-05-31 13:26:41 +01:00
parent 0cc7c9a13c
commit 46a2993917
2 changed files with 27 additions and 0 deletions

View File

@@ -312,6 +312,23 @@ else:
exponent = round(ex)
result = x / pow(2.0, ex)
proc splitDecimal*[T: float32|float64](x: T): tuple[intpart: T, floatpart: T] =
## Breaks `x` into an integral and a fractional part.
##
## Returns a tuple containing intpart and floatpart representing
## the integer part and the fractional part respectively.
##
## Both parts have the same sign as `x`. Analogous to the `modf`
## function in C.
var
absolute: T
absolute = abs(x)
result.intpart = floor(absolute)
result.floatpart = absolute - result.intpart
if x < 0:
result.intpart = -result.intpart
result.floatpart = -result.floatpart
{.pop.}
proc degToRad*[T: float32|float64](d: T): T {.inline.} =
@@ -399,3 +416,11 @@ when isMainModule:
doAssert floatIsEqual(round(-547.652, -2), -500.0)
doAssert floatIsEqual(round(-547.652, -3), -1000.0)
doAssert floatIsEqual(round(-547.652, -4), 0.0)
block: # splitDecimal() tests
doAssert floatIsEqual(splitDecimal(54.674).intpart, 54.0)
doAssert floatIsEqual(splitDecimal(54.674).floatpart, 0.674)
doAssert floatIsEqual(splitDecimal(-693.4356).intpart, -693.0)
doAssert floatIsEqual(splitDecimal(-693.4356).floatpart, -0.4356)
doAssert floatIsEqual(splitDecimal(0.0).intpart, 0.0)
doAssert floatIsEqual(splitDecimal(0.0).floatpart, 0.0)

View File

@@ -61,6 +61,8 @@ Library Additions
- Added ``strscans`` module that implements a ``scanf`` for easy input extraction.
- Added a version of ``parseutils.parseUntil`` that can deal with a string ``until`` token. The other
versions are for ``char`` and ``set[char]``.
- Added ``splitDecimal`` to ``math.nim`` to split a floating point value
into an integer part and a floating part (in the range -1<x<1).
Compiler Additions