Difference between revisions of "Documentation/BASIC Guide/Numbers"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
(22 intermediate revisions by 6 users not shown)
Line 5: Line 5:
 
|NextPage=Documentation/BASIC Guide/Boolean
 
|NextPage=Documentation/BASIC Guide/Boolean
 
|lang=block
 
|lang=block
}}<!-- {{DISPLAYTITLE:Numbers -->
+
}}
 
+
{{DISPLAYTITLE:Number Variables}}
 
+
__NOTOC__
= Numbers =
+
 
{{OOo}} Basic supports five basic types for processing numbers:
 
{{OOo}} Basic supports five basic types for processing numbers:
  
 
* Integer
 
* Integer
 
* Long Integer
 
* Long Integer
* Float
+
* Single
 
* Double
 
* Double
 
* Currency
 
* Currency
Line 19: Line 18:
 
== Integer Variables ==
 
== Integer Variables ==
  
Integer variables can store any whole number between -32768 and 32767. An integer variable can take up to two bytes of memory. The type declaration symbol for an integer variable is %. Calculations that use integer variables are very fast and are particularly useful for loop counters. If you assign a floating point number to an integer variable, the number is rounded up or down to the next whole number.
+
Integer variables can store any whole number between '''-32768''' and '''32767'''. An integer variable can take up to two bytes of memory. The type declaration symbol for an integer variable is <tt>%</tt>. Calculations that use integer variables are very fast and are particularly useful for loop counters. If you assign a floating point number to an integer variable, the number is rounded up or down to the next whole number.
  
 
Example declarations for integer variables:
 
Example declarations for integer variables:
  
Dim Variable As Integer
+
<source lang="oobas">
Dim Variable%
+
Dim Variable As Integer
 +
Dim Variable%
 +
</source>
  
 
== Long Integer Variables ==
 
== Long Integer Variables ==
  
Long integer variables can store any whole number between –2147483648 and 2147483647. A long integer variable can takes up to four bytes of memory. The type declaration symbol for a long integer is &. Calculations with long integer variables are very fast and are particularly useful for loop counters. If you assign a floating point number to a long integer variable, the number is rounded up or down to the next whole number.
+
Long integer variables can store any whole number between '''–2147483648''' and '''2147483647'''. A long integer variable can takes up to four bytes of memory. The type declaration symbol for a long integer is <tt>&</tt>. Calculations with long integer variables are very fast and are particularly useful for loop counters. If you assign a floating point number to a long integer variable, the number is rounded up or down to the next whole number.
  
 
Example declarations for long integer variables:
 
Example declarations for long integer variables:
  
Dim Variable as Long
+
<source lang="oobas">
Dim Variable&
+
Dim Variable as Long
 +
Dim Variable&
 +
</source>
  
 
== Single Variables ==
 
== Single Variables ==
  
Single variables can store any positive or negative floating point number between 3.402823 x 1038 and 1.401298 x 10-45. A single variable can take up to four bytes of memory. The type declaration symbol for a single variable is !.
+
Single variables can store any positive or negative floating point number between '''3.402823 x 10<sup>38</sup>''' and '''1.401298 x 10<sup>-45</sup>'''. A single variable can take up to four bytes of memory. The type declaration symbol for a single variable is <tt>!</tt>.
  
 
Originally, single variables were used to reduce the computing time required for the more precise double variables. However, these speed considerations no longer apply, reducing the need for single variables.
 
Originally, single variables were used to reduce the computing time required for the more precise double variables. However, these speed considerations no longer apply, reducing the need for single variables.
Line 43: Line 46:
 
Example declarations for single variables:
 
Example declarations for single variables:
  
Dim Variable as Single
+
<source lang="oobas">
Dim Variable!
+
Dim Variable as Single
 +
Dim Variable!
 +
</source>
  
 
== Double Variables ==
 
== Double Variables ==
  
Double variables can store any positive or negative floating point numbers between 1.79769313486232 x 10308 and 4.94065645841247 x 10-324. A double variable can take up to eight bytes of memory. Double variables are suitable for precise calculations. The type declaration symbol is #.
+
Double variables can store any positive or negative floating point numbers between '''1.79769313486232 x 10<sup>308</sup>''' and '''4.94065645841247 x 10<sup>-324</sup>'''. A double variable can take up to eight bytes of memory. Double variables are suitable for precise calculations. The type declaration symbol is <tt>#</tt>.
  
 
Example declarations of double variables:
 
Example declarations of double variables:
  
Dim Variable As Double
+
<source lang="oobas">
Dim Variable#
+
Dim Variable As Double
 +
Dim Variable#
 +
</source>
  
 
== Currency Variables ==
 
== Currency Variables ==
  
Currency variables differ from the other variable types by the way they handle values. The decimal point is fixed and is followed by four decimal places. The variable can contain up to 15 numbers before the decimal point. A currency variable can store any value between —922337203685477.5808 and +922337203685477.5807 and takes up to eight bytes of memory. The type declaration symbol for a currency variable is @.
+
Currency variables differ from the other variable types by the way they handle values. The decimal point is fixed and is followed by four decimal places. The variable can contain up to 15 numbers before the decimal point. A currency variable can store any value between '''-922337203685477.5808''' and '''+922337203685477.5807''' and takes up to eight bytes of memory. The type declaration symbol for a currency variable is <tt>@</tt>.
  
 
Currency variables are mostly intended for business calculations that yield unforeseeable rounding errors due to the use of floating point numbers.
 
Currency variables are mostly intended for business calculations that yield unforeseeable rounding errors due to the use of floating point numbers.
Line 63: Line 70:
 
Example declarations of currency variables:
 
Example declarations of currency variables:
  
Dim Variable As Currency
+
<source lang="oobas">
Dim Variable@
+
Dim Variable As Currency
 +
Dim Variable@
 +
</source>
 +
 
 +
{{Warn|The handling of Basic Currency type is not reliable. {{bug|31001}} {{bug|54049}} {{bug|91121}} {{bug|107277}} are still not corrected on {{OOo}} version 3.1.1. }}
  
 
== Specification of Explicit Numbers ==
 
== Specification of Explicit Numbers ==
Line 74: Line 85:
 
The simplest method is to work with whole numbers. They are listed in the source text without a comma separating the thousand figure:
 
The simplest method is to work with whole numbers. They are listed in the source text without a comma separating the thousand figure:
  
Dim A As Integer
+
<source lang="oobas">
Dim B As Float
+
Dim A As Integer
+
Dim B As Double
A = 1210
+
 
B = 2438
+
A = 1210
 +
B = 2438
 +
</source>
  
 
The numbers can be preceded by both a plus (+) or minus (-) sign (with or without a space in between):
 
The numbers can be preceded by both a plus (+) or minus (-) sign (with or without a space in between):
  
Dim A As Integer
+
<source lang="oobas">
Dim B As Float
+
Dim A As Integer
+
Dim B As Double
A = + 121
+
 
B = - 243
+
A = + 121
 +
B = - 243
 +
</source>
  
 
=== Decimal Numbers ===
 
=== Decimal Numbers ===
Line 92: Line 107:
 
When you type a decimal number, use a period (.) as the decimal point. This rule ensures that source texts can be transferred from one country to another without conversion.
 
When you type a decimal number, use a period (.) as the decimal point. This rule ensures that source texts can be transferred from one country to another without conversion.
  
Dim A As Integer
+
<source lang="oobas">
Dim B As Integer
+
Dim A As Integer
Dim C As Float
+
Dim B As Integer
+
Dim C As Double
A = 1223.53      ' is rounded
+
 
B = - 23446.46  ' is rounded
+
A = 1223.53      ' is rounded
C = + 3532.76323
+
B = - 23446.46  ' is rounded
 +
C = + 3532.76323
 +
</source>
  
 
You can also use plus (+) or minus (-) signs as prefixes for decimal numbers (again with or without spaces).
 
You can also use plus (+) or minus (-) signs as prefixes for decimal numbers (again with or without spaces).
Line 106: Line 123:
 
=== Exponential Writing Style ===
 
=== Exponential Writing Style ===
  
{{OOo}} Basic allows numbers to be specified in the exponential writing style, for example, you can write 1.5e-10 for the number 1.5 x 10-10 (0.00000000015). The letter "e" can be lowercase or uppercase with or without a plus sign (+) as a prefix.
+
{{OOo}} Basic allows numbers to be specified in the exponential writing style, for example, you can write <tt>1.5e-10</tt> for the number 1.5 x 10<sup>-10</sup> (0.00000000015). The letter "e" can be lowercase or uppercase with or without a plus sign (+) as a prefix.
  
 
Here are a few correct and incorrect examples of numbers in exponential format:
 
Here are a few correct and incorrect examples of numbers in exponential format:
  
Dim A As Double
+
<source lang="oobas">
+
Dim A As Double
A = 1.43E2    ' Correct
+
 
A = + 1.43E2  ' Correct (space between plus and basic number)
+
A = 1.43E2    ' Correct
A = - 1.43E2  ' Correct (space between minus and basic number)
+
A = + 1.43E2  ' Correct (space between plus and basic number)
A = 1.43E-2  ' Correct (negative exponent)
+
A = - 1.43E2  ' Correct (space between minus and basic number)
A = 1.43E -2  ' Incorrect (spaces not permitted within the number)
+
A = 1.43E-2  ' Correct (negative exponent)
A = 1,43E-2  ' Incorrect (commas not permitted as decimal points)
+
A = 1.43E -2  ' Incorrect (spaces not permitted within the number)
A = 1.43E2.2  ' Incorrect (exponent must be a whole number)
+
A = 1,43E-2  ' Incorrect (commas not permitted as decimal points)
 +
A = 1.43E2.2  ' Incorrect (exponent must be a whole number)
 +
</source>
  
 
Note, that in the first and third incorrect examples that no error message is generated even though the variables return incorrect values. The expression
 
Note, that in the first and third incorrect examples that no error message is generated even though the variables return incorrect values. The expression
Line 124: Line 143:
 
  A = 1.43E -2
 
  A = 1.43E -2
  
is interpreted as 1.43 minus 2, which corresponds to the value -0.57. However, the value 1.43 * 102 (corresponding to 0.0143) was the intended value. With the value
+
is interpreted as 1.43 minus 2, which corresponds to the value -0.57. However, the value 1.43 x 10<sup>-2</sup> (corresponding to 0.0143) was the intended value. With the value
  
 
  A = 1.43E2.2
 
  A = 1.43E2.2
Line 134: Line 153:
 
=== Hexadecimal Values ===
 
=== Hexadecimal Values ===
  
In the hexadecimal system (base 16 system), a 2-digit number corresponds to precisely one byte. This allows numbers to be handled in a manner which more closely reflects machine architecture. In the hexadecimal system, the numbers 0 to 9 and the letters A to F are used as numbers. An A stands for the decimal number 10, while the letter F represents the decimal number 15. {{OOo}} Basic lets you use whole numbered hexadecimal values, so long as they are preceded by &H.
+
In the hexadecimal system (base 16 system), a 2-digit number corresponds to precisely one byte. This allows numbers to be handled in a manner which more closely reflects machine architecture. In the hexadecimal system, the numbers 0 to 9 and the letters A to F are used as numbers. An A stands for the decimal number 10, while the letter F represents the decimal number 15. {{OOo}} Basic lets you use whole numbered hexadecimal values, so long as they are preceded by <tt>&H</tt>.
  
Dim A As Long
+
<source lang="oobas">
A = &HFF ' Hexadecimal value FF, corresponds to the decimal value 255
+
Dim A As Long
A = &H10 ' Hexadecimal value 10, corresponds to the decimal value 16
+
A = &HFF ' Hexadecimal value FF, corresponds to the decimal value 255
 +
A = &H10 ' Hexadecimal value 10, corresponds to the decimal value 16
 +
</source>
  
 
=== Octal Values ===
 
=== Octal Values ===
  
{{OOo}} Basic also understands the octal system (base 8 system), which uses the numbers 0 to 7. You must use whole numbers that are preceded by &O.
+
{{OOo}} Basic also understands the octal system (base 8 system), which uses the numbers 0 to 7. You must use whole numbers that are preceded by <tt>&O</tt>.
  
Dim A As Long
+
<source lang="oobas">
A = &O77 ' Octal value 77, corresponds to the decimal value 63
+
Dim A As Long
A = &O10 ' Octal value 10, corresponds to the decimal value 8
+
A = &O77 ' Octal value 77, corresponds to the decimal value 63
 +
A = &O10 ' Octal value 10, corresponds to the decimal value 8
 +
</source>
 +
 
 +
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Numbers}}
 +
{{PDL1}}

Revision as of 07:17, 12 July 2018


Apache OpenOffice Basic supports five basic types for processing numbers:

  • Integer
  • Long Integer
  • Single
  • Double
  • Currency

Integer Variables

Integer variables can store any whole number between -32768 and 32767. An integer variable can take up to two bytes of memory. The type declaration symbol for an integer variable is %. Calculations that use integer variables are very fast and are particularly useful for loop counters. If you assign a floating point number to an integer variable, the number is rounded up or down to the next whole number.

Example declarations for integer variables:

Dim Variable As Integer
Dim Variable%

Long Integer Variables

Long integer variables can store any whole number between –2147483648 and 2147483647. A long integer variable can takes up to four bytes of memory. The type declaration symbol for a long integer is &. Calculations with long integer variables are very fast and are particularly useful for loop counters. If you assign a floating point number to a long integer variable, the number is rounded up or down to the next whole number.

Example declarations for long integer variables:

Dim Variable as Long
Dim Variable&

Single Variables

Single variables can store any positive or negative floating point number between 3.402823 x 1038 and 1.401298 x 10-45. A single variable can take up to four bytes of memory. The type declaration symbol for a single variable is !.

Originally, single variables were used to reduce the computing time required for the more precise double variables. However, these speed considerations no longer apply, reducing the need for single variables.

Example declarations for single variables:

Dim Variable as Single
Dim Variable!

Double Variables

Double variables can store any positive or negative floating point numbers between 1.79769313486232 x 10308 and 4.94065645841247 x 10-324. A double variable can take up to eight bytes of memory. Double variables are suitable for precise calculations. The type declaration symbol is #.

Example declarations of double variables:

Dim Variable As Double
Dim Variable#

Currency Variables

Currency variables differ from the other variable types by the way they handle values. The decimal point is fixed and is followed by four decimal places. The variable can contain up to 15 numbers before the decimal point. A currency variable can store any value between -922337203685477.5808 and +922337203685477.5807 and takes up to eight bytes of memory. The type declaration symbol for a currency variable is @.

Currency variables are mostly intended for business calculations that yield unforeseeable rounding errors due to the use of floating point numbers.

Example declarations of currency variables:

Dim Variable As Currency
Dim Variable@
Documentation caution.png The handling of Basic Currency type is not reliable. Issue 31001 Issue 54049 Issue 91121 Issue 107277 are still not corrected on Apache OpenOffice version 3.1.1.

Specification of Explicit Numbers

Numbers can be presented in several ways, for example, in decimal format or in scientific notation, or even with a different base than the decimal system. The following rules apply to numerical characters in Apache OpenOffice Basic:

Whole Numbers

The simplest method is to work with whole numbers. They are listed in the source text without a comma separating the thousand figure:

Dim A As Integer
Dim B As Double
 
A = 1210
B = 2438

The numbers can be preceded by both a plus (+) or minus (-) sign (with or without a space in between):

Dim A As Integer
Dim B As Double
 
A = + 121
B = - 243

Decimal Numbers

When you type a decimal number, use a period (.) as the decimal point. This rule ensures that source texts can be transferred from one country to another without conversion.

Dim A As Integer
Dim B As Integer
Dim C As Double
 
A = 1223.53      ' is rounded
B = - 23446.46   ' is rounded
C = + 3532.76323

You can also use plus (+) or minus (-) signs as prefixes for decimal numbers (again with or without spaces).

If a decimal number is assigned to an integer variable, Apache OpenOffice Basic rounds the figure up or down.

Exponential Writing Style

Apache OpenOffice Basic allows numbers to be specified in the exponential writing style, for example, you can write 1.5e-10 for the number 1.5 x 10-10 (0.00000000015). The letter "e" can be lowercase or uppercase with or without a plus sign (+) as a prefix.

Here are a few correct and incorrect examples of numbers in exponential format:

Dim A As Double
 
A = 1.43E2    ' Correct
A = + 1.43E2  ' Correct (space between plus and basic number)
A = - 1.43E2  ' Correct (space between minus and basic number)
A = 1.43E-2   ' Correct (negative exponent)
A = 1.43E -2  ' Incorrect (spaces not permitted within the number)
A = 1,43E-2   ' Incorrect (commas not permitted as decimal points)
A = 1.43E2.2  ' Incorrect (exponent must be a whole number)

Note, that in the first and third incorrect examples that no error message is generated even though the variables return incorrect values. The expression

A = 1.43E -2

is interpreted as 1.43 minus 2, which corresponds to the value -0.57. However, the value 1.43 x 10-2 (corresponding to 0.0143) was the intended value. With the value

A = 1.43E2.2

Apache OpenOffice Basic ignores the part of the exponent after the decimal point and interprets the expression as

A = 1.43E2

Hexadecimal Values

In the hexadecimal system (base 16 system), a 2-digit number corresponds to precisely one byte. This allows numbers to be handled in a manner which more closely reflects machine architecture. In the hexadecimal system, the numbers 0 to 9 and the letters A to F are used as numbers. An A stands for the decimal number 10, while the letter F represents the decimal number 15. Apache OpenOffice Basic lets you use whole numbered hexadecimal values, so long as they are preceded by &H.

Dim A As Long
A = &HFF ' Hexadecimal value FF, corresponds to the decimal value 255
A = &H10 ' Hexadecimal value 10, corresponds to the decimal value 16

Octal Values

Apache OpenOffice Basic also understands the octal system (base 8 system), which uses the numbers 0 to 7. You must use whole numbers that are preceded by &O.

Dim A As Long
A = &O77 ' Octal value 77, corresponds to the decimal value 63
A = &O10 ' Octal value 10, corresponds to the decimal value 8


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools