Sum of a series: Difference between revisions
Content added Content deleted
(→{{header|RPL}}: HP-49+ version) |
Recombinant (talk | contribs) m (→Zig: Change sum to u64 (from i64). Update Zig code to Zig 0.11.0) |
||
Line 3,379: | Line 3,379: | ||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
{{Works with|Zig|0.11.0}} |
|||
<syntaxhighlight lang="zig">const std = @import("std"); |
<syntaxhighlight lang="zig">const std = @import("std"); |
||
fn f(x: |
fn f(x: u64) f64 { |
||
return 1/@ |
return 1 / @as(f64, @floatFromInt(x * x)); |
||
} |
} |
||
fn |
fn sum(comptime func: fn (u64) f64, n: u64) f64 { |
||
var s: f64 = 0.0; |
var s: f64 = 0.0; |
||
var i: |
var i: u64 = n; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
return s; |
return s; |
||
} |
} |
||
pub fn main() !void |
pub fn main() !void { |
||
{ |
|||
const stdout = std.io.getStdOut().writer(); |
const stdout = std.io.getStdOut().writer(); |
||
try stdout.print("S_1000 = {d:.15}\n", .{ |
try stdout.print("S_1000 = {d:.15}\n", .{sum(f, 1000)}); |
||
}</syntaxhighlight> |
}</syntaxhighlight>{{out}} |
||
{{out}} |
|||
<pre>S_1000 = 1.643934566681560</pre> |
<pre>S_1000 = 1.643934566681560</pre> |
||