Write float arrays to a text file: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 4 users not shown)
Line 666:
1E+11 3.1623E+05
</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn DoIt
NSUInteger i
CFStringRef s
CFArrayRef x = @[@1, @2, @3, @1e11]
CFArrayRef y = @[@1, @1.4142135623730951, @1.7320508075688772, @316227.76601683791]
CFMutableStringRef mutStr = fn MutableStringWithCapacity(0)
CFURLRef desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
CFURLRef url = fn URLByAppendingPathComponent( desktopURL, @"Array_to_file.txt" )
for i = 0 to 3
if ( i < 3 )
s = fn StringWithFormat( @"%ld \t", fn NumberIntegerValue( x[i] ) )
MutableStringAppendString( mutStr, s )
s = fn StringWithFormat( @"%.4f\n", fn NumberFloatValue( y[i] ) )
MutableStringAppendString( mutStr, s )
else
s = fn StringWithFormat( @"%.e\t", fn NumberFloatValue( x[i] ) )
MutableStringAppendString( mutStr, s )
s = fn StringWithFormat( @"%.4e\n", fn NumberFloatValue( y[i] ) )
MutableStringAppendString( mutStr, s )
end if
next
fn StringWriteToURL( mutStr, url, YES, NSUTF8StringEncoding, NULL )
print mutStr
end fn
 
fn DoIt
 
HandleEven
</syntaxhighlight>
{{output}}
<pre>
1 1.0000
2 1.4142
3 1.7321
1e+11 3.1623e+05
</pre>
 
 
=={{header|Go}}==
Line 971 ⟶ 1,014:
 
io.close( fp )</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Precision here is just Round (as school rounding)
 
Print round(2.5,0)=3
 
Print round(1.5,0)=2
 
Print round(-2.5,0)=-3
 
Print round(-1.5,0)=-2
 
<syntaxhighlight lang="m2000 interpreter">
Module Test1 (filename$, x, xprecision, y, yprecision) {
locale 1033 // set decimal point symbol to "."
// using: for wide output // for UTF16LE
// here we use ANSI (8bit per character)
open filename$ for output as #f
for i=0 to len(x)-1
print #f, format$("{0} {1}", round(x#val(i),xprecision-1), round(y#val(i), yprecision-1))
next
close #f
win "notepad", dir$+filename$
}
Test1 "OutFloat.num", (1, 2, 3, 1.e11),3, (1, 1.4142135623730951, 1.7320508075688772, 316227.76601683791), 5
</syntaxhighlight>
{{out}}
<pre>1 1
2 1.4142
3 1.7321
100000000000 316227.766</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 1,820 ⟶ 1,894:
</pre>
 
=={{header|V (Vlang)}}==
{{incomplete|V (Vlang)|Formatting at least currently doesn't allow variables, floating point notation `g` seems to have bugs}}
<syntaxhighlight lang="textv (vlang)">import os
const (
x = [1.0, 2, 3, 1e11]
Line 1,856 ⟶ 1,930:
 
In the final example, we need to force exponential format as numbers of this size would normally be printed in decimal format.
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./fmt" for Fmt
 
var x = [1, 2, 3, 1e11]
Line 1,878 ⟶ 1,952:
3 1.7321
1e11 3.1623e05
</pre>
 
=={{header|XPL0}}==
Output is redirected to the file like this: writefloat > filename
 
Precision here refers to the size of the numeric field. A precision of 5
means a total of five digits, e.g: 1.2345. XPL0 translates this as one place
before the decimal point and four places after it.
 
<syntaxhighlight lang "XPL0">include xpllib; \for Print
real X, Y;
int N;
[X:= [1., 2., 3., 1e11];
Y:= [1., 1.4142135623730951, 1.7320508075688772, 316227.76601683791];
for N:= 0 to 3 do
Print("%1.2g\t%1.4g\n", X(N), Y(N));
]</syntaxhighlight>
{{out}}
<pre>
1 1
2 1.4142
3 1.7321
1e11 3.1623e5
</pre>
 
9,476

edits