How to resolve the algorithm Calculating the value of e step by step in the Zig programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Calculating the value of e step by step in the Zig programming language
Table of Contents
Problem Statement
Calculate the value of e.
(e is also known as Euler's number and Napier's constant.)
See details: Calculating the value of e
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Calculating the value of e step by step in the Zig programming language
Source code in the zig programming language
const std = @import("std");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
var n: u32 = 0;
var state: u2 = 0;
var p0: u64 = 0;
var q0: u64 = 1;
var p1: u64 = 1;
var q1: u64 = 0;
while (true) {
var a: u64 = undefined;
switch (state) {
0 => {
a = 2;
state = 1;
},
1 => {
a = 1;
state = 2;
},
2 => {
n += 2;
a = n;
state = 3;
},
3 => {
a = 1;
state = 1;
},
}
const ov1 = @mulWithOverflow(a, p1);
if (ov1[1] != 0) break;
const ov2 = @addWithOverflow(ov1[0], p0);
if (ov2[1] != 0) break;
const ov3 = @mulWithOverflow(a, q1);
if (ov3[1] != 0) break;
const ov4 = @addWithOverflow(ov3[0], q0);
if (ov4[1] != 0) break;
const p2 = ov2[0];
const q2 = ov4[0];
try stdout.print("e ~= {d:>19} / {d:>19} = ", .{ p2, q2 });
try decPrint(stdout, p2, q2, 36);
try stdout.writeByte('\n');
p0 = p1;
p1 = p2;
q0 = q1;
q1 = q2;
}
}
fn decPrint(ostream: anytype, num: u64, den: u64, prec: usize) !void {
// print out integer part.
try ostream.print("{}.", .{num / den});
// arithmetic with the remainders is done with u128, as the
// multiply by 10 could potentially overflow a u64.
//
const m: u128 = @intCast(den);
var r = @as(u128, num) % m;
var dec: usize = 0; // decimal place we're in.
while (dec < prec and r > 0) {
const n = 10 * r;
r = n % m;
dec += 1;
const ch = @as(u8, @intCast(n / m)) + '0';
try ostream.writeByte(ch);
}
}
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Tcl programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Latitude programming language
You may also check:How to resolve the algorithm Function definition step by step in the Sather programming language
You may also check:How to resolve the algorithm Nth root step by step in the langur programming language