Calculating the value of e: Difference between revisions
m
→{{header|Zig}}: Update Zig code to Zig 0.11.0
Recombinant (talk | contribs) m (→{{header|Zig}}: Update Zig code to Zig 0.11.0) |
|||
Line 4,327:
</pre>
=={{header|Zig}}==
{{works with|Zig|0.11.0}}
This uses the continued fraction method to generate the maximum ratio that can be computed using 64 bit math. The final ratio is correct to 35 decimal places.
<syntaxhighlight lang="zig">
const std = @import("std");
const stdout = std.io.getStdOut().writer();▼
pub fn main() !void {
▲ const stdout = std.io.getStdOut().writer();
var n: u32 = 0;
var state: u2 = 0;
Line 4,361 ⟶ 4,362:
},
}
if
const ov3
if
const ov4 = @addWithOverflow(ov3[0], q0);
if (ov4[1] != 0) break;
const q2 = ov4[0];
try stdout.print("e ~= {d:>19} / {d:>19} = ", .{p2, q2});▼
try dec_print(stdout, p2, q2, 36);▼
▲ try stdout.print("e ~= {d:>19} / {d:>19} = ", .{ p2, q2 });
try stdout.writeByte('\n');
p0 = p1;
Line 4,380 ⟶ 4,383:
}
fn
// print out integer part.
try ostream.print("{}.", .{num / den});
Line 4,387 ⟶ 4,390:
// multiply by 10 could potentially overflow a u64.
//
const m: u128 = @intCast(
var r = @as(u128, num) % m;
var dec: usize = 0; // decimal place we're in.
Line 4,394 ⟶ 4,397:
r = n % m;
dec += 1;
const ch = @
try ostream.writeByte(ch);
}
Line 4,443 ⟶ 4,446:
e ~= 5739439214861417731 / 2111421691000680031 = 2.718281828459045235360287471352662497
</pre>
=={{header|zkl}}==
{{trans|C}}
|