diff options
Diffstat (limited to 'lib/std/formatfloat.nim')
-rw-r--r-- | lib/std/formatfloat.nim | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/lib/std/formatfloat.nim b/lib/std/formatfloat.nim index 872549c3b..9258245f6 100644 --- a/lib/std/formatfloat.nim +++ b/lib/std/formatfloat.nim @@ -35,8 +35,8 @@ proc writeFloatToBufferRoundtrip*(buf: var array[65, char]; value: float32): int result = float32ToChars(buf, value, forceTrailingDotZero=true).int buf[result] = '\0' -proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>", - importc: "sprintf", varargs, noSideEffect.} +proc c_snprintf(buf: cstring, n: csize_t, frmt: cstring): cint {.header: "<stdio.h>", + importc: "snprintf", varargs, noSideEffect.} proc writeToBuffer(buf: var array[65, char]; value: cstring) = var i = 0 @@ -49,7 +49,7 @@ proc writeFloatToBufferSprintf*(buf: var array[65, char]; value: BiggestFloat): ## ## returns the amount of bytes written to `buf` not counting the ## terminating '\0' character. - var n = c_sprintf(cast[cstring](addr buf), "%.16g", value).int + var n = c_snprintf(cast[cstring](addr buf), 65, "%.16g", value).int var hasDot = false for i in 0..n-1: if buf[i] == ',': @@ -104,19 +104,19 @@ when defined(js): proc nimFloatToString(a: float): cstring = ## ensures the result doesn't print like an integer, i.e. return 2.0, not 2 # print `-0.0` properly - asm """ + {.emit: """ function nimOnlyDigitsOrMinus(n) { return n.toString().match(/^-?\d+$/); } if (Number.isSafeInteger(`a`)) - `result` = `a` === 0 && 1 / `a` < 0 ? "-0.0" : `a`+".0" + `result` = `a` === 0 && 1 / `a` < 0 ? "-0.0" : `a`+".0"; else { - `result` = `a`+"" + `result` = `a`+""; if(nimOnlyDigitsOrMinus(`result`)){ - `result` = `a`+".0" + `result` = `a`+".0"; } } - """ + """.} proc addFloat*(result: var string; x: float | float32) {.inline.} = ## Converts float to its string representation and appends it to `result`. |