Zero to the zero power

From Rosetta Code
Revision as of 06:03, 19 June 2014 by Sonia (talk | contribs) (→‎{{header|Go}}: cmplx.Pow updated in Go 1.3 (released today))
Task
Zero to the zero power
You are encouraged to solve this task according to the task description, using any language you may know.

Some programming languages are not exactly consistent (with other programming languages) when raising zero to the zeroth power: .

Task requirements

Show the results of raising zero to the zeroth power.

If your computer language objects to 0**0 at compile time, you may also try something like: <lang rexx>x = 0 y = 0 z = x**y ··· show the result ···</lang> Of course, use any symbols or notation that is supported in your computer language for exponentiation.

See also

Ada

<lang Ada>with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Long_Integer_Text_IO,

 Ada.Long_Long_Integer_Text_IO, Ada.Float_Text_IO, Ada.Long_Float_Text_IO,
 Ada.Long_Long_Float_Text_IO;

use Ada.Text_IO, Ada.Integer_Text_IO, Ada.Long_Integer_Text_IO,

 Ada.Long_Long_Integer_Text_IO, Ada.Float_Text_IO, Ada.Long_Float_Text_IO,
 Ada.Long_Long_Float_Text_IO;

procedure Test5 is

  I    : Integer           := 0;
  LI   : Long_Integer      := 0;
  LLI  : Long_Long_Integer := 0;
  F    : Float             := 0.0;
  LF   : Long_Float        := 0.0;
  LLF  : Long_Long_Float   := 0.0;
  Zero : Natural           := 0;

begin

  Put ("Integer           0^0 = "); 
  Put (I ** Zero, 2);   New_Line;
  Put ("Long Integer      0^0 = ");
  Put (LI ** Zero, 2);  New_Line;
  Put ("Long Long Integer 0^0 = ");
  Put (LLI ** Zero, 2); New_Line;
  Put ("Float           0.0^0 = ");           
  Put (F ** Zero);   New_Line;
  Put ("Long Float      0.0^0 = ");      
  Put (LF ** Zero);  New_Line;
  Put ("Long Long Float 0.0^0 = "); 
  Put (LLF ** Zero); New_Line;

end Test5; </lang>

Output:
Integer           0^0 =  1
Long Integer      0^0 =  1
Long Long Integer 0^0 =  1
Float           0.0^0 =  1.00000E+00
Long Float      0.0^0 =  1.00000000000000E+00
Long Long Float 0.0^0 =  1.00000000000000000E+00

ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.6.win32

<lang algol68>print( ( 0 ^ 0, newline ) ) </lang>

Output:
         +1

Applesoft BASIC

]? 0^0
1

AWK

<lang AWK>

  1. syntax: GAWK -f ZERO_TO_THE_ZERO_POWER.AWK

BEGIN {

   print(0 ^ 0)
   exit(0)

} </lang>

output:

1

Bc

0 ^ 0
1

Bracmat

<lang bracmat>0^0</lang> Output:

1

C

Works with: C99

This example uses the standard pow function in the math library. 0^0 is given as 1. <lang c>#include <stdio.h>

  1. include <math.h>
  2. include <complex.h>

int main() { printf("0 ^ 0 = %f\n", pow(0,0));

       double complex c = cpow(0,0);

printf("0+0i ^ 0+0i = %f+%fi\n", creal(c), cimag(c)); return 0; }</lang>

Output:
0 ^ 0 = 1.000000
0+0i ^ 0+0i = nan+nani

C++

<lang cpp>#include <iostream>

  1. include <cmath>
  2. include <complex>

int main() {

 std::cout << "0 ^ 0 = " << std::pow(0,0) << std::endl;
 std::cout << "0+0i ^ 0+0i = " <<
   std::pow(std::complex<double>(0),std::complex<double>(0)) << std::endl;
 return 0;

}</lang>

Output:
0 ^ 0 = 1
0+0i ^ 0+0i = (nan,nan)

Common Lisp

> (expt 0 0)
1

D

<lang d>void main() {

   import std.stdio, std.math, std.bigint, std.complex;
   writeln("Int:     ", 0 ^^ 0);
   writeln("Ulong:   ", 0UL ^^ 0UL);
   writeln("Float:   ", 0.0f ^^ 0.0f);
   writeln("Double:  ", 0.0 ^^ 0.0);
   writeln("Real:    ", 0.0L ^^ 0.0L);
   writeln("pow:     ", pow(0, 0));
   writeln("BigInt:  ", 0.BigInt ^^ 0);
   writeln("Complex: ", complex(0.0, 0.0) ^^ 0);

}</lang>

Output:
Int:     1
Ulong:   1
Float:   1
Double:  1
Real:    1
pow:     1
BigInt:  1
Complex: 1+0i

Go

Go does not have an exponentiation operator but has functions in the standard library for three types, float64, complex128, and big.Int. As of Go 1.3, all are documented to return 1. <lang go>package main

import (

   "fmt"
   "math"
   "math/big"
   "math/cmplx"

)

func main() {

   fmt.Println("float64:    ", math.Pow(0, 0))
   var b big.Int
   fmt.Println("big integer:", b.Exp(&b, &b, nil))
   fmt.Println("complex:    ", cmplx.Pow(0, 0))

}</lang>

Output:
float64:     1
big integer: 1
complex:     (1+0i)

Haskell

<lang haskell>import Data.Complex

main = do

 print $ 0 ^ 0
 print $ 0.0 ^ 0
 print $ 0 ^^ 0
 print $ 0 ** 0
 print $ (0 :+ 0) ^ 0
 print $ (0 :+ 0) ** (0 :+ 0)</lang>
Output:
1
1.0
1.0
1.0
1.0 :+ 0.0
NaN :+ NaN

Icon and Unicon

"Works" in both languages: <lang unicon>procedure main()

   write(0^0)

end</lang>

Output:

->z2z

Run-time error 204
File z2z.icn; Line 2
real overflow, underflow, or division by zero
Traceback:
   main()
   {0 ^ 0} from line 2 in z2z.icn
->

J

<lang j> 0 ^ 0 1</lang>

Java

<lang java>System.out.println(Math.pow(0, 0));</lang>

Output:
1.0

JavaScript

Works with: Node.js

In interactive mode: <lang javascript>> Math.pow(0, 0); 1</lang>


Mathematica

<lang Mathematica>0^0</lang>

Output:
Indeterminate

OCaml

In the interpreter:

# 0.0 ** 0.0;;
- : float = 1.
# Complex.pow Complex.zero Complex.zero;;
- : Complex.t = {Complex.re = nan; Complex.im = nan}
# #load "nums.cma";;
# open Num;;
# Int 0 **/ Int 0;;                 
- : Num.num = Int 1

ooRexx

<lang oorexx>/**********************************************************************

  • 21.04.2014 Walter Pachl
                                                                                                                                            • /

Say 'rxCalcpower(0,0) ->' rxCalcpower(0,0) Say '0**0 ->' 0**0

requires rxmath library</lang>
Output:
rxCalcpower(0,0)  -> 1
0**0              -> 1 

PARI/GP

<lang parigp>0^0</lang>

Output:
%1 = 1

Perl

<lang perl>print 0 ** 0, "\n";

use Math::Complex;

print cplx(0,0) ** cplx(0,0), "\n";</lang>

Output:
1
1

Perl 6

Translation of REXX.

<lang perl 6>say '0 ** 0 (zero to the zeroth power) ───► ', 0**0</lang>

Output:
0 ** 0 (zero to the zeroth power) ───► 1

PHP

<lang PHP><?php echo pow(0,0);</lang>

Output:
1

PL/I

<lang pli> zhz: Proc Options(Main);

Dcl a dec float(10) Init(1);
Dcl b dec float(10) Init(0);
Put skip list('1**0=',a**b);
Put skip list('0**1=',b**a);
Put skip list('0**0=',b**b);
End;</lang>
Output:
1**0=                    1.000000000E+0000
0**1=                    0.000000000E+0000
0**0=
IBM0682I  ONCODE=1553  X in EXPONENT(X) was invalid.
   At offset +0000025B in procedure with entry ZHZ   

PowerShell

<lang PowerShell>[math]::pow(0,0)</lang>

Python

<lang python>from decimal import Decimal from fractions import Fraction for n in (Decimal(0), Fraction(0, 1), complex(0), float(0), int(0)): try: n1 = n**n except: n1 = '<Raised exception>' try: n2 = pow(n, n) except: n2 = '<Raised exception>' print('%8s: ** -> %r; pow -> %r' % (n.__class__.__name__, n1, n2))</lang>

Output:
 Decimal: ** -> '<Raised exception>'; pow -> '<Raised exception>'
Fraction: ** -> Fraction(1, 1); pow -> Fraction(1, 1)
 complex: ** -> (1+0j); pow -> (1+0j)
   float: ** -> 1.0; pow -> 1.0
     int: ** -> 1; pow -> 1

Racket

<lang racket>#lang racket

as many zeros as I can think of...

(define zeros (list

              0  ; unspecified number type
              0. ; hinted as float
              #e0 ; explicitly exact
              #i0 ; explicitly inexact
              0+0i ; exact complex
              0.+0.i ; float inexact
              ))

(for*((z zeros) (p zeros))

 (printf "(~a)^(~a) = ~s~%" z p
 (with-handlers [(exn:fail:contract:divide-by-zero? exn-message)]
   (expt z p))))</lang>
Output:
(0)^(0) = 1
(0)^(0.0) = 1.0
(0)^(0) = 1
(0)^(0.0) = 1.0
(0)^(0) = 1
(0)^(0.0+0.0i) = "expt: undefined for 0 and 0.0+0.0i"
(0.0)^(0) = 1
(0.0)^(0.0) = 1.0
(0.0)^(0) = 1
(0.0)^(0.0) = 1.0
(0.0)^(0) = 1
(0.0)^(0.0+0.0i) = +nan.0+nan.0i
(0)^(0) = 1
(0)^(0.0) = 1.0
(0)^(0) = 1
(0)^(0.0) = 1.0
(0)^(0) = 1
(0)^(0.0+0.0i) = "expt: undefined for 0 and 0.0+0.0i"
(0.0)^(0) = 1
(0.0)^(0.0) = 1.0
(0.0)^(0) = 1
(0.0)^(0.0) = 1.0
(0.0)^(0) = 1
(0.0)^(0.0+0.0i) = +nan.0+nan.0i
(0)^(0) = 1
(0)^(0.0) = 1.0
(0)^(0) = 1
(0)^(0.0) = 1.0
(0)^(0) = 1
(0)^(0.0+0.0i) = "expt: undefined for 0 and 0.0+0.0i"
(0.0+0.0i)^(0) = 1
(0.0+0.0i)^(0.0) = 1.0+0.0i
(0.0+0.0i)^(0) = 1
(0.0+0.0i)^(0.0) = 1.0+0.0i
(0.0+0.0i)^(0) = 1
(0.0+0.0i)^(0.0+0.0i) = +nan.0+nan.0i

REXX

<lang rexx>/*REXX program shows the results of raising zero to the zeroth power.*/ say '0 ** 0 (zero to the zeroth power) ───► ' 0**0</lang>
using PC/REXX
using Personal REXX
using REGINA output

0 ** 0  (zero to the zeroth power) ───►  1

using R4
output

Error 26 : Invalid whole number (SYNTAX)
Information: 0 ** 0 is undefined
Error occurred in statement# 2
Statement source: say '0 ** 0  (zero to the zeroth power) ───► ' 0**0
Statement context: C:\ZERO_TO0.REX, procedure: ZERO_TO0

using ROO
output

Error 26 : Invalid whole number (SYNTAX)
Information: 0 ** 0 is undefined
Error occurred in statement# 2
Statement source: say '0 ** 0  (zero to the zeroth power) ───► ' 0**0
Statement context: C:\ZERO_TO0.REX, procedure: ZERO_TO0

Ruby

<lang ruby>require 'complex' require 'rational' require 'bigdecimal'

[0, 0.0, Complex(0), Rational(0), BigDecimal.new("0")].each {|n|

 printf "%10s: ** -> %s\n" % [n.class, n**n]

}</lang>

Output:
    Fixnum: ** -> 1
     Float: ** -> 1.0
   Complex: ** -> 1+0i
  Rational: ** -> 1/1
BigDecimal: ** -> 0.1E1

Scheme

<lang scheme>(display (expt 0 0)) (newline) (display (expt 0.0 0.0)) (newline) (display (expt 0+0i 0+0i)) (newline)</lang>

Output:
1
1.0
1.0

Standard ML

In the interpreter:

- Math.pow (0.0, 0.0);
val it = 1.0 : real

Tcl

Interactively… <lang tcl>% expr 0**0 1 % expr 0.0**0.0 1.0</lang>

zkl

<lang zkl>(0.0).pow(0) //--> 1.0 var BN=Import("zklBigNum"); // big ints BN(0).pow(0) //--> 1</lang>