-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal.pl
48 lines (35 loc) · 963 Bytes
/
decimal.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 30 July 2019
# https://github.com/trizen
# Compute a solution to x = 3*tanh(x), starting as x = 2.9847045853578868155597912346...
# https://oeis.org/A309211 -- decimal
# https://oeis.org/A309207 -- cfrac
use 5.020;
use strict;
use warnings;
use experimental qw(signatures);
use Math::AnyNum qw(:overload approx_cmp float tanh);
local $Math::AnyNum::PREC = 50000;
sub binary_inverse ($n, $f, $min = 0, $max = $n) {
($min, $max) = ($max, $min) if ($min > $max);
$min = float($min);
$max = float($max);
for (; ;) {
my $m = ($min + $max) / 2;
my ($x, $y) = $f->($m);
my $c = approx_cmp($x, $y);
if ($c < 0) {
$min = $m;
}
elsif ($c > 0) {
$max = $m;
}
else {
return "$m";
}
}
}
my $x = binary_inverse(4, sub ($x) { ($x, 3*tanh($x)) }, 1, 4);
say $x;
say 3*tanh($x);