Getal-variabelen

From Apache OpenOffice Wiki
< NL‎ | Documentation‎ | BASIC Guide
Revision as of 16:10, 20 January 2013 by DiGro (Talk | contribs)

Jump to: navigation, search
Book.png


Apache OpenOffice BASIC ondersteunt vijf basistypen voor het verwerken van getallen:

  • Integer
  • Long Integer
  • Single
  • Double
  • Valuta

Integer-variabelen

Integer-variabelen kunnen elk geheel getal tussen -32768 en 32767 bevatten. Een integer-variabele kan twee bytes geheugen in beslag nemen. Het symbool voor typedeclaratie voor een integer variabele is %. Berekeningen die integer-variabelen gebruiken zijn zeer snel en in het bijzonder handig bij lus-tellingen. Als u een "zwevende komma"-getal toewijst aan een variabele Integer, wordt het getal afgerond naar boven of beneden naar het volgende gehele getal.

Voorbeeld voor declaraties van variabelen Integer:

Dim Variabele As Integer
Dim Variabele%

Long Integer-variabelen

Long integer-variabelen kunnen elk geheel getal tussen –2147483648 en 2147483647 bevatten. Een long integer-variabele kan tot vier bytes geheugen in beslag nemen. Het symbool voor typedeclaratie van een long integer is &. Berekeningen die long integer-variabelen gebruiken zijn zeer snel en in het bijzonder handig bij lus-tellingen. Als u een "zwevende komma"-getal toewijst aan een variabele Long Integer, wordt het getal afgerond naar boven of beneden naar het volgende gehele getal.

Voorbeeld voor declaraties van variabelen Long Integer:

Dim Variabele as Long
Dim Variabele&

Single-variabelen

Single variabelen kunnen elk positief of negatief "zwevende komma"-getal tussen 3.402823 x 1038 en 1.401298 x 10-45 bevatten. Een variabele Single kan tot vier bytes geheugen in beslag nemen. Het symbool voor typedeclaratie van een variabele Single is !.

Oorspronkelijk werden variabelen Single gebruikt om de computertijd te reduceren die vereist was voor de meer precieze variabelen Double. Echter, deze snelheidsoverwegingen zijn niet langer van toepassing, wat de noodzaak voor het gebruik van variabelen Single beperkt.

Voorbeeld voor declaraties van variabelen Single:

Dim Variabele as Single
Dim Variabele!

Double-variabelen

Double-variabelen kunnen elk positief of negatief "zwevende komma"-getal tussen 1.79769313486232 x 10308 en 4.94065645841247 x 10-324 bevatten. Een variabele Double kan tot acht bytes geheugen in beslag nemen. Double-variabelen zijn geschikt voor precieze berekeningen. Het typedeclaratie-symbool is #.

Voorbeeld voor declaraties van variabelen Double:

Dim Variabele As Double
Dim Variabele#

Currency-variabelen

Currency-variabelen verschillen van de andere types variabelen door de manier waarop zij waarden verwerken. Het decimale punt staat vast en wordt gevolgd door vier decimale plaatsen. De variabele kan tot 15 getallen bevatten vóór de decimale punt. Een variabele Currency kan elke waarde bevatten tussen -922337203685477.5808 en +922337203685477.5807 en neemt tot acht bytes geheugen in beslag. Het symbool voor typedeclaratie van een valuta variabele is @.

Currency-variabelen zijn meestal bedoeld voor zakelijke berekeningen die leiden tot onvoorziene afrondingsfouten ten gevolge van het gebruik van "zwevende komma"-getallen

Voorbeeld voor declaraties van variabelen Currency:

Dim Variabele As Currency
Dim Variabele@
Documentation caution.png De afhandeling van het BASIC-type Currency is niet betrouwbaar. Issue 31001 Issue 54049 Issue 91121 Issue 107277 zijn nog steeds niet gecorrigeerd in Apache OpenOffice versie 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