Write float arrays to a text file: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Sidef}}: Fix link: Perl 6 --> Raku) |
(Added Wren) |
||
Line 1,674: | Line 1,674: | ||
3,000E000 1,73205E000 |
3,000E000 1,73205E000 |
||
1,000E011 3,16228E005 |
1,000E011 3,16228E005 |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-fmt}} |
|||
In the above module, 'precision' signifies the number of decimal places whereas for the purpose of this task it appears to mean the number of significant figures. We adjust for that by using a precision one less than the required figure. |
|||
In the final example, we need to force exponential format as numbers of this size would normally be printed in decimal format. |
|||
<lang ecmascript>import "io" for File |
|||
import "/fmt" for Fmt |
|||
var x = [1, 2, 3, 1e11] |
|||
var y = [1, 1.4142135623730951, 1.7320508075688772, 316227.76601683791] |
|||
var xprec = 3 - 1 |
|||
var yprec = 5 - 1 |
|||
File.create("filename.txt") { |file| |
|||
for (i in 0...x.count) { |
|||
var f = (i < x.count-1) ? "h" : "e" |
|||
var s = Fmt.swrite("$0.%(xprec)%(f)\t$0.%(yprec)%(f)\n", x[i], y[i]) |
|||
file.writeBytes(s) |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
The contents of filename.txt: |
|||
<pre> |
|||
1 1 |
|||
2 1.4142 |
|||
3 1.7321 |
|||
1e11 3.1623e05 |
|||
</pre> |
</pre> |
||