commit
396b5619fc
6 changed files with 40 additions and 9 deletions
|
|
@ -106,10 +106,13 @@ proc c_fopen(filename, mode: cstring): C_TextFileStar {.
|
||||||
importc: "fopen", header: "<stdio.h>".}
|
importc: "fopen", header: "<stdio.h>".}
|
||||||
proc c_fclose(f: C_TextFileStar) {.importc: "fclose", header: "<stdio.h>".}
|
proc c_fclose(f: C_TextFileStar) {.importc: "fclose", header: "<stdio.h>".}
|
||||||
|
|
||||||
proc c_sprintf(buf, frmt: cstring) {.header: "<stdio.h>",
|
proc c_sprintf(buf, frmt: cstring): int {.header: "<stdio.h>",
|
||||||
importc: "sprintf", varargs, noSideEffect.}
|
importc: "sprintf", varargs, noSideEffect.}
|
||||||
# we use it only in a way that cannot lead to security issues
|
# we use it only in a way that cannot lead to security issues
|
||||||
|
|
||||||
|
proc c_localeconv():ptr cstring {.header: "<locale.h>",
|
||||||
|
importc: "localeconv", noSideEffect.}
|
||||||
|
|
||||||
proc c_fread(buf: pointer, size, n: int, f: C_BinaryFileStar): int {.
|
proc c_fread(buf: pointer, size, n: int, f: C_BinaryFileStar): int {.
|
||||||
importc: "fread", header: "<stdio.h>".}
|
importc: "fread", header: "<stdio.h>".}
|
||||||
proc c_fseek(f: C_BinaryFileStar, offset: clong, whence: int): int {.
|
proc c_fseek(f: C_BinaryFileStar, offset: clong, whence: int): int {.
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,12 @@ proc reprFloat(x: float): string {.compilerproc.} = return $x
|
||||||
|
|
||||||
proc reprPointer(x: pointer): string {.compilerproc.} =
|
proc reprPointer(x: pointer): string {.compilerproc.} =
|
||||||
var buf: array [0..59, char]
|
var buf: array [0..59, char]
|
||||||
c_sprintf(buf, "%p", x)
|
discard c_sprintf(buf, "%p", x)
|
||||||
return $buf
|
return $buf
|
||||||
|
|
||||||
proc `$`(x: uint64): string =
|
proc `$`(x: uint64): string =
|
||||||
var buf: array [0..59, char]
|
var buf: array [0..59, char]
|
||||||
c_sprintf(buf, "%llu", x)
|
discard c_sprintf(buf, "%llu", x)
|
||||||
return $buf
|
return $buf
|
||||||
|
|
||||||
proc reprStrAux(result: var string, s: string) =
|
proc reprStrAux(result: var string, s: string) =
|
||||||
|
|
|
||||||
|
|
@ -250,10 +250,16 @@ proc nimIntToStr(x: int): string {.compilerRtl.} =
|
||||||
for j in 0..i div 2 - 1:
|
for j in 0..i div 2 - 1:
|
||||||
swap(result[j], result[i-j-1])
|
swap(result[j], result[i-j-1])
|
||||||
|
|
||||||
proc nimFloatToStr(x: float): string {.compilerproc.} =
|
proc nimFloatToStr(f: float): string {.compilerproc.} =
|
||||||
var buf: array [0..59, char]
|
var buf: array [0..64, char]
|
||||||
c_sprintf(buf, "%#.16e", x)
|
var n:int = c_sprintf(buf, "%.16g", f)
|
||||||
|
for i in 0..n-1:
|
||||||
|
if buf[i] notin {'0'..'9','-'}:
|
||||||
return $buf
|
return $buf
|
||||||
|
buf[n] = c_localeconv()[0]
|
||||||
|
buf[n+1] = '0'
|
||||||
|
buf[n+2] = '\0'
|
||||||
|
result = $buf
|
||||||
|
|
||||||
proc nimInt64ToStr(x: int64): string {.compilerRtl.} =
|
proc nimInt64ToStr(x: int64): string {.compilerRtl.} =
|
||||||
result = newString(sizeof(x)*4)
|
result = newString(sizeof(x)*4)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
discard """
|
discard """
|
||||||
output: "5.0000000000000000e+00"
|
output: "5.0"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
discard """
|
discard """
|
||||||
output: '''108
|
output: '''108
|
||||||
11 -1 1936
|
11 -1 1936
|
||||||
4.0000000000000002e-01
|
0.4
|
||||||
true
|
true
|
||||||
truefalse'''
|
truefalse'''
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
22
tests/system/tfloatToString.nim
Normal file
22
tests/system/tfloatToString.nim
Normal file
|
|
@ -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))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue