From a0622dad0fcab145104939c092d620f76cd010e7 Mon Sep 17 00:00:00 2001 From: YRSG Date: Sat, 3 Oct 2026 10:07:15 +0800 Subject: [PATCH 1/3] GDScript Math concept docs Do not merge-This is a WIP Adds the math docs --- concepts/numbers/about.md | 199 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 concepts/numbers/about.md diff --git a/concepts/numbers/about.md b/concepts/numbers/about.md new file mode 100644 index 00000000..342a1c51 --- /dev/null +++ b/concepts/numbers/about.md @@ -0,0 +1,199 @@ +# About + +GDScript has three different types of built-in numbers: integers ([`int`][int]), floating-point ([`float`][float]), and Vectors ([`Vector`][vector]). #To add 1- + +```gdscript +# Ints are whole numbers. +>>> 1234 +1234 +>>> type(1234) + + +>>> -12 +-12 +``` + +Numbers containing a decimal point (with or without fractional parts) are identified as `floats`: + +```gdscript +>>> 3.45 +3.45 +>>> type(3.45) + +``` + +## Arithmetic + +GDScript fully supports arithmetic between these different number types, and will convert narrower numbers to match their less narrow counterparts when used with the binary arithmetic operators (`+`, `-`, `*`, `/`, `//`, and `%`). + +All numbers (except vectors) support all [arithmetic operations][arithmetic-operations], evaluated according to [operator precedence][operator precedence]. +Support for mathematical functions (beyond `+` and `-`) for complex numbers can be found in the [cmath][cmath] module. + +### Addition and subtraction + +Addition and subtraction operators behave as they do in normal math. +If one or more of the operands is a `float`, the remaining `int`s will be converted to `float`s as well: + +```python +>>> 5 - 3 +2 +# The int is widened to a float here, and a float is returned. +>>> 3 + 4.0 +7.0 +``` + +### Multiplication + +As with addition and subtraction, multiplication will convert narrower numbers to match their less narrow counterparts: + +```python +>>> 3 * 2 +6 + +>>> 3 * 2.0 +6.0 +``` + +### Division + +Division always returns a `float`, even if the result is a whole number: + +```python +>>> 6/5 +1.2 + +>>> 6/2 +3.0 +``` + +### Floor division + +If an `int` result is needed, you can use floor division to truncate the result. +Floor division is performed using the `//` operator: + +```python +>>> 6//5 +1 + +>>> 6//2 +3 +``` + +### Modulo + +The modulo operator (`%`) returns the remainder of the division of the two operands: + +```python +# The result of % is zero here, because dividing 8 by 2 leaves no remainder +>>> 8 % 2 +0 + +# The result of % is 2 here, because 3 only goes into 5 once, with 2 left over +>>> 5 % 3 +2 +``` + +Another way to look at 5 % 3: + +```python +>>> whole_part = int(5/3) +1 + +>>> decimal_part = 5/3 - whole_part +0.6666666666666667 + +>>> whole_remainder = decimal_part * 3 +2.0 +``` + +## Exponentiation + +Exponentiation is performed using the `**` operator: + +```python +>>> 2 ** 3 +8 + +>>> 4 ** 0.5 +2 +``` + +## Conversions + +Numbers can be converted from `int` to `floats` and `floats` to `int` using the built-in functions `int()` and `float()`: + +```python +>>> int(3.45) +3 + +>>> float(3) +3.0 +``` + +## Round + +Python provides a built-in function [`round(, )`][round] to round off a floating point number to a given number of decimal places. +If no number of decimal places is specified, the number is rounded off to the nearest integer and will return an `int`: + +```python +>>> round(3.1415926535, 2) +3.14 + +>>> round(3.1415926535) +3 +``` + +## Priority and parentheses + +Python allows you to use parentheses to group expressions. +This is useful when you want to override the default order of operations. + +```python +>>> 2 + 3 * 4 +14 + +>>> (2 + 3) * 4 +20 +``` + +Python follows the [PEMDAS][pemdas] rule for operator precedence. +This means calculations within `()` have the highest priority, followed by `**`, then `*`, `/`, `//`, `%`, `+`, and `-`: + +```python +>>> 2 + 3 - 4 * 4 +-11 + +>>> (2 + 3 - 4) * 4 +4 + +# In the following example, the `**` operator has the highest priority, then `*`, then `+` +# Meaning we first do 4 ** 4, then 3 * 256, then 2 + 768 +>>> 2 + 3 * 4 ** 4 +770 +``` + +## Precision & Representation + +Integers in Python have [arbitrary precision][arbitrary-precision] -- the number of digits is limited only by the available memory of the host system. + +Floating point numbers are usually implemented using a `double` in C (_15 decimal places of precision_), but will vary in representation based on the host system. +Complex numbers have a `real` and an `imaginary` part, both of which are represented by floating point numbers. + +For a more detailed discussions of the issues and limitations of floating point arithmetic across programming languages, take a look at [0.30000000000000004.com][0.30000000000000004.com] and [The Python Tutorial][floating point math]. + +[0.30000000000000004.com]: https://0.30000000000000004.com/ +[arbitrary-precision]: https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic +[arithmetic-operations]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex +[bin]: https://docs.python.org/3/library/functions.html#bin +[cmath]: https://docs.python.org/3.9/library/cmath.html +[decimals]: https://docs.python.org/3/library/decimal.html#module-decimal +[float]: https://docs.godotengine.org/en/stable/classes/class_float.html#class-float +[floating point math]: https://docs.python.org/3.9/tutorial/floatingpoint.html +[fractions]: https://docs.python.org/3/library/fractions.html +[hex]: https://docs.python.org/3/library/functions.html#hex +[int]: https://docs.godotengine.org/en/stable/classes/class_int.html#int +[oct]: https://docs.python.org/3/library/functions.html#oct +[operator precedence]: https://docs.python.org/3/reference/expressions.html#operator-precedence +[pemdas]: https://mathworld.wolfram.com/PEMDAS.html +[round]: https://docs.python.org/3/library/functions.html#round +[vector]: https://docs.godotengine.org/en/stable/classes/class_vector2.html#class-vector2 From 7adb43327d91d8e6e435096ae921817bd6e87238 Mon Sep 17 00:00:00 2001 From: YRSG Date: Sat, 3 Oct 2026 14:24:30 +0800 Subject: [PATCH 2/3] Revise arithmetic operations section in about.md Updated examples and explanations for arithmetic operations, including addition, subtraction, multiplication, division, modulo, exponentiation, conversions, rounding, and operator precedence. --- concepts/numbers/about.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/concepts/numbers/about.md b/concepts/numbers/about.md index 342a1c51..6948c9cf 100644 --- a/concepts/numbers/about.md +++ b/concepts/numbers/about.md @@ -34,7 +34,7 @@ Support for mathematical functions (beyond `+` and `-`) for complex numbers can Addition and subtraction operators behave as they do in normal math. If one or more of the operands is a `float`, the remaining `int`s will be converted to `float`s as well: -```python +```gdscript >>> 5 - 3 2 # The int is widened to a float here, and a float is returned. @@ -46,7 +46,7 @@ If one or more of the operands is a `float`, the remaining `int`s will be conver As with addition and subtraction, multiplication will convert narrower numbers to match their less narrow counterparts: -```python +```gdscript >>> 3 * 2 6 @@ -58,7 +58,7 @@ As with addition and subtraction, multiplication will convert narrower numbers t Division always returns a `float`, even if the result is a whole number: -```python +```gdscript >>> 6/5 1.2 @@ -71,7 +71,7 @@ Division always returns a `float`, even if the result is a whole number: If an `int` result is needed, you can use floor division to truncate the result. Floor division is performed using the `//` operator: -```python +```gdscript >>> 6//5 1 @@ -83,7 +83,7 @@ Floor division is performed using the `//` operator: The modulo operator (`%`) returns the remainder of the division of the two operands: -```python +```gdscript # The result of % is zero here, because dividing 8 by 2 leaves no remainder >>> 8 % 2 0 @@ -95,7 +95,7 @@ The modulo operator (`%`) returns the remainder of the division of the two opera Another way to look at 5 % 3: -```python +```gdscript >>> whole_part = int(5/3) 1 @@ -110,7 +110,7 @@ Another way to look at 5 % 3: Exponentiation is performed using the `**` operator: -```python +```gdscript >>> 2 ** 3 8 @@ -122,7 +122,7 @@ Exponentiation is performed using the `**` operator: Numbers can be converted from `int` to `floats` and `floats` to `int` using the built-in functions `int()` and `float()`: -```python +```gdscript >>> int(3.45) 3 @@ -135,7 +135,7 @@ Numbers can be converted from `int` to `floats` and `floats` to `int` using the Python provides a built-in function [`round(, )`][round] to round off a floating point number to a given number of decimal places. If no number of decimal places is specified, the number is rounded off to the nearest integer and will return an `int`: -```python +```gdscript >>> round(3.1415926535, 2) 3.14 @@ -148,7 +148,7 @@ If no number of decimal places is specified, the number is rounded off to the ne Python allows you to use parentheses to group expressions. This is useful when you want to override the default order of operations. -```python +```gdscript >>> 2 + 3 * 4 14 @@ -159,7 +159,7 @@ This is useful when you want to override the default order of operations. Python follows the [PEMDAS][pemdas] rule for operator precedence. This means calculations within `()` have the highest priority, followed by `**`, then `*`, `/`, `//`, `%`, `+`, and `-`: -```python +```gdscript >>> 2 + 3 - 4 * 4 -11 From c6a88ea96fc10022f210db8d5b170c0d3904188e Mon Sep 17 00:00:00 2001 From: YRSG Date: Sun, 4 Oct 2026 08:41:35 +0800 Subject: [PATCH 3/3] Update Python references to GDScript in about.md --- concepts/numbers/about.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/concepts/numbers/about.md b/concepts/numbers/about.md index 6948c9cf..e1a28e01 100644 --- a/concepts/numbers/about.md +++ b/concepts/numbers/about.md @@ -132,7 +132,7 @@ Numbers can be converted from `int` to `floats` and `floats` to `int` using the ## Round -Python provides a built-in function [`round(, )`][round] to round off a floating point number to a given number of decimal places. +GDScript provides a built-in function [`round(, )`][round] to round off a floating point number to a given number of decimal places. If no number of decimal places is specified, the number is rounded off to the nearest integer and will return an `int`: ```gdscript @@ -145,7 +145,7 @@ If no number of decimal places is specified, the number is rounded off to the ne ## Priority and parentheses -Python allows you to use parentheses to group expressions. +GDScript allows you to use parentheses to group expressions. This is useful when you want to override the default order of operations. ```gdscript @@ -156,7 +156,7 @@ This is useful when you want to override the default order of operations. 20 ``` -Python follows the [PEMDAS][pemdas] rule for operator precedence. +GDScript follows the [PEMDAS][pemdas] rule for operator precedence. This means calculations within `()` have the highest priority, followed by `**`, then `*`, `/`, `//`, `%`, `+`, and `-`: ```gdscript @@ -174,26 +174,26 @@ This means calculations within `()` have the highest priority, followed by `**`, ## Precision & Representation -Integers in Python have [arbitrary precision][arbitrary-precision] -- the number of digits is limited only by the available memory of the host system. +Integers in GDScript have [arbitrary precision][arbitrary-precision] -- the number of digits is limited only by the available memory of the host system. Floating point numbers are usually implemented using a `double` in C (_15 decimal places of precision_), but will vary in representation based on the host system. Complex numbers have a `real` and an `imaginary` part, both of which are represented by floating point numbers. -For a more detailed discussions of the issues and limitations of floating point arithmetic across programming languages, take a look at [0.30000000000000004.com][0.30000000000000004.com] and [The Python Tutorial][floating point math]. +For a more detailed discussions of the issues and limitations of floating point arithmetic across programming languages, take a look at [0.30000000000000004.com][0.30000000000000004.com] and [The GDScript Tutorial][floating point math]. [0.30000000000000004.com]: https://0.30000000000000004.com/ [arbitrary-precision]: https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic -[arithmetic-operations]: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex -[bin]: https://docs.python.org/3/library/functions.html#bin -[cmath]: https://docs.python.org/3.9/library/cmath.html -[decimals]: https://docs.python.org/3/library/decimal.html#module-decimal +[arithmetic-operations]: https://docs.GDScript.org/3/library/stdtypes.html#numeric-types-int-float-complex +[bin]: https://docs.GDScript.org/3/library/functions.html#bin +[cmath]: https://docs.GDScript.org/3.9/library/cmath.html +[decimals]: https://docs.GDScript.org/3/library/decimal.html#module-decimal [float]: https://docs.godotengine.org/en/stable/classes/class_float.html#class-float -[floating point math]: https://docs.python.org/3.9/tutorial/floatingpoint.html -[fractions]: https://docs.python.org/3/library/fractions.html -[hex]: https://docs.python.org/3/library/functions.html#hex +[floating point math]: https://docs.GDScript.org/3.9/tutorial/floatingpoint.html +[fractions]: https://docs.GDScript.org/3/library/fractions.html +[hex]: https://docs.GDScript.org/3/library/functions.html#hex [int]: https://docs.godotengine.org/en/stable/classes/class_int.html#int -[oct]: https://docs.python.org/3/library/functions.html#oct -[operator precedence]: https://docs.python.org/3/reference/expressions.html#operator-precedence +[oct]: https://docs.GDScript.org/3/library/functions.html#oct +[operator precedence]: https://docs.GDScript.org/3/reference/expressions.html#operator-precedence [pemdas]: https://mathworld.wolfram.com/PEMDAS.html -[round]: https://docs.python.org/3/library/functions.html#round +[round]: https://docs.GDScript.org/3/library/functions.html#round [vector]: https://docs.godotengine.org/en/stable/classes/class_vector2.html#class-vector2