Write float arrays to a text file: Difference between revisions

Content added Content deleted
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: Line 1,039:
<lang nim>import strutils, math, sequtils
<lang nim>import strutils, math, sequtils


const OutFileName = "floatarr2file.txt"
const
outFileName = "floatarr2file.txt"


const
const
xprecision = 3
XPrecision = 3
yprecision = 5
Yprecision = 5


var a: seq[float] = @[1.0, 2.0, 3.0, 100_000_000_000.0]
let a = [1.0, 2.0, 3.0, 100_000_000_000.0]
var b: seq[float] = @[sqrt(a[0]), sqrt(a[1]), sqrt(a[2]), sqrt(a[3])]
let b = [sqrt(a[0]), sqrt(a[1]), sqrt(a[2]), sqrt(a[3])]
var c = zip(a, b)
var res = ""
for t in zip(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, ffDefault, yprecision) & "\n")
formatFloat(t[1], ffDefault, Yprecision) & "\n"


writeFile(outFileName, res)
OutFileName.writeFile res
var res2 = readFile(outFileName)
var res2 = OutFileName.readFile()
echo(res2)</lang>
echo res2</lang>
{{out}}
{{out}}
<pre>1.00 1.0000
<pre>1.00 1.0000
2.00 1.4142
2.00 1.4142
3.00 1.7321
3.00 1.7321
1.00e+11 3.1623e+05</pre>
1.00e+11 3.1623e+05</pre>


=={{header|OCaml}}==
=={{header|OCaml}}==