From d2deafbd783b61147a0f6692a24041cc689f3ac3 Mon Sep 17 00:00:00 2001 From: John Dupuy Date: Wed, 13 May 2020 19:29:24 -0500 Subject: [PATCH] First pass at three methods of streaming floats --- faststreams/textio.nim | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/faststreams/textio.nim b/faststreams/textio.nim index 9b7ba09..3ae2c3c 100644 --- a/faststreams/textio.nim +++ b/faststreams/textio.nim @@ -91,6 +91,31 @@ proc writeText*(s: OutputStream, x: CompiledIntTypes) = else: writeText(s, MatchingUInt(x)) +when defined(aa): + proc writeText*(s: OutputStream, x: float64|float) = # this version mostly works, but does not account for rounding error + var whole = x.int + writeText(s, whole) + var frac = abs(x - whole.float) + if frac == 0.0: + return + write s, '.' + var limit: byte = 8 + while (frac != 0.0) and (limit > 0): + limit -= 1 + frac *= 10.0 + whole = frac.int + frac -= whole.float + write s, char(ord('0') + whole) + +when defined(bb): + proc writeText*(s: OutputStream, x: float64|float) = # this version just prints the integer part + writeText(s, x.int) + +when defined(cc): + proc writeText*(s: OutputStream, x: float64|float) = # this version uses ftoa of stdlibc + proc floatToStr(x: float): string {.magic: "FloatToStr", noSideEffect.} + writeText(s, floatToStr(x)) + template writeText*(s: OutputStream, str: string) = write s, str