From cea2a9087d44378802c77ba041a4f54cd7b6087d Mon Sep 17 00:00:00 2001 From: katlogic Date: Sun, 15 Jun 2014 01:49:14 +0200 Subject: [PATCH 1/2] More human readable `$`(float) The output matches that of Python (eg 1e100, not 1.0e100), but also reflects locale (assuming it was set using setlocale() before). --- lib/system/ansi_c.nim | 5 ++++- lib/system/repr.nim | 4 ++-- lib/system/sysstr.nim | 14 ++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/system/ansi_c.nim b/lib/system/ansi_c.nim index 5111bc3cf5..da101cc2c6 100644 --- a/lib/system/ansi_c.nim +++ b/lib/system/ansi_c.nim @@ -106,10 +106,13 @@ proc c_fopen(filename, mode: cstring): C_TextFileStar {. importc: "fopen", header: "".} proc c_fclose(f: C_TextFileStar) {.importc: "fclose", header: "".} -proc c_sprintf(buf, frmt: cstring) {.header: "", +proc c_sprintf(buf, frmt: cstring): int {.header: "", importc: "sprintf", varargs, noSideEffect.} # we use it only in a way that cannot lead to security issues +proc c_localeconv():ptr cstring {.header: "", + importc: "localeconv", noSideEffect.} + proc c_fread(buf: pointer, size, n: int, f: C_BinaryFileStar): int {. importc: "fread", header: "".} proc c_fseek(f: C_BinaryFileStar, offset: clong, whence: int): int {. diff --git a/lib/system/repr.nim b/lib/system/repr.nim index 487bac0527..f8f9496680 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -17,12 +17,12 @@ proc reprFloat(x: float): string {.compilerproc.} = return $x proc reprPointer(x: pointer): string {.compilerproc.} = var buf: array [0..59, char] - c_sprintf(buf, "%p", x) + discard c_sprintf(buf, "%p", x) return $buf proc `$`(x: uint64): string = var buf: array [0..59, char] - c_sprintf(buf, "%llu", x) + discard c_sprintf(buf, "%llu", x) return $buf proc reprStrAux(result: var string, s: string) = diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index 4244bae4cb..183ea0c8d1 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -250,10 +250,16 @@ proc nimIntToStr(x: int): string {.compilerRtl.} = for j in 0..i div 2 - 1: swap(result[j], result[i-j-1]) -proc nimFloatToStr(x: float): string {.compilerproc.} = - var buf: array [0..59, char] - c_sprintf(buf, "%#.16e", x) - return $buf +proc nimFloatToStr(f: float): string {.compilerproc.} = + var buf: array [0..64, char] + var n:int = c_sprintf(buf, "%.16g", f) + for i in 0..n-1: + if buf[i] notin {'0'..'9','-'}: + return $buf + buf[n] = c_localeconv()[0] + buf[n+1] = '0' + buf[n+2] = '\0' + result = $buf proc nimInt64ToStr(x: int64): string {.compilerRtl.} = result = newString(sizeof(x)*4) From 9532951cfc4280d815160a4c86c7fec60d2a2001 Mon Sep 17 00:00:00 2001 From: katlogic Date: Sun, 15 Jun 2014 01:53:09 +0200 Subject: [PATCH 2/2] Tests for `$`(float) --- tests/matrix/tmatrix2.nim | 2 +- tests/showoff/tdrdobbs_examples.nim | 2 +- tests/system/tfloatToString.nim | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/system/tfloatToString.nim diff --git a/tests/matrix/tmatrix2.nim b/tests/matrix/tmatrix2.nim index 442096e93a..82990f1a59 100644 --- a/tests/matrix/tmatrix2.nim +++ b/tests/matrix/tmatrix2.nim @@ -1,5 +1,5 @@ discard """ - output: "5.0000000000000000e+00" + output: "5.0" """ type diff --git a/tests/showoff/tdrdobbs_examples.nim b/tests/showoff/tdrdobbs_examples.nim index 8a39990ba5..13a685950e 100644 --- a/tests/showoff/tdrdobbs_examples.nim +++ b/tests/showoff/tdrdobbs_examples.nim @@ -1,7 +1,7 @@ discard """ output: '''108 11 -1 1936 -4.0000000000000002e-01 +0.4 true truefalse''' """ diff --git a/tests/system/tfloatToString.nim b/tests/system/tfloatToString.nim new file mode 100644 index 0000000000..bb45a91d70 --- /dev/null +++ b/tests/system/tfloatToString.nim @@ -0,0 +1,22 @@ +discard """ + output:'''2.3242 +2.982 +123912.1 +123912.1823 +5.0 +1e+100 +inf +-inf +nan +''' +""" + +echo($(2.3242)) +echo($(2.982)) +echo($(123912.1)) +echo($(123912.1823)) +echo($(5.0)) +echo($(1e100)) +echo($(1e1000000)) +echo($(-1e1000000)) +echo($(0.0/0.0))