VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

"^" - 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) 2015, HerongYang.com, All Rights Reserved.
-->
<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.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

Arithmetic Operations

 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

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"^" - Arithmetic Exponentiation Operation - Updated in 2015, by Dr. Herong Yang