mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-07-28 18:25:16 +00:00
Merge pull request 'Fix count to actually _be_ a count' (#458) from strega-nil/ziglings-exercises:make-threading-mathier into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/458
This commit is contained in:
@@ -28,9 +28,9 @@
|
|||||||
// We will not go into the formula and its derivation in detail, but
|
// We will not go into the formula and its derivation in detail, but
|
||||||
// will deal with the series straight away:
|
// will deal with the series straight away:
|
||||||
//
|
//
|
||||||
// 4 4 4 4 4
|
// 4 4 4 4 4
|
||||||
// PI = --- - --- + --- - --- + --- ...
|
// PI = + --- - --- + --- - --- + --- ...
|
||||||
// 1 3 5 7 9
|
// 1 3 5 7 9
|
||||||
//
|
//
|
||||||
// As you can clearly see, the series starts with the whole number 4 and
|
// As you can clearly see, the series starts with the whole number 4 and
|
||||||
// approaches the circle number by subtracting and adding smaller and
|
// approaches the circle number by subtracting and adding smaller and
|
||||||
@@ -71,27 +71,28 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const count = 1_000_000_000;
|
const count = 500_000_000;
|
||||||
|
|
||||||
var pi_plus: f64 = 0;
|
var pi_plus: f64 = 0;
|
||||||
var pi_minus: f64 = 0;
|
var pi_minus: f64 = 0;
|
||||||
|
|
||||||
{
|
{
|
||||||
// First thread to calculate the plus numbers.
|
// First thread to calculate the plus numbers.
|
||||||
const handle1 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_plus, 5, count });
|
const handle1 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_plus, 1, count });
|
||||||
defer handle1.join();
|
defer handle1.join();
|
||||||
|
|
||||||
// Second thread to calculate the minus numbers.
|
// Second thread to calculate the minus numbers.
|
||||||
???
|
???
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Here we add up the results.
|
// Here we add up the results.
|
||||||
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});
|
std.debug.print("PI ≈ {d:.8} (error = {e:.1})\n", .{ pi_plus - pi_minus, std.math.pi - (pi_plus - pi_minus) });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn thread_pi(pi: *f64, begin: u64, end: u64) !void {
|
fn thread_pi(pi: *f64, begin: u64, count: u64) !void {
|
||||||
var n: u64 = begin;
|
for (0..count) |i| {
|
||||||
while (n < end) : (n += 4) {
|
const term = 4 / @as(f64, @floatFromInt(begin + 4 * i));
|
||||||
pi.* += 4 / @as(f64, @floatFromInt(n));
|
pi.* += term;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If you wish, you can increase the number of loop passes, which
|
// If you wish, you can increase the number of loop passes, which
|
||||||
@@ -106,5 +107,6 @@ fn thread_pi(pi: *f64, begin: u64, end: u64) !void {
|
|||||||
// And you should remove the formatting restriction in "print",
|
// And you should remove the formatting restriction in "print",
|
||||||
// otherwise you will not be able to see the additional digits.
|
// otherwise you will not be able to see the additional digits.
|
||||||
//
|
//
|
||||||
// If count = 10_000_000_000_000 you should see the following:
|
// If count = 2_500_000_000_000, you should see the following:
|
||||||
// 3.141592653589
|
// 3.141592653589
|
||||||
|
// (after waiting on the order of 1000 seconds, or 17 minutes)
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
--- exercises/108_threading2.zig 2026-04-14 06:44:18.848246237 +0200
|
--- exercises/108_threading2.zig 2026-06-29 23:25:53
|
||||||
+++ answers/108_threading2.zig 2026-04-14 08:15:30.894485037 +0200
|
+++ answers/108_threading2.zig 2026-06-29 23:25:25
|
||||||
@@ -81,8 +81,8 @@
|
@@ -82,7 +82,8 @@
|
||||||
defer handle1.join();
|
defer handle1.join();
|
||||||
|
|
||||||
// Second thread to calculate the minus numbers.
|
// Second thread to calculate the minus numbers.
|
||||||
- ???
|
- ???
|
||||||
-
|
|
||||||
+ const handle2 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_minus, 3, count });
|
+ const handle2 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_minus, 3, count });
|
||||||
+ defer handle2.join();
|
+ defer handle2.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Here we add up the results.
|
// Here we add up the results.
|
||||||
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});
|
|
||||||
|
|||||||
@@ -1173,7 +1173,7 @@ const exercises = [_]Exercise{
|
|||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "108_threading2.zig",
|
.main_file = "108_threading2.zig",
|
||||||
.output = "PI ≈ 3.14159265",
|
.output = "PI ≈ 3.14159265 (error = 1.0e-9)",
|
||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "109_files.zig",
|
.main_file = "109_files.zig",
|
||||||
|
|||||||
Reference in New Issue
Block a user