How to resolve the algorithm Write float arrays to a text file step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Write float arrays to a text file step by step in the Raku programming language
Table of Contents
Problem Statement
Write two equal-sized numerical arrays 'x' and 'y' to a two-column text file named 'filename'. The first column of the file contains values from an 'x'-array with a given 'xprecision', the second -- values from 'y'-array with 'yprecision'. For example, considering: The file should look like: This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Write float arrays to a text file step by step in the Raku programming language
Source code in the raku programming language
sub writefloat ( $filename, @x, @y, $x_precision = 3, $y_precision = 5 ) {
my $fh = open $filename, :w;
for flat @x Z @y -> $x, $y {
$fh.printf: "%.*g\t%.*g\n", $x_precision, $x, $y_precision, $y;
}
$fh.close;
}
my @x = 1, 2, 3, 1e11;
my @y = @x.map({.sqrt});
writefloat( 'sqrt.dat', @x, @y );
sub writefloat($filename, @x, @y, :$x-precision = 3, :$y-precision = 3) {
open($filename, :w).print:
join '', flat (@x».fmt("%.{$x-precision}g") X "\t") Z (@y».fmt("%.{$y-precision}g") X "\n");
}
my @x = 1, 2, 3, 1e11;
writefloat('sqrt.dat', @x, @x».sqrt, :y-precision(5));
You may also check:How to resolve the algorithm Quaternion type step by step in the GAP programming language
You may also check:How to resolve the algorithm Move-to-front algorithm step by step in the Racket programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Befunge programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Toka programming language
You may also check:How to resolve the algorithm Date format step by step in the Smalltalk programming language