VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.00

"^" - Arithmetic Exponentiation Operation

This section provides some detail rules and a tutorial example on how arithmetic exponentiation operation works in VBScript.

Here are some detail rules about the arithmetic operation - exponentiation:

  • Arithmetic exponentiation operation uses the exponentiation operator: "^"
  • Arithmetic exponentiation operation requires that at least one of the operands is a numeric subtype: Byte, Integer, Long, Single or Double.
  • If an operand is not a numeric subtype, it will be converted into a numeric subtype.
  • The Boolean subtype is considered as a numeric subtype in a exponentiation operation. "True" represents a numeric value of -1. "False" represents a numeric value of 0.
  • The Empty subtype is considered as a numeric subtype in a exponentiation operation. "Empty" represents a numeric value of 0.
  • When a String value is used in a exponentiation operation, it will be parsed into a Long value. If the parsing process fails, the exponentiation operation will raise the "Type mismatch" runtime error.
  • If the first operand is a negative value, the second operand must be an integer. Otherwise, the operation will fail.
  • The returning value is the result of the first operand raised to the power of the second operand.
  • The subtype of the returning value is always Double.

Here is a tutorial example showing you how arithmetic modulus operation rules work:

<html>
<body>
<!-- exponentiation_operation.html
   Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
   document.writeln("2^3=" & (2^3) & " : " & TypeName(2^3))
   document.writeln("2^31=" & (2^31) & " : " & TypeName(2^31))
   document.writeln("2^64.5=" & (2^64.5) & " : " & TypeName(2^64.5))
   document.writeln("2^0.5=" & (2^0.5) & " : " & TypeName(2^0.5))
   document.writeln("2^0=" & (2^0) & " : " & TypeName(2^0))
   document.writeln("0^0=" & (0^0) & " : " & TypeName(0^0))
   document.writeln("0^1=" & (0^1) & " : " & TypeName(0^1))
   document.writeln("2^-1=" & (2^-1) & " : " & TypeName(2^-1))
   document.writeln("2^-3=" & (2^-3) & " : " & TypeName(2^-3))
   document.writeln("-2^-3=" & (-2^-3) & " : " & TypeName(-2^-3))
'   document.writeln("-2^0.5=" & (-2^0.5) & " : " & TypeName(-2^0.5))
   document.writeln("3.33^9.99=" & (3.33^9.99) & " : " _ 
      & TypeName(3.33^9.99))
   document.writeln("3^""7""=" & (3^"7") & " : " & TypeName(3^"7"))
'  document.writeln("3^""7Hello""=" & (3^"7Hello") & " : " _
'     & TypeName(3^"7Hello"))
   document.writeln("3^True=" & (3^True) & " : " & TypeName(3^True))
   document.writeln("3^False=" & (3^False) & " : " _
      & TypeName(3^False))
'   document.writeln("False^True=" & (False^True) & " : " _
'      & TypeName(False^True))
   document.writeln("Empty^3=" & (Empty^3) & " : " _ 
      & TypeName(Empty^3))
</script>
</pre>
</body>
</html>

Here is the output of this VBScript example script:

2^3=8 : Double
2^31=2147483648 : Double
2^64.5=2.60876356506656E+19 : Double
2^0.5=1.4142135623731 : Double
2^0=1 : Double
0^0=1 : Double
0^1=0 : Double
2^-1=0.5 : Double
2^-3=0.125 : Double
-2^-3=-0.125 : Double
3.33^9.99=165660.089908899 : Double
3^"7"=2187 : Double
3^True=0.333333333333333 : Double
3^False=1 : Double
Empty^3=0 : Double

The output confirms some of arithmetic exponentiation operation rules:

  • Return values from all tests are Double values .
  • "2^3=8 : Double" shows that the result is a Double value, even if the whole operation could be done in integer mode.
  • "2^0.5=1.4142135623731 : Double" shows an interesting problem. If VBScript is doing the calculation Double mode, The result should have more digits, like 1.4142135623730950488016887242097.
  • The statement on (-2^0.5) is commented out, because the second operand must an integer if first operand is negative.
  • The statement on (False^True) is commented out, because it will be converted to (0^-1), which will result an operation of dividing by 0.

Sections in This Chapter

Introduction of Arithmetic Operations

"+" - Arithmetic Addition Operation

"-" - Arithmetic Subtraction Operation

"*" - Arithmetic Multiplication Operation

"/" - Arithmetic Division Operation

"\" - Arithmetic Integer Division Operation

"Mod" - Arithmetic Modulus Operation

"^" - Arithmetic Exponentiation Operation

Dr. Herong Yang, updated in 2008
"^" - Arithmetic Exponentiation Operation