Jump to content

Write float arrays to a text file: Difference between revisions

Updated to work with Nim 1.4: changed "t.a" to "t[0]" and "t.b" to "t[1]". Replaced tabulation with spaces. Other minor changes.
m (→‎{{header|Phix}}: added syntax colouring the hard way)
(Updated to work with Nim 1.4: changed "t.a" to "t[0]" and "t.b" to "t[1]". Replaced tabulation with spaces. Other minor changes.)
Line 1,039:
<lang nim>import strutils, math, sequtils
 
const outFileNameOutFileName = "floatarr2file.txt"
const
outFileName = "floatarr2file.txt"
 
const
xprecisionXPrecision = 3
yprecisionYprecision = 5
 
varlet a: seq[float] = @[1.0, 2.0, 3.0, 100_000_000_000.0]
varlet b: seq[float] = @[sqrt(a[0]), sqrt(a[1]), sqrt(a[2]), sqrt(a[3])]
var cres = zip(a, b)""
for t in czip(a, b):
var res: string = ""
res.add formatFloat(t[0], ffDefault, Xprecision) & " " &
for t in c:
res.add(formatFloat(t.a, ffDefault, xprecision) & "\t" & formatFloat(t.b[1], ffDefault, yprecisionYprecision) & "\n")
 
OutFileName.writeFile(outFileName, res)
var res2 = OutFileName.readFile(outFileName)
echo( res2)</lang>
{{out}}
<pre>1.00 1.0000
2.00 1.4142
3.00 1.7321
1.00e+11 3.1623e+05</pre>
 
=={{header|OCaml}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.