開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Perl 6 (プログラミング言語)
- Rakudo(コンパイラ、実装)
追記: 2017/04/24
浮動小数点数の取扱いの注意点についてを perl 6、perl 5、python (python3)、ruby(2.4)、cでも試して結果を確認してみた。
@kagamihr @mkamimura 何が起きてるんだ?面白そう?と思ってコード眺めてみましたが、perl版のバグですね。間違えてfを2回呼んで比較してる。直せば他の言語と同じ結果っぽいです。
— kashi (@mkashi) 2017年4月28日
Perl 5と Perl 6では、少なくとも試した範囲だと誤差は出ないみたい。他の言語とは浮動小数点数の実装方法が違ったりするからかなぁ。(実数について、有理数と無理数の実装が違って、有理数は正確になるような実装とか?)
コード(Emacs)
perl 6
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- sub f($p, $q) { (1 - $p) + $p * (1 - $q); } sub g($p, $q) { 1 - $p * $q; } say '==, p, q, (1 - p) + p * (1 - q), 1 - p * q'; for 0..9 -> $p { for 0..9 -> $q { my $p0 = $p / 10; my $q0 = $q / 10; my $a1 = f($p0, $q0); my $a2 = g($p0, $q0); say $a1 == $a2, ", $p0, $q0, $a1, $a2"; } }
perl 5
#!/usr/bin/env perl # -*- coding: utf-8 -*- sub f { my ($p, $q) = @_; (1 - $p) + $p * (1 - $q); } sub g { my ($p, $q) = @_; 1 - $p * $q; } print "==, p, q, (1 - p) + p * (1 - q), 1 - p * q\n"; for my $p (0..9) { for my $q (0..9) { my $p0 = $p / 10.0; my $q0 = $q / 10.0; my $a1 = f($p0, $q0); my $a2 = g($p0, $q0); print $a1 == $a2 ? "True" : "False", ", $p0, $q0, $a1, $a2\n"; } }
python 3
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def f(p, q): return (1 - p) + p * (1 - q) def g(p, q): return 1 - p * q print('==, p, q, (1 - p) + p * (1 - q), 1 - p * q') for p in range(10): for q in range(10): p0 = p / 10 q0 = q / 10 a1 = f(p0, q0) a2 = g(p0, q0) print('{0}, {1}, {2}, {3}, {4}'.format(a1 == a2, p0, q0, a1, a2))
ruby 2.4
#!/usr/bin/env ruby2.4 # -*- coding: utf-8 -*- def f(p, q) (1 - p) + p * (1 - q) end def g(p, q) 1 - p * q end puts '==, p, q, (1 - p) + p * (1 - q), 1 - p * q' (0..9).each do |p| (0..9).each do |q| p0 = p / 10.0 q0 = q / 10.0 a1 = f p0, q0 a2 = g p0, q0 puts "#{a1 == a2}, #{p0}, #{q0}, #{a1}, #{a2}" end end
c
#include <stdio.h> double f(double p, double q) { return (1 - p) + p * (1 - q); } double g(double p, double q) { return 1 - p * q; } int main() { printf("bool, p, q, (1 - p) + p * (1 - q), 1 - p * q\n"); for (double p = 0; p < 1; p += 0.1) { for (double q = 0; q < 1; q += 0.1) { double a1 = f(p, q); double a2 = g(p, q); printf("%s, %f, %f, %f, %f\n", a1 == a2 ? "true" : "false", p, q, a1, a2); } } }
入出力結果(Terminal, REPL)
$ ./sample1.pl6 ==, p, q, (1 - p) + p * (1 - q), 1 - p * q True, 0, 0, 1, 1 True, 0, 0.1, 1, 1 True, 0, 0.2, 1, 1 True, 0, 0.3, 1, 1 True, 0, 0.4, 1, 1 True, 0, 0.5, 1, 1 True, 0, 0.6, 1, 1 True, 0, 0.7, 1, 1 True, 0, 0.8, 1, 1 True, 0, 0.9, 1, 1 True, 0.1, 0, 1, 1 True, 0.1, 0.1, 0.99, 0.99 True, 0.1, 0.2, 0.98, 0.98 True, 0.1, 0.3, 0.97, 0.97 True, 0.1, 0.4, 0.96, 0.96 True, 0.1, 0.5, 0.95, 0.95 True, 0.1, 0.6, 0.94, 0.94 True, 0.1, 0.7, 0.93, 0.93 True, 0.1, 0.8, 0.92, 0.92 True, 0.1, 0.9, 0.91, 0.91 True, 0.2, 0, 1, 1 True, 0.2, 0.1, 0.98, 0.98 True, 0.2, 0.2, 0.96, 0.96 True, 0.2, 0.3, 0.94, 0.94 True, 0.2, 0.4, 0.92, 0.92 True, 0.2, 0.5, 0.9, 0.9 True, 0.2, 0.6, 0.88, 0.88 True, 0.2, 0.7, 0.86, 0.86 True, 0.2, 0.8, 0.84, 0.84 True, 0.2, 0.9, 0.82, 0.82 True, 0.3, 0, 1, 1 True, 0.3, 0.1, 0.97, 0.97 True, 0.3, 0.2, 0.94, 0.94 True, 0.3, 0.3, 0.91, 0.91 True, 0.3, 0.4, 0.88, 0.88 True, 0.3, 0.5, 0.85, 0.85 True, 0.3, 0.6, 0.82, 0.82 True, 0.3, 0.7, 0.79, 0.79 True, 0.3, 0.8, 0.76, 0.76 True, 0.3, 0.9, 0.73, 0.73 True, 0.4, 0, 1, 1 True, 0.4, 0.1, 0.96, 0.96 True, 0.4, 0.2, 0.92, 0.92 True, 0.4, 0.3, 0.88, 0.88 True, 0.4, 0.4, 0.84, 0.84 True, 0.4, 0.5, 0.8, 0.8 True, 0.4, 0.6, 0.76, 0.76 True, 0.4, 0.7, 0.72, 0.72 True, 0.4, 0.8, 0.68, 0.68 True, 0.4, 0.9, 0.64, 0.64 True, 0.5, 0, 1, 1 True, 0.5, 0.1, 0.95, 0.95 True, 0.5, 0.2, 0.9, 0.9 True, 0.5, 0.3, 0.85, 0.85 True, 0.5, 0.4, 0.8, 0.8 True, 0.5, 0.5, 0.75, 0.75 True, 0.5, 0.6, 0.7, 0.7 True, 0.5, 0.7, 0.65, 0.65 True, 0.5, 0.8, 0.6, 0.6 True, 0.5, 0.9, 0.55, 0.55 True, 0.6, 0, 1, 1 True, 0.6, 0.1, 0.94, 0.94 True, 0.6, 0.2, 0.88, 0.88 True, 0.6, 0.3, 0.82, 0.82 True, 0.6, 0.4, 0.76, 0.76 True, 0.6, 0.5, 0.7, 0.7 True, 0.6, 0.6, 0.64, 0.64 True, 0.6, 0.7, 0.58, 0.58 True, 0.6, 0.8, 0.52, 0.52 True, 0.6, 0.9, 0.46, 0.46 True, 0.7, 0, 1, 1 True, 0.7, 0.1, 0.93, 0.93 True, 0.7, 0.2, 0.86, 0.86 True, 0.7, 0.3, 0.79, 0.79 True, 0.7, 0.4, 0.72, 0.72 True, 0.7, 0.5, 0.65, 0.65 True, 0.7, 0.6, 0.58, 0.58 True, 0.7, 0.7, 0.51, 0.51 True, 0.7, 0.8, 0.44, 0.44 True, 0.7, 0.9, 0.37, 0.37 True, 0.8, 0, 1, 1 True, 0.8, 0.1, 0.92, 0.92 True, 0.8, 0.2, 0.84, 0.84 True, 0.8, 0.3, 0.76, 0.76 True, 0.8, 0.4, 0.68, 0.68 True, 0.8, 0.5, 0.6, 0.6 True, 0.8, 0.6, 0.52, 0.52 True, 0.8, 0.7, 0.44, 0.44 True, 0.8, 0.8, 0.36, 0.36 True, 0.8, 0.9, 0.28, 0.28 True, 0.9, 0, 1, 1 True, 0.9, 0.1, 0.91, 0.91 True, 0.9, 0.2, 0.82, 0.82 True, 0.9, 0.3, 0.73, 0.73 True, 0.9, 0.4, 0.64, 0.64 True, 0.9, 0.5, 0.55, 0.55 True, 0.9, 0.6, 0.46, 0.46 True, 0.9, 0.7, 0.37, 0.37 True, 0.9, 0.8, 0.28, 0.28 True, 0.9, 0.9, 0.19, 0.19 $ ./sample1.pl ==, p, q, (1 - p) + p * (1 - q), 1 - p * q True, 0, 0, 1, 1 True, 0, 0.1, 1, 1 True, 0, 0.2, 1, 1 True, 0, 0.3, 1, 1 True, 0, 0.4, 1, 1 True, 0, 0.5, 1, 1 True, 0, 0.6, 1, 1 True, 0, 0.7, 1, 1 True, 0, 0.8, 1, 1 True, 0, 0.9, 1, 1 True, 0.1, 0, 1, 1 True, 0.1, 0.1, 0.99, 0.99 True, 0.1, 0.2, 0.98, 0.98 True, 0.1, 0.3, 0.97, 0.97 True, 0.1, 0.4, 0.96, 0.96 False, 0.1, 0.5, 0.95, 0.95 False, 0.1, 0.6, 0.94, 0.94 True, 0.1, 0.7, 0.93, 0.93 False, 0.1, 0.8, 0.92, 0.92 True, 0.1, 0.9, 0.91, 0.91 True, 0.2, 0, 1, 1 False, 0.2, 0.1, 0.98, 0.98 False, 0.2, 0.2, 0.96, 0.96 False, 0.2, 0.3, 0.94, 0.94 False, 0.2, 0.4, 0.92, 0.92 True, 0.2, 0.5, 0.9, 0.9 False, 0.2, 0.6, 0.88, 0.88 False, 0.2, 0.7, 0.86, 0.86 False, 0.2, 0.8, 0.84, 0.84 False, 0.2, 0.9, 0.82, 0.82 True, 0.3, 0, 1, 1 True, 0.3, 0.1, 0.97, 0.97 True, 0.3, 0.2, 0.94, 0.94 False, 0.3, 0.3, 0.91, 0.91 False, 0.3, 0.4, 0.88, 0.88 True, 0.3, 0.5, 0.85, 0.85 False, 0.3, 0.6, 0.82, 0.82 False, 0.3, 0.7, 0.79, 0.79 False, 0.3, 0.8, 0.76, 0.76 True, 0.3, 0.9, 0.73, 0.73 True, 0.4, 0, 1, 1 True, 0.4, 0.1, 0.96, 0.96 False, 0.4, 0.2, 0.92, 0.92 False, 0.4, 0.3, 0.88, 0.88 True, 0.4, 0.4, 0.84, 0.84 True, 0.4, 0.5, 0.8, 0.8 True, 0.4, 0.6, 0.76, 0.76 True, 0.4, 0.7, 0.72, 0.72 True, 0.4, 0.8, 0.68, 0.68 False, 0.4, 0.9, 0.64, 0.64 True, 0.5, 0, 1, 1 True, 0.5, 0.1, 0.95, 0.95 True, 0.5, 0.2, 0.9, 0.9 True, 0.5, 0.3, 0.85, 0.85 True, 0.5, 0.4, 0.8, 0.8 True, 0.5, 0.5, 0.75, 0.75 True, 0.5, 0.6, 0.7, 0.7 True, 0.5, 0.7, 0.65, 0.65 True, 0.5, 0.8, 0.6, 0.6 True, 0.5, 0.9, 0.55, 0.55 True, 0.6, 0, 1, 1 False, 0.6, 0.1, 0.94, 0.94 True, 0.6, 0.2, 0.88, 0.88 True, 0.6, 0.3, 0.82, 0.82 True, 0.6, 0.4, 0.76, 0.76 True, 0.6, 0.5, 0.7, 0.7 True, 0.6, 0.6, 0.64, 0.64 True, 0.6, 0.7, 0.58, 0.58 True, 0.6, 0.8, 0.52, 0.52 False, 0.6, 0.9, 0.46, 0.46 True, 0.7, 0, 1, 1 True, 0.7, 0.1, 0.93, 0.93 True, 0.7, 0.2, 0.86, 0.86 True, 0.7, 0.3, 0.79, 0.79 True, 0.7, 0.4, 0.72, 0.72 True, 0.7, 0.5, 0.65, 0.65 True, 0.7, 0.6, 0.58, 0.58 True, 0.7, 0.7, 0.51, 0.51 False, 0.7, 0.8, 0.44, 0.44 True, 0.7, 0.9, 0.37, 0.37 True, 0.8, 0, 1, 1 False, 0.8, 0.1, 0.92, 0.92 False, 0.8, 0.2, 0.84, 0.84 False, 0.8, 0.3, 0.76, 0.76 True, 0.8, 0.4, 0.68, 0.68 True, 0.8, 0.5, 0.6, 0.6 True, 0.8, 0.6, 0.52, 0.52 False, 0.8, 0.7, 0.44, 0.44 False, 0.8, 0.8, 0.36, 0.36 True, 0.8, 0.9, 0.28, 0.28 True, 0.9, 0, 1, 1 True, 0.9, 0.1, 0.91, 0.91 False, 0.9, 0.2, 0.82, 0.82 True, 0.9, 0.3, 0.73, 0.73 False, 0.9, 0.4, 0.64, 0.64 True, 0.9, 0.5, 0.55, 0.55 False, 0.9, 0.6, 0.46, 0.46 False, 0.9, 0.7, 0.37, 0.37 True, 0.9, 0.8, 0.28, 0.28 True, 0.9, 0.9, 0.19, 0.19 $ ./sample1.py ==, p, q, (1 - p) + p * (1 - q), 1 - p * q True, 0.0, 0.0, 1.0, 1.0 True, 0.0, 0.1, 1.0, 1.0 True, 0.0, 0.2, 1.0, 1.0 True, 0.0, 0.3, 1.0, 1.0 True, 0.0, 0.4, 1.0, 1.0 True, 0.0, 0.5, 1.0, 1.0 True, 0.0, 0.6, 1.0, 1.0 True, 0.0, 0.7, 1.0, 1.0 True, 0.0, 0.8, 1.0, 1.0 True, 0.0, 0.9, 1.0, 1.0 True, 0.1, 0.0, 1.0, 1.0 True, 0.1, 0.1, 0.99, 0.99 True, 0.1, 0.2, 0.98, 0.98 True, 0.1, 0.3, 0.97, 0.97 True, 0.1, 0.4, 0.96, 0.96 False, 0.1, 0.5, 0.9500000000000001, 0.95 False, 0.1, 0.6, 0.9400000000000001, 0.94 True, 0.1, 0.7, 0.93, 0.93 False, 0.1, 0.8, 0.92, 0.9199999999999999 True, 0.1, 0.9, 0.91, 0.91 True, 0.2, 0.0, 1.0, 1.0 False, 0.2, 0.1, 0.9800000000000001, 0.98 False, 0.2, 0.2, 0.9600000000000001, 0.96 False, 0.2, 0.3, 0.9400000000000001, 0.94 False, 0.2, 0.4, 0.92, 0.9199999999999999 True, 0.2, 0.5, 0.9, 0.9 False, 0.2, 0.6, 0.8800000000000001, 0.88 False, 0.2, 0.7, 0.8600000000000001, 0.86 False, 0.2, 0.8, 0.8400000000000001, 0.84 False, 0.2, 0.9, 0.8200000000000001, 0.82 True, 0.3, 0.0, 1.0, 1.0 True, 0.3, 0.1, 0.97, 0.97 True, 0.3, 0.2, 0.94, 0.94 False, 0.3, 0.3, 0.9099999999999999, 0.91 False, 0.3, 0.4, 0.8799999999999999, 0.88 True, 0.3, 0.5, 0.85, 0.85 False, 0.3, 0.6, 0.82, 0.8200000000000001 False, 0.3, 0.7, 0.7899999999999999, 0.79 False, 0.3, 0.8, 0.7599999999999999, 0.76 True, 0.3, 0.9, 0.73, 0.73 True, 0.4, 0.0, 1.0, 1.0 True, 0.4, 0.1, 0.96, 0.96 False, 0.4, 0.2, 0.92, 0.9199999999999999 False, 0.4, 0.3, 0.8799999999999999, 0.88 True, 0.4, 0.4, 0.84, 0.84 True, 0.4, 0.5, 0.8, 0.8 True, 0.4, 0.6, 0.76, 0.76 True, 0.4, 0.7, 0.72, 0.72 True, 0.4, 0.8, 0.6799999999999999, 0.6799999999999999 False, 0.4, 0.9, 0.64, 0.6399999999999999 True, 0.5, 0.0, 1.0, 1.0 True, 0.5, 0.1, 0.95, 0.95 True, 0.5, 0.2, 0.9, 0.9 True, 0.5, 0.3, 0.85, 0.85 True, 0.5, 0.4, 0.8, 0.8 True, 0.5, 0.5, 0.75, 0.75 True, 0.5, 0.6, 0.7, 0.7 True, 0.5, 0.7, 0.65, 0.65 True, 0.5, 0.8, 0.6, 0.6 True, 0.5, 0.9, 0.55, 0.55 True, 0.6, 0.0, 1.0, 1.0 False, 0.6, 0.1, 0.9400000000000001, 0.94 True, 0.6, 0.2, 0.88, 0.88 True, 0.6, 0.3, 0.8200000000000001, 0.8200000000000001 True, 0.6, 0.4, 0.76, 0.76 True, 0.6, 0.5, 0.7, 0.7 True, 0.6, 0.6, 0.64, 0.64 True, 0.6, 0.7, 0.5800000000000001, 0.5800000000000001 True, 0.6, 0.8, 0.52, 0.52 False, 0.6, 0.9, 0.46, 0.45999999999999996 True, 0.7, 0.0, 1.0, 1.0 True, 0.7, 0.1, 0.93, 0.93 True, 0.7, 0.2, 0.86, 0.86 True, 0.7, 0.3, 0.79, 0.79 True, 0.7, 0.4, 0.72, 0.72 True, 0.7, 0.5, 0.65, 0.65 True, 0.7, 0.6, 0.5800000000000001, 0.5800000000000001 True, 0.7, 0.7, 0.51, 0.51 False, 0.7, 0.8, 0.44, 0.44000000000000006 True, 0.7, 0.9, 0.37, 0.37 True, 0.8, 0.0, 1.0, 1.0 False, 0.8, 0.1, 0.92, 0.9199999999999999 False, 0.8, 0.2, 0.8400000000000001, 0.84 False, 0.8, 0.3, 0.7599999999999999, 0.76 True, 0.8, 0.4, 0.6799999999999999, 0.6799999999999999 True, 0.8, 0.5, 0.6, 0.6 True, 0.8, 0.6, 0.52, 0.52 False, 0.8, 0.7, 0.44, 0.44000000000000006 False, 0.8, 0.8, 0.35999999999999993, 0.3599999999999999 True, 0.8, 0.9, 0.2799999999999999, 0.2799999999999999 True, 0.9, 0.0, 1.0, 1.0 True, 0.9, 0.1, 0.91, 0.91 False, 0.9, 0.2, 0.8200000000000001, 0.82 True, 0.9, 0.3, 0.73, 0.73 False, 0.9, 0.4, 0.64, 0.6399999999999999 True, 0.9, 0.5, 0.55, 0.55 False, 0.9, 0.6, 0.46, 0.45999999999999996 False, 0.9, 0.7, 0.37000000000000005, 0.37 True, 0.9, 0.8, 0.2799999999999999, 0.2799999999999999 True, 0.9, 0.9, 0.18999999999999995, 0.18999999999999995 $ ./sample1.rb ==, p, q, (1 - p) + p * (1 - q), 1 - p * q true, 0.0, 0.0, 1.0, 1.0 true, 0.0, 0.1, 1.0, 1.0 true, 0.0, 0.2, 1.0, 1.0 true, 0.0, 0.3, 1.0, 1.0 true, 0.0, 0.4, 1.0, 1.0 true, 0.0, 0.5, 1.0, 1.0 true, 0.0, 0.6, 1.0, 1.0 true, 0.0, 0.7, 1.0, 1.0 true, 0.0, 0.8, 1.0, 1.0 true, 0.0, 0.9, 1.0, 1.0 true, 0.1, 0.0, 1.0, 1.0 true, 0.1, 0.1, 0.99, 0.99 true, 0.1, 0.2, 0.98, 0.98 true, 0.1, 0.3, 0.97, 0.97 true, 0.1, 0.4, 0.96, 0.96 false, 0.1, 0.5, 0.9500000000000001, 0.95 false, 0.1, 0.6, 0.9400000000000001, 0.94 true, 0.1, 0.7, 0.93, 0.93 false, 0.1, 0.8, 0.92, 0.9199999999999999 true, 0.1, 0.9, 0.91, 0.91 true, 0.2, 0.0, 1.0, 1.0 false, 0.2, 0.1, 0.9800000000000001, 0.98 false, 0.2, 0.2, 0.9600000000000001, 0.96 false, 0.2, 0.3, 0.9400000000000001, 0.94 false, 0.2, 0.4, 0.92, 0.9199999999999999 true, 0.2, 0.5, 0.9, 0.9 false, 0.2, 0.6, 0.8800000000000001, 0.88 false, 0.2, 0.7, 0.8600000000000001, 0.86 false, 0.2, 0.8, 0.8400000000000001, 0.84 false, 0.2, 0.9, 0.8200000000000001, 0.82 true, 0.3, 0.0, 1.0, 1.0 true, 0.3, 0.1, 0.97, 0.97 true, 0.3, 0.2, 0.94, 0.94 false, 0.3, 0.3, 0.9099999999999999, 0.91 false, 0.3, 0.4, 0.8799999999999999, 0.88 true, 0.3, 0.5, 0.85, 0.85 false, 0.3, 0.6, 0.82, 0.8200000000000001 false, 0.3, 0.7, 0.7899999999999999, 0.79 false, 0.3, 0.8, 0.7599999999999999, 0.76 true, 0.3, 0.9, 0.73, 0.73 true, 0.4, 0.0, 1.0, 1.0 true, 0.4, 0.1, 0.96, 0.96 false, 0.4, 0.2, 0.92, 0.9199999999999999 false, 0.4, 0.3, 0.8799999999999999, 0.88 true, 0.4, 0.4, 0.84, 0.84 true, 0.4, 0.5, 0.8, 0.8 true, 0.4, 0.6, 0.76, 0.76 true, 0.4, 0.7, 0.72, 0.72 true, 0.4, 0.8, 0.6799999999999999, 0.6799999999999999 false, 0.4, 0.9, 0.64, 0.6399999999999999 true, 0.5, 0.0, 1.0, 1.0 true, 0.5, 0.1, 0.95, 0.95 true, 0.5, 0.2, 0.9, 0.9 true, 0.5, 0.3, 0.85, 0.85 true, 0.5, 0.4, 0.8, 0.8 true, 0.5, 0.5, 0.75, 0.75 true, 0.5, 0.6, 0.7, 0.7 true, 0.5, 0.7, 0.65, 0.65 true, 0.5, 0.8, 0.6, 0.6 true, 0.5, 0.9, 0.55, 0.55 true, 0.6, 0.0, 1.0, 1.0 false, 0.6, 0.1, 0.9400000000000001, 0.94 true, 0.6, 0.2, 0.88, 0.88 true, 0.6, 0.3, 0.8200000000000001, 0.8200000000000001 true, 0.6, 0.4, 0.76, 0.76 true, 0.6, 0.5, 0.7, 0.7 true, 0.6, 0.6, 0.64, 0.64 true, 0.6, 0.7, 0.5800000000000001, 0.5800000000000001 true, 0.6, 0.8, 0.52, 0.52 false, 0.6, 0.9, 0.46, 0.45999999999999996 true, 0.7, 0.0, 1.0, 1.0 true, 0.7, 0.1, 0.93, 0.93 true, 0.7, 0.2, 0.86, 0.86 true, 0.7, 0.3, 0.79, 0.79 true, 0.7, 0.4, 0.72, 0.72 true, 0.7, 0.5, 0.65, 0.65 true, 0.7, 0.6, 0.5800000000000001, 0.5800000000000001 true, 0.7, 0.7, 0.51, 0.51 false, 0.7, 0.8, 0.44, 0.44000000000000006 true, 0.7, 0.9, 0.37, 0.37 true, 0.8, 0.0, 1.0, 1.0 false, 0.8, 0.1, 0.92, 0.9199999999999999 false, 0.8, 0.2, 0.8400000000000001, 0.84 false, 0.8, 0.3, 0.7599999999999999, 0.76 true, 0.8, 0.4, 0.6799999999999999, 0.6799999999999999 true, 0.8, 0.5, 0.6, 0.6 true, 0.8, 0.6, 0.52, 0.52 false, 0.8, 0.7, 0.44, 0.44000000000000006 false, 0.8, 0.8, 0.35999999999999993, 0.3599999999999999 true, 0.8, 0.9, 0.2799999999999999, 0.2799999999999999 true, 0.9, 0.0, 1.0, 1.0 true, 0.9, 0.1, 0.91, 0.91 false, 0.9, 0.2, 0.8200000000000001, 0.82 true, 0.9, 0.3, 0.73, 0.73 false, 0.9, 0.4, 0.64, 0.6399999999999999 true, 0.9, 0.5, 0.55, 0.55 false, 0.9, 0.6, 0.46, 0.45999999999999996 false, 0.9, 0.7, 0.37000000000000005, 0.37 true, 0.9, 0.8, 0.2799999999999999, 0.2799999999999999 true, 0.9, 0.9, 0.18999999999999995, 0.18999999999999995 $ ./sample1 bool, p, q, (1 - p) + p * (1 - q), 1 - p * q true, 0.000000, 0.000000, 1.000000, 1.000000 true, 0.000000, 0.100000, 1.000000, 1.000000 true, 0.000000, 0.200000, 1.000000, 1.000000 true, 0.000000, 0.300000, 1.000000, 1.000000 true, 0.000000, 0.400000, 1.000000, 1.000000 true, 0.000000, 0.500000, 1.000000, 1.000000 true, 0.000000, 0.600000, 1.000000, 1.000000 true, 0.000000, 0.700000, 1.000000, 1.000000 true, 0.000000, 0.800000, 1.000000, 1.000000 true, 0.000000, 0.900000, 1.000000, 1.000000 true, 0.000000, 1.000000, 1.000000, 1.000000 true, 0.100000, 0.000000, 1.000000, 1.000000 true, 0.100000, 0.100000, 0.990000, 0.990000 true, 0.100000, 0.200000, 0.980000, 0.980000 true, 0.100000, 0.300000, 0.970000, 0.970000 true, 0.100000, 0.400000, 0.960000, 0.960000 false, 0.100000, 0.500000, 0.950000, 0.950000 false, 0.100000, 0.600000, 0.940000, 0.940000 true, 0.100000, 0.700000, 0.930000, 0.930000 true, 0.100000, 0.800000, 0.920000, 0.920000 true, 0.100000, 0.900000, 0.910000, 0.910000 true, 0.100000, 1.000000, 0.900000, 0.900000 true, 0.200000, 0.000000, 1.000000, 1.000000 false, 0.200000, 0.100000, 0.980000, 0.980000 false, 0.200000, 0.200000, 0.960000, 0.960000 false, 0.200000, 0.300000, 0.940000, 0.940000 false, 0.200000, 0.400000, 0.920000, 0.920000 true, 0.200000, 0.500000, 0.900000, 0.900000 false, 0.200000, 0.600000, 0.880000, 0.880000 false, 0.200000, 0.700000, 0.860000, 0.860000 false, 0.200000, 0.800000, 0.840000, 0.840000 true, 0.200000, 0.900000, 0.820000, 0.820000 true, 0.200000, 1.000000, 0.800000, 0.800000 true, 0.300000, 0.000000, 1.000000, 1.000000 true, 0.300000, 0.100000, 0.970000, 0.970000 true, 0.300000, 0.200000, 0.940000, 0.940000 true, 0.300000, 0.300000, 0.910000, 0.910000 true, 0.300000, 0.400000, 0.880000, 0.880000 true, 0.300000, 0.500000, 0.850000, 0.850000 true, 0.300000, 0.600000, 0.820000, 0.820000 true, 0.300000, 0.700000, 0.790000, 0.790000 true, 0.300000, 0.800000, 0.760000, 0.760000 true, 0.300000, 0.900000, 0.730000, 0.730000 true, 0.300000, 1.000000, 0.700000, 0.700000 true, 0.400000, 0.000000, 1.000000, 1.000000 true, 0.400000, 0.100000, 0.960000, 0.960000 false, 0.400000, 0.200000, 0.920000, 0.920000 false, 0.400000, 0.300000, 0.880000, 0.880000 true, 0.400000, 0.400000, 0.840000, 0.840000 true, 0.400000, 0.500000, 0.800000, 0.800000 true, 0.400000, 0.600000, 0.760000, 0.760000 true, 0.400000, 0.700000, 0.720000, 0.720000 false, 0.400000, 0.800000, 0.680000, 0.680000 true, 0.400000, 0.900000, 0.640000, 0.640000 false, 0.400000, 1.000000, 0.600000, 0.600000 true, 0.500000, 0.000000, 1.000000, 1.000000 true, 0.500000, 0.100000, 0.950000, 0.950000 true, 0.500000, 0.200000, 0.900000, 0.900000 true, 0.500000, 0.300000, 0.850000, 0.850000 true, 0.500000, 0.400000, 0.800000, 0.800000 true, 0.500000, 0.500000, 0.750000, 0.750000 true, 0.500000, 0.600000, 0.700000, 0.700000 true, 0.500000, 0.700000, 0.650000, 0.650000 true, 0.500000, 0.800000, 0.600000, 0.600000 true, 0.500000, 0.900000, 0.550000, 0.550000 true, 0.500000, 1.000000, 0.500000, 0.500000 true, 0.600000, 0.000000, 1.000000, 1.000000 false, 0.600000, 0.100000, 0.940000, 0.940000 true, 0.600000, 0.200000, 0.880000, 0.880000 false, 0.600000, 0.300000, 0.820000, 0.820000 true, 0.600000, 0.400000, 0.760000, 0.760000 true, 0.600000, 0.500000, 0.700000, 0.700000 true, 0.600000, 0.600000, 0.640000, 0.640000 true, 0.600000, 0.700000, 0.580000, 0.580000 true, 0.600000, 0.800000, 0.520000, 0.520000 true, 0.600000, 0.900000, 0.460000, 0.460000 false, 0.600000, 1.000000, 0.400000, 0.400000 true, 0.700000, 0.000000, 1.000000, 1.000000 true, 0.700000, 0.100000, 0.930000, 0.930000 true, 0.700000, 0.200000, 0.860000, 0.860000 true, 0.700000, 0.300000, 0.790000, 0.790000 true, 0.700000, 0.400000, 0.720000, 0.720000 true, 0.700000, 0.500000, 0.650000, 0.650000 true, 0.700000, 0.600000, 0.580000, 0.580000 true, 0.700000, 0.700000, 0.510000, 0.510000 true, 0.700000, 0.800000, 0.440000, 0.440000 true, 0.700000, 0.900000, 0.370000, 0.370000 false, 0.700000, 1.000000, 0.300000, 0.300000 true, 0.800000, 0.000000, 1.000000, 1.000000 true, 0.800000, 0.100000, 0.920000, 0.920000 false, 0.800000, 0.200000, 0.840000, 0.840000 true, 0.800000, 0.300000, 0.760000, 0.760000 true, 0.800000, 0.400000, 0.680000, 0.680000 true, 0.800000, 0.500000, 0.600000, 0.600000 true, 0.800000, 0.600000, 0.520000, 0.520000 true, 0.800000, 0.700000, 0.440000, 0.440000 true, 0.800000, 0.800000, 0.360000, 0.360000 true, 0.800000, 0.900000, 0.280000, 0.280000 false, 0.800000, 1.000000, 0.200000, 0.200000 true, 0.900000, 0.000000, 1.000000, 1.000000 true, 0.900000, 0.100000, 0.910000, 0.910000 true, 0.900000, 0.200000, 0.820000, 0.820000 true, 0.900000, 0.300000, 0.730000, 0.730000 true, 0.900000, 0.400000, 0.640000, 0.640000 true, 0.900000, 0.500000, 0.550000, 0.550000 true, 0.900000, 0.600000, 0.460000, 0.460000 true, 0.900000, 0.700000, 0.370000, 0.370000 true, 0.900000, 0.800000, 0.280000, 0.280000 true, 0.900000, 0.900000, 0.190000, 0.190000 false, 0.900000, 1.000000, 0.100000, 0.100000 true, 1.000000, 0.000000, 1.000000, 1.000000 true, 1.000000, 0.100000, 0.900000, 0.900000 true, 1.000000, 0.200000, 0.800000, 0.800000 true, 1.000000, 0.300000, 0.700000, 0.700000 false, 1.000000, 0.400000, 0.600000, 0.600000 true, 1.000000, 0.500000, 0.500000, 0.500000 false, 1.000000, 0.600000, 0.400000, 0.400000 false, 1.000000, 0.700000, 0.300000, 0.300000 false, 1.000000, 0.800000, 0.200000, 0.200000 false, 1.000000, 0.900000, 0.100000, 0.100000 true, 1.000000, 1.000000, 0.000000, 0.000000 $
@kagamihr @mkamimura いえ、perl6は 整数/整数 を行なうと有理数型が作られるようです。加減乗除の範囲内では誤差なしで計算できそうです。(長い計算だと計算時間が大変なことになりそうですが。)
— kashi (@mkashi) 2017年4月28日
ということで、$p / 10 ではなく、10.0 で割ったり、1.0 を掛けたりしてみる。
コード(Emacs)
perl 6
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- sub f($p, $q) { (1 - $p) + $p * (1 - $q); } sub g($p, $q) { 1 - $p * $q; } say '==, p, q, (1 - p) + p * (1 - q), 1 - p * q'; for 0..9 -> $p { for 0..9 -> $q { my $p0 = $p / 10.0; my $q0 = $q / 10.0; my $a1 = f($p0, $q0); my $a2 = g($p0, $q0); say $a1 == $a2, ", $p0, $q0, $a1, $a2"; } } for 0..9 -> $p { for 0..9 -> $q { my $p0 = $p / 10 * 1.0; my $q0 = $q / 10 * 1.0; my $a1 = f($p0, $q0); my $a2 = g($p0, $q0); say $a1 == $a2, ", $p0, $q0, $a1, $a2"; } }
入出力結果(Terminal, REPL)
$ ./sample2.pl6 ==, p, q, (1 - p) + p * (1 - q), 1 - p * q True, 0, 0, 1, 1 True, 0, 0.1, 1, 1 True, 0, 0.2, 1, 1 True, 0, 0.3, 1, 1 True, 0, 0.4, 1, 1 True, 0, 0.5, 1, 1 True, 0, 0.6, 1, 1 True, 0, 0.7, 1, 1 True, 0, 0.8, 1, 1 True, 0, 0.9, 1, 1 True, 0.1, 0, 1, 1 True, 0.1, 0.1, 0.99, 0.99 True, 0.1, 0.2, 0.98, 0.98 True, 0.1, 0.3, 0.97, 0.97 True, 0.1, 0.4, 0.96, 0.96 True, 0.1, 0.5, 0.95, 0.95 True, 0.1, 0.6, 0.94, 0.94 True, 0.1, 0.7, 0.93, 0.93 True, 0.1, 0.8, 0.92, 0.92 True, 0.1, 0.9, 0.91, 0.91 True, 0.2, 0, 1, 1 True, 0.2, 0.1, 0.98, 0.98 True, 0.2, 0.2, 0.96, 0.96 True, 0.2, 0.3, 0.94, 0.94 True, 0.2, 0.4, 0.92, 0.92 True, 0.2, 0.5, 0.9, 0.9 True, 0.2, 0.6, 0.88, 0.88 True, 0.2, 0.7, 0.86, 0.86 True, 0.2, 0.8, 0.84, 0.84 True, 0.2, 0.9, 0.82, 0.82 True, 0.3, 0, 1, 1 True, 0.3, 0.1, 0.97, 0.97 True, 0.3, 0.2, 0.94, 0.94 True, 0.3, 0.3, 0.91, 0.91 True, 0.3, 0.4, 0.88, 0.88 True, 0.3, 0.5, 0.85, 0.85 True, 0.3, 0.6, 0.82, 0.82 True, 0.3, 0.7, 0.79, 0.79 True, 0.3, 0.8, 0.76, 0.76 True, 0.3, 0.9, 0.73, 0.73 True, 0.4, 0, 1, 1 True, 0.4, 0.1, 0.96, 0.96 True, 0.4, 0.2, 0.92, 0.92 True, 0.4, 0.3, 0.88, 0.88 True, 0.4, 0.4, 0.84, 0.84 True, 0.4, 0.5, 0.8, 0.8 True, 0.4, 0.6, 0.76, 0.76 True, 0.4, 0.7, 0.72, 0.72 True, 0.4, 0.8, 0.68, 0.68 True, 0.4, 0.9, 0.64, 0.64 True, 0.5, 0, 1, 1 True, 0.5, 0.1, 0.95, 0.95 True, 0.5, 0.2, 0.9, 0.9 True, 0.5, 0.3, 0.85, 0.85 True, 0.5, 0.4, 0.8, 0.8 True, 0.5, 0.5, 0.75, 0.75 True, 0.5, 0.6, 0.7, 0.7 True, 0.5, 0.7, 0.65, 0.65 True, 0.5, 0.8, 0.6, 0.6 True, 0.5, 0.9, 0.55, 0.55 True, 0.6, 0, 1, 1 True, 0.6, 0.1, 0.94, 0.94 True, 0.6, 0.2, 0.88, 0.88 True, 0.6, 0.3, 0.82, 0.82 True, 0.6, 0.4, 0.76, 0.76 True, 0.6, 0.5, 0.7, 0.7 True, 0.6, 0.6, 0.64, 0.64 True, 0.6, 0.7, 0.58, 0.58 True, 0.6, 0.8, 0.52, 0.52 True, 0.6, 0.9, 0.46, 0.46 True, 0.7, 0, 1, 1 True, 0.7, 0.1, 0.93, 0.93 True, 0.7, 0.2, 0.86, 0.86 True, 0.7, 0.3, 0.79, 0.79 True, 0.7, 0.4, 0.72, 0.72 True, 0.7, 0.5, 0.65, 0.65 True, 0.7, 0.6, 0.58, 0.58 True, 0.7, 0.7, 0.51, 0.51 True, 0.7, 0.8, 0.44, 0.44 True, 0.7, 0.9, 0.37, 0.37 True, 0.8, 0, 1, 1 True, 0.8, 0.1, 0.92, 0.92 True, 0.8, 0.2, 0.84, 0.84 True, 0.8, 0.3, 0.76, 0.76 True, 0.8, 0.4, 0.68, 0.68 True, 0.8, 0.5, 0.6, 0.6 True, 0.8, 0.6, 0.52, 0.52 True, 0.8, 0.7, 0.44, 0.44 True, 0.8, 0.8, 0.36, 0.36 True, 0.8, 0.9, 0.28, 0.28 True, 0.9, 0, 1, 1 True, 0.9, 0.1, 0.91, 0.91 True, 0.9, 0.2, 0.82, 0.82 True, 0.9, 0.3, 0.73, 0.73 True, 0.9, 0.4, 0.64, 0.64 True, 0.9, 0.5, 0.55, 0.55 True, 0.9, 0.6, 0.46, 0.46 True, 0.9, 0.7, 0.37, 0.37 True, 0.9, 0.8, 0.28, 0.28 True, 0.9, 0.9, 0.19, 0.19 True, 0, 0, 1, 1 True, 0, 0.1, 1, 1 True, 0, 0.2, 1, 1 True, 0, 0.3, 1, 1 True, 0, 0.4, 1, 1 True, 0, 0.5, 1, 1 True, 0, 0.6, 1, 1 True, 0, 0.7, 1, 1 True, 0, 0.8, 1, 1 True, 0, 0.9, 1, 1 True, 0.1, 0, 1, 1 True, 0.1, 0.1, 0.99, 0.99 True, 0.1, 0.2, 0.98, 0.98 True, 0.1, 0.3, 0.97, 0.97 True, 0.1, 0.4, 0.96, 0.96 True, 0.1, 0.5, 0.95, 0.95 True, 0.1, 0.6, 0.94, 0.94 True, 0.1, 0.7, 0.93, 0.93 True, 0.1, 0.8, 0.92, 0.92 True, 0.1, 0.9, 0.91, 0.91 True, 0.2, 0, 1, 1 True, 0.2, 0.1, 0.98, 0.98 True, 0.2, 0.2, 0.96, 0.96 True, 0.2, 0.3, 0.94, 0.94 True, 0.2, 0.4, 0.92, 0.92 True, 0.2, 0.5, 0.9, 0.9 True, 0.2, 0.6, 0.88, 0.88 True, 0.2, 0.7, 0.86, 0.86 True, 0.2, 0.8, 0.84, 0.84 True, 0.2, 0.9, 0.82, 0.82 True, 0.3, 0, 1, 1 True, 0.3, 0.1, 0.97, 0.97 True, 0.3, 0.2, 0.94, 0.94 True, 0.3, 0.3, 0.91, 0.91 True, 0.3, 0.4, 0.88, 0.88 True, 0.3, 0.5, 0.85, 0.85 True, 0.3, 0.6, 0.82, 0.82 True, 0.3, 0.7, 0.79, 0.79 True, 0.3, 0.8, 0.76, 0.76 True, 0.3, 0.9, 0.73, 0.73 True, 0.4, 0, 1, 1 True, 0.4, 0.1, 0.96, 0.96 True, 0.4, 0.2, 0.92, 0.92 True, 0.4, 0.3, 0.88, 0.88 True, 0.4, 0.4, 0.84, 0.84 True, 0.4, 0.5, 0.8, 0.8 True, 0.4, 0.6, 0.76, 0.76 True, 0.4, 0.7, 0.72, 0.72 True, 0.4, 0.8, 0.68, 0.68 True, 0.4, 0.9, 0.64, 0.64 True, 0.5, 0, 1, 1 True, 0.5, 0.1, 0.95, 0.95 True, 0.5, 0.2, 0.9, 0.9 True, 0.5, 0.3, 0.85, 0.85 True, 0.5, 0.4, 0.8, 0.8 True, 0.5, 0.5, 0.75, 0.75 True, 0.5, 0.6, 0.7, 0.7 True, 0.5, 0.7, 0.65, 0.65 True, 0.5, 0.8, 0.6, 0.6 True, 0.5, 0.9, 0.55, 0.55 True, 0.6, 0, 1, 1 True, 0.6, 0.1, 0.94, 0.94 True, 0.6, 0.2, 0.88, 0.88 True, 0.6, 0.3, 0.82, 0.82 True, 0.6, 0.4, 0.76, 0.76 True, 0.6, 0.5, 0.7, 0.7 True, 0.6, 0.6, 0.64, 0.64 True, 0.6, 0.7, 0.58, 0.58 True, 0.6, 0.8, 0.52, 0.52 True, 0.6, 0.9, 0.46, 0.46 True, 0.7, 0, 1, 1 True, 0.7, 0.1, 0.93, 0.93 True, 0.7, 0.2, 0.86, 0.86 True, 0.7, 0.3, 0.79, 0.79 True, 0.7, 0.4, 0.72, 0.72 True, 0.7, 0.5, 0.65, 0.65 True, 0.7, 0.6, 0.58, 0.58 True, 0.7, 0.7, 0.51, 0.51 True, 0.7, 0.8, 0.44, 0.44 True, 0.7, 0.9, 0.37, 0.37 True, 0.8, 0, 1, 1 True, 0.8, 0.1, 0.92, 0.92 True, 0.8, 0.2, 0.84, 0.84 True, 0.8, 0.3, 0.76, 0.76 True, 0.8, 0.4, 0.68, 0.68 True, 0.8, 0.5, 0.6, 0.6 True, 0.8, 0.6, 0.52, 0.52 True, 0.8, 0.7, 0.44, 0.44 True, 0.8, 0.8, 0.36, 0.36 True, 0.8, 0.9, 0.28, 0.28 True, 0.9, 0, 1, 1 True, 0.9, 0.1, 0.91, 0.91 True, 0.9, 0.2, 0.82, 0.82 True, 0.9, 0.3, 0.73, 0.73 True, 0.9, 0.4, 0.64, 0.64 True, 0.9, 0.5, 0.55, 0.55 True, 0.9, 0.6, 0.46, 0.46 True, 0.9, 0.7, 0.37, 0.37 True, 0.9, 0.8, 0.28, 0.28 True, 0.9, 0.9, 0.19, 0.19 $ perl6 You may want to `zef install Readline` or `zef install Linenoise` or use rlwrap for a line editor To exit type 'exit' or '^D' > (1).WHAT (Int) > (1.0).WHAT (Rat) > (pi).WHAT (Num) > (sqrt 2).WHAT (Num) > (1.234567890234567890234567890234567890234567890234567890234567890234567890234567890234567890234567890).WHAT (Rat) > exit $
Perl 6 は他の言語と違って、1が正確な整数、1.0が丸め誤差が発生する可能性のある実数というわけではなく、1.0を正確な有理数として扱ってくれるみたい。ということで、表記可能(計算結果ではなく直接入力可能)な実数(1.2とか)は全て正確な有理数として扱ってくれるみたい。
0 コメント:
コメントを投稿