- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathformatfloat.nim
147 lines (131 loc) · 4.51 KB
/
formatfloat.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#
#
# Nim's Runtime Library
# (c) Copyright 2022 Nim contributors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module implements formatting floats as strings.
whendefined(nimPreviewSlimSystem):
import std/assertions
from system/ansi_c import c_memcpy
procaddCstringN(result: varstring, buf: cstring; buflen: int) =
# no nimvm support needed, so it doesn't need to be fast here either
let oldLen =result.len
let newLen = oldLen + buflen
result.setLen newLen
c_memcpy(result[oldLen].addr, buf, buflen.csize_t)
import std/private/[dragonbox, schubfach]
procwriteFloatToBufferRoundtrip*(buf: vararray[65, char]; value: BiggestFloat): int=
## This is the implementation to format floats.
##
## returns the amount of bytes written to `buf` not counting the
## terminating '\0' character.
result=toChars(buf, value, forceTrailingDotZero=true).int
buf[result] ='\0'
procwriteFloatToBufferRoundtrip*(buf: vararray[65, char]; value: float32): int=
result=float32ToChars(buf, value, forceTrailingDotZero=true).int
buf[result] ='\0'
procc_snprintf(buf: cstring, n: csize_t, frmt: cstring): cint {.header: "<stdio.h>",
importc: "snprintf", varargs, noSideEffect.}
procwriteToBuffer(buf: vararray[65, char]; value: cstring) =
var i =0
while value[i] !='\0':
buf[i] = value[i]
inc i
procwriteFloatToBufferSprintf*(buf: vararray[65, char]; value: BiggestFloat): int=
## This is the implementation to format floats.
##
## returns the amount of bytes written to `buf` not counting the
## terminating '\0' character.
var n =c_snprintf(cast[cstring](addr buf), 65, "%.16g", value).int
var hasDot =false
for i in0..n-1:
if buf[i] ==',':
buf[i] ='.'
hasDot =true
elif buf[i] in {'a'..'z', 'A'..'Z', '.'}:
hasDot =true
ifnot hasDot:
buf[n] ='.'
buf[n+1] ='0'
buf[n+2] ='\0'
result= n +2
else:
result= n
# On Windows nice numbers like '1.#INF', '-1.#INF' or '1.#NAN' or 'nan(ind)'
# of '-1.#IND' are produced.
# We want to get rid of these here:
if buf[n-1] in {'n', 'N', 'D', 'd', ')'}:
writeToBuffer(buf, "nan")
result=3
elif buf[n-1] =='F':
if buf[0] =='-':
writeToBuffer(buf, "-inf")
result=4
else:
writeToBuffer(buf, "inf")
result=3
procwriteFloatToBuffer*(buf: vararray[65, char]; value: BiggestFloat|float32): int {.inline.} =
whendefined(nimLegacySprintf):
writeFloatToBufferSprintf(buf, value)
else:
writeFloatToBufferRoundtrip(buf, value)
procaddFloatRoundtrip*(result: varstring; x: float|float32) =
whennimvm:
raiseAssert"unreachable"
else:
var buffer {.noinit.}: array[65, char]
let n =writeFloatToBufferRoundtrip(buffer, x)
result.addCstringN(cast[cstring](buffer[0].addr), n)
procaddFloatSprintf*(result: varstring; x: float) =
whennimvm:
raiseAssert"unreachable"
else:
var buffer {.noinit.}: array[65, char]
let n =writeFloatToBufferSprintf(buffer, x)
result.addCstringN(cast[cstring](buffer[0].addr), n)
whendefined(js):
procnimFloatToString(a: float): cstring=
## ensures the result doesn't print like an integer, i.e. return 2.0, not 2
# print `-0.0` properly
{.emit: """
function nimOnlyDigitsOrMinus(n) {
return n.toString().match(/^-?\d+$/);
}
if (Number.isSafeInteger(`a`))
`result` = `a` === 0 && 1 / `a` < 0 ? "-0.0" : `a`+".0";
else if (isNaN(`a`)) // Number.isNaN is since ES6
`result` = "nan"; // or it'll be "NaN"
else if (!isFinite(`a`)) // Number.isFinite newer but unnecessary here
`result` = `a` > 0 ? "inf" : "-inf"; // or it'll be [-]Infinity
else {
`result` = `a`+"";
if(nimOnlyDigitsOrMinus(`result`)){
`result` = `a`+".0";
}
}
""".}
procaddFloat*(result: varstring; x: float|float32) {.inline.} =
## Converts float to its string representation and appends it to `result`.
runnableExamples:
var
s ="foo:"
b =45.67
s.addFloat(45.67)
assert s =="foo:45.67"
templateimpl=
whendefined(nimLegacySprintf):
addFloatSprintf(result, x)
else:
addFloatRoundtrip(result, x)
whendefined(js):
whennimvm: impl()
else:
result.addnimFloatToString(x)
else: impl()
whendefined(nimPreviewSlimSystem):
func`$`*(x: float|float32): string=
## Outplace version of `addFloat`.
result.addFloat(x)