Number: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(Added proper number match regex)
(Reworked page & commented out stub content that may confuse users and code related to parseNumber. Also added {{Stub}})
Line 1: Line 1:
=Number Type=
{{SideTOC}}
A real number, i.e. 1, -25, 6.52, 1805.6352
A number is, depending on scope, either a [http://en.wikipedia.org/wiki/Single-precision_floating-point_format single precision floating point number] (when talking about scripting) or a range of numerical types when talking about config context.


Numbers are stored as [http://en.wikipedia.org/wiki/Single-precision_floating-point_format single precision floating point numbers], which allow for a large range of values with limited precision (generally accurate to 6 or 7 decimal places). The number [[Data Types|data type]] is also sometimes referred to as "scalar" (for example, returned by the [[supportInfo]] script command).
==Scripting==
In SQF, there are multiple accepted number formats.
However, all of them will result in the same  {{Inline code|SCALAR}} ([[typeName]]) value type.


The largest real positive number that can be entered via script is:  3.4028235e38
{{Informative|Regex to match all numbers in [[SQF_syntax|SQF]] is {{Inline code|<nowiki>(((\$|0x)[0-9a-fA-F]+)|(\.[0-9]+))|(\b[0-9]+(\.[0-9]+|[eE][-+]?[0-9]+)?)\b</nowiki>}}}}


The largest real negative number that can be entered via script is: -3.4028235e38
The '''<span style="color: #008000;">largest positive</span>''' number that can be archived is {{Inline code|3.4028235e38}} and the '''<span style="color: #800000;">largest negative</span>''' is {{Inline code|-3.4028235e38}}.
However, it also is possible to generate positive or negative infinite values using either {{Inline code|1e39}} (string representation: {{Inline code|1.#INF}}) or {{Inline code|-1e39}} (string representation: {{Inline code|-1.#INF}}).


Larger numbers: In scripts it is possible to generate a representation of an infinite positive or negative number which compares even larger or smaller than the above two floating point limits;
To check, if a number is finite, one can use the [[finite]] operator.


Positive infinity  1e39  = "1.#INF"
===Decimal (Base 10)===
A decimal number is your normal {{Inline code|0.5}} syntax stuff with one extra: You may ommit the initial pack of digits.


Negative infinity -1e39  = "-1.#INF"
Some Examples would be:
* {{Inline code|5.197}}
* {{Inline code| 0.47}}
* {{Inline code| 16.0}}
* {{Inline code|.8314}}
* {{Inline code|12345}}


Indeterminate (NaN) = "-1.#IND"
A regex catching theese kind of numbers could look like this {{Inline code|<nowiki>((\.[0-9]+)|(\b[0-9]+(\.[0-9]+)?))\b</nowiki>}}


====Scientific Notation====
The [https://en.wikipedia.org/wiki/Scientific_notation Scientific Notation] is a way of expressing numbers that are too big or too small to be conveniently written in decimal form.


The [[finite]] command can be used to verify if a number represents infinity or NaN.
It starts of like a normal decimal and then gets expressed by an {{Inline code|E}} (Not case sensitive, thus {{Inline code|e}} is also valid) followed by an __optional__ {{Inline code|+}} or {{Inline code|-}} sign and ends with a range of digits.


===Technical Specification===
Some Examples would be:
A number is a sequence of characters starting either with a digit, an '''$''' or a '''.'''. If it starts with a digit it may be an integer (e.g. 3), a floating point number (e.g. 3.2) or a hexadecimal number (see below). If it starts with a '''$''' it is also a hexadecimal number and if it starts with a '''.''' it is a floating point number smaller than 0 (e.g. .1 = 0.1).
* {{Inline code|1.23E4 ⇔ 1.23 ⋅ 10<sup>4 </sup> ⇔ 12300}}
If the number is not hexadecimal it may contain exactly one period and ends before the first non-digit character (except that one period of course). The only exception from that is the scientific notation.
* {{Inline code|5e-2  ⇔    5 ⋅ 10<sup>-2</sup> ⇔  0.05}}


The scientific notation in SQF has the syntax <code>([0-9]+.)?[0-9]+[eE][+-]?[0-9]+</code>
A regex catching theese kind of numbers could look like this  {{Inline code|([0-9]+.)?[0-9]+[eE][+-]?[0-9]+}}
{{Informative|Regex to match all numbers is <code><nowiki>((\$[0-9a-fA-F]+)|(\.[0-9]+))|(\b[0-9]+(\.[0-9]+|[eE][-+]?[0-9]+)?\b)</nowiki></code>}}


That means it consists of a pre-factor that can be any number (integer or floating point). Then a '''e''' or '''E''' followed by an optional sign ('''+''' or '''-''') and at the end an <b>integer</b> (a whole number). This will then be interpreted in the following way
===Hexadecimal (Base 16)===
In [[SQF_syntax|SQF]], [https://en.wikipedia.org/wiki/Hexadecimal hexadecimal) (also base 16, or hex) is a positional numeral system with a base of 16.
They start either with {{Inline code|0x}} or with a single {{Inline code|$}}.


<code>aEb = a * 10^b</code>
This gets followed by one of the following characters: {{Inline code|0 1 2 3 4 5 6 7 8 9 A B C D E F}}.
<b>Examples:</b>
Note that casing does not matters, thus both {{Inline code|0xa}} and {{Inline code|0xA}} are valid
<code>1e2 = 1E2 = 100<br>5e-2 = 5E-2 = 0.05<br>1.234e4 = 1.234E4 = 12340</code>


If omitted the exponent '''b''' will be considered to be zero and therefore
Some Examples would be:
<pre>aE = a</pre>
* {{Inline code|0xa5}}
Although the result is being calculated the engine will issue an error on that becasue the exponent is not optional!
* {{Inline code|$5C}}
If the pre-factor '''a''' is omitted it is no longer a number as it doesn't start with a digit or a period. If one tries to use a hexadecimal number as the pre-factor the '''e''' will simply be interpreted as part of that hexadecimal number and therefore it is no longer a scientific notation.
* {{Inline code|$FFFFFF}}
* {{Inline code|0x123ABC}}


Essentially numbers in SQF are in the format the '''atof''' function in C(++) expects numbers to be (See http://www.cplusplus.com/reference/cstdlib/atof/) with the exception that hexadecimals can also start with a '''$''' as well.
A regex catching theese kind of numbers could look like this {{Inline code|<nowiki>((\$|0x)[0-9a-fA-F]+)\b</nowiki>}}


==Hex numbers==
==Config==
 
{{Stub}}
Arma supports hexadecimal numbers in both scripts and configs. Most common notation for hex numbers is '''0x''' followed by the hexadecimal value. Alternatively symbol '''$''' can be used for the same thing. It is also possible to mix and match notations:
Unlike scripting, configs actually do allow for multiple number types (integer, float, etc.).
 
They are stored proper with their corresponding type in mind.
<code>[[hint]] [[str]] 0xFF; //255
[[hint]] [[str]] $FF; //255
[[hint]] [[str]] (0xFF + $FF + 255); //765</code>
Hexadecimal numbers are case-insensitive:
<pre>0xFF = 0xff</pre>


<!--
The following used to be written down in here.
It was removed as it is not providing any informations whatsoever (Degrees, Radians), Redundant info or stuff that is not related to the numerical numbertype (parseNumber stuff).
In case anybody wants to add informations (for example: what a "poetic label" actually means and looks), it may be readded.
-->
<!--
==Degrees==
==Degrees==


Line 68: Line 81:


<code>[[hint]] [[str]] [[parseNumber]] [[true]]; //1</code>
<code>[[hint]] [[str]] [[parseNumber]] [[true]]; //1</code>
-->
[[Category: Data Types]]
[[Category: Data Types]]

Revision as of 02:26, 29 March 2019

Template:SideTOC A number is, depending on scope, either a single precision floating point number (when talking about scripting) or a range of numerical types when talking about config context.

Scripting

In SQF, there are multiple accepted number formats. However, all of them will result in the same SCALAR (typeName) value type.

Regex to match all numbers in SQF is (((\$|0x)[0-9a-fA-F]+)|(\.[0-9]+))|(\b[0-9]+(\.[0-9]+|[eE][-+]?[0-9]+)?)\b

The largest positive number that can be archived is 3.4028235e38 and the largest negative is -3.4028235e38. However, it also is possible to generate positive or negative infinite values using either 1e39 (string representation: 1.#INF) or -1e39 (string representation: -1.#INF).

To check, if a number is finite, one can use the finite operator.

Decimal (Base 10)

A decimal number is your normal 0.5 syntax stuff with one extra: You may ommit the initial pack of digits.

Some Examples would be:

  • 5.197
  • 0.47
  • 16.0
  • .8314
  • 12345

A regex catching theese kind of numbers could look like this ((\.[0-9]+)|(\b[0-9]+(\.[0-9]+)?))\b

Scientific Notation

The Scientific Notation is a way of expressing numbers that are too big or too small to be conveniently written in decimal form.

It starts of like a normal decimal and then gets expressed by an E (Not case sensitive, thus e is also valid) followed by an __optional__ + or - sign and ends with a range of digits.

Some Examples would be:

  • 1.23E4 ⇔ 1.23 ⋅ 104 ⇔ 12300
  • 5e-2 ⇔ 5 ⋅ 10-2 ⇔ 0.05

A regex catching theese kind of numbers could look like this ([0-9]+.)?[0-9]+[eE][+-]?[0-9]+

Hexadecimal (Base 16)

In SQF, [https://en.wikipedia.org/wiki/Hexadecimal hexadecimal) (also base 16, or hex) is a positional numeral system with a base of 16. They start either with 0x or with a single $.

This gets followed by one of the following characters: 0 1 2 3 4 5 6 7 8 9 A B C D E F. Note that casing does not matters, thus both 0xa and 0xA are valid

Some Examples would be:

  • 0xa5
  • $5C
  • $FFFFFF
  • 0x123ABC

A regex catching theese kind of numbers could look like this ((\$|0x)[0-9a-fA-F]+)\b

Config

Template:Stub Unlike scripting, configs actually do allow for multiple number types (integer, float, etc.). They are stored proper with their corresponding type in mind.