気になって調べてみました。
www.textfile.org - 0の0乗 経由Radium Software Development
"a0 = 1" から類推すると 1 のように思えるが, "0n = 0" から類推すると 0 のようにも思えてくる。本当の答はどちらだろう?
Javascript
Math.pow(
,
)
Perl
% perl -le 'print 0**0' 1
複素数にしても駄目。
#!/usr/bin/env perl -l use strict; use Math::Complex; my $z0 = cplx(0,0); print "$z0 ** $z0 == ", $z0**$z0;
%perl cpow.pl 0 ** 0 == 1
Ruby
% ruby -e 'p 0**0' 1
#!/usr/bin/env ruby include Math require 'complex' z0 = Complex(0,0); p z0**z0 puts z0**z0
% ruby cpow.rb Complex(1, 0) 1+0i
Python
% python -c 'print 0**0' 1
C
厳密にはlibm。C++は省略。
#include <stdio.h> #include <stdlib.h> #include <math.h> int main(int argc, char **argv){ double x = argc > 1 ? atof(argv[1]) : 0.0; double y = argc > 2 ? atof(argv[2]) : 0.0; return printf("pow(%f, %f) == %f\n",x, y, pow(x,y)); }
% gcc -Wall -W -lm -o pow pow.c % ./pow pow(0.000000, 0.000000) == 1.000000
scheme
実装はgauche
% gosh gosh> (expt 0 0) 1
Haskell
% ghci ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base ... linking ... done. Prelude> 0^0 1 Prelude> 0**0 1.0
bc
% bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 0^0 1
こうなってくると、累乗をサポートしていてかつ0**0!=1な言語が存在しないかとても気になる。もちろんそういうクラスやプロトタイプで実装するのはたいていの言語では簡単にできるのではあるのだけど。
Dan the Nullingual Powered by Null
追記:
Radium Software Development例えば C99 (ISO/IEC 9899;1999) の math.h に含まれる pow 関数は,底と指数の両方に 0 が与えられた場合,ドメインエラーを返すと定義されている [C99] 。
これ、「ドメインエラーを返す場合もある」ですね。原文は以下のとおり。
p.229The pow functions compute x raised to the powery. A domain error occurs if x is finite and negative and y is finite and not an integer value. A range error may occur. A domain error may occur if x is zero and y is zero. A domain error or range error may occur if x is zero andyis less than zero.
で、GCCではこんな感じです。
man pow (On OS X, gcc 4.01)pow(x, +-0) returns 1 for any x, even a NaN.man pow (On FreeBSD 6-Stable, gcc 3.4.4)
The function pow(x, 0) returns x**0 = 1 for all x including x = 0, infin- ity, and NaN . Previous implementations of pow may have defined x**0 to be undefined in some or all of these cases. Here are reasons for return- ing x**0 = 1 always: 1. Any program that already tests whether x is zero (or infinite or NaN) before computing x**0 cannot care whether 0**0 = 1 or not. Any program that depends upon 0**0 to be invalid is dubious any- way since that expression's meaning and, if invalid, its conse- quences vary from one computer system to another. 2. Some Algebra texts (e.g. Sigler's) define x**0 = 1 for all x, including x = 0. This is compatible with the convention that accepts a[0] as the value of polynomial p(x) = a[0]*x**0 + a[1]*x**1 + a[2]*x**2 +...+ a[n]*x**n at x = 0 rather than reject a[0]*0**0 as invalid. 3. Analysts will accept 0**0 = 1 despite that x**y can approach any- thing or nothing as x and y approach 0 independently. The reason for setting 0**0 = 1 anyway is this: If x(z) and y(z) are any functions analytic (expandable in power series) in z around z = 0, and if there x(0) = y(0) = 0, then x(z)**y(z) -> 1 as z -> 0. 4. If 0**0 = 1, then infinity**0 = 1/0**0 = 1 too; and then NaN**0 = 1 too because x**0 = 1 for all finite and infinite x, i.e., inde- pendently of x.
このブログにコメントするにはログインが必要です。
さんログアウト
この記事には許可ユーザしかコメントができません。