You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: 'Used to round down a value to the closest integer less than or equal to that value.'
2
+
Title: 'math.floor()'
3
+
Description: 'Rounds a number down to the nearest integer.'
4
4
Subjects:
5
5
- 'Computer Science'
6
-
- 'Game Development'
6
+
- 'Web Development'
7
7
Tags:
8
-
- 'Arithmetic'
9
8
- 'Functions'
10
9
- 'Math'
11
10
- 'Numbers'
@@ -14,56 +13,239 @@ CatalogContent:
14
13
- 'paths/computer-science'
15
14
---
16
15
17
-
In Lua, the **`math.floor()`** function is a standard function that is part of the Lua `math` library. The `math.floor()` function is used to round down a given value to its closest integer value that is less than or equal to the given value.
16
+
The **`math.floor()`** function is a built-in mathematical function in Lua that rounds a number down to the nearest integer. It returns the largest integer less than or equal to the given number, effectively rounding down any fractional value.
18
17
19
-
> **Note:**`math.floor()`always returns a value equal to or less than the given value. It _does not_ round up under any circumstances. For example, `math.floor(2.999)` returns 2 as that is the closest integer value that is _less than or equal_ to 2.999. To round numbers in either direction, use `math.round()`.
18
+
The `math.floor()`function is commonly used in programming scenarios where precise integer values are needed, such as array indexing, pagination calculations, converting measurements to whole units, creating stepped progress bars, and implementing floor division algorithms. It's beneficial in game development for coordinate calculations, financial applications for currency rounding, and data processing where fractional values need to be eliminated while maintaining mathematical accuracy.
20
19
21
20
## Syntax
22
21
23
-
As `floor()` is a method that is a part of the standard Lua `math` library, it must be called as `math.floor()`.
24
-
25
22
```pseudo
26
-
math.floor(mycoolvalue) -- Returns the closest integer value that is less than or equal to mycoolvalue
23
+
math.floor(x)
24
+
```
25
+
26
+
**Parameters:**
27
+
28
+
-`x`: A numeric value (integer or float) that will be rounded down to the nearest integer.
29
+
30
+
**Return value:**
31
+
32
+
The `math.floor()` function returns an integer value representing the largest integer less than or equal to the input parameter `x`. The function returns the same integer value if the input is already an integer.
33
+
34
+
## Example 1: Basic Floor Operation
35
+
36
+
This example demonstrates the fundamental usage of `math.floor()` with positive decimal numbers:
37
+
38
+
```lua
39
+
-- Basic floor operation with positive numbers
40
+
localvalue1=7.8
41
+
localvalue2=12.1
42
+
localvalue3=5.999
43
+
44
+
-- Apply floor function to each value
45
+
localresult1=math.floor(value1)
46
+
localresult2=math.floor(value2)
47
+
localresult3=math.floor(value3)
48
+
49
+
-- Display the results
50
+
print("math.floor(" ..value1..") = " ..result1)
51
+
print("math.floor(" ..value2..") = " ..result2)
52
+
print("math.floor(" ..value3..") = " ..result3)
53
+
```
54
+
55
+
The output for this code is:
56
+
57
+
```shell
58
+
math.floor(7.8) = 7
59
+
math.floor(12.1) = 12
60
+
math.floor(5.999) = 5
27
61
```
28
62
29
-
## Example 1
63
+
This example shows how `math.floor()` consistently rounds down to the nearest integer regardless of how close the decimal portion is to the following whole number. Even 5.999 rounds down to 5, not up to 6.
64
+
65
+
## Example 2: Array Indexing with Floor
30
66
31
-
To find the closest integer less than or equal to 1.234:
67
+
This example demonstrates a practical use case where `math.floor()` ensures proper array indexing by converting floating-point calculations to valid integer indices:
32
68
33
69
```lua
34
-
print(math.floor(1.234))
70
+
-- Simulating array indexing with calculated positions
print("20% through array gives index: " ..lowIndex)
96
+
print("Item: " ..inventory[lowIndex])
35
97
```
36
98
37
-
This results in the following output:
99
+
The output of this code will be:
38
100
39
101
```shell
40
-
1
102
+
Calculated index: 3.75
103
+
Floored index: 3
104
+
Item at index 3: potion
105
+
20% through array gives index: 1
106
+
Item: sword
41
107
```
42
108
43
-
## Example 2
109
+
This example shows how `math.floor()` prevents array indexing errors by ensuring calculated indices are always valid integers, which is essential when working with dynamic positioning in arrays.
110
+
111
+
## Example 3: Financial Calculations
44
112
45
-
It is important to remember that `math.floor()`does not perform rounding, as in the following example:
113
+
This example demonstrates using `math.floor()`in financial calculations where monetary values need to be rounded down to avoid overcharging customers:
46
114
47
115
```lua
48
-
print(math.floor(2.999))
116
+
-- Financial calculation example - discount pricing
This example illustrates how `math.floor()` is used in financial applications to ensure customers are never overcharged due to rounding, while businesses can maintain predictable pricing structures.
174
+
175
+
## Example 4: Use Cases for Negative Numbers
58
176
59
-
When working with negative numbers, it is important to remember that `math.floor()`returns the closest integer _less than or equal_ to a given value:
177
+
This example shows how `math.floor()`behaves with negative numbers, which is crucial for understanding its mathematical definition:
60
178
61
179
```lua
62
-
print(math.floor(-4.56))
180
+
-- Working with negative numbers
181
+
localnegativeValues= {-2.3, -5.7, -10.1, -0.5}
182
+
183
+
print("Understanding math.floor() with negative numbers:")
> **Note:** With negative numbers, `math.floor()` rounds toward negative infinity, not toward zero. This means -2.3 becomes -3, not -2.
230
+
231
+
## Frequently Asked Questions
232
+
233
+
### 1. What's the difference between `math.floor()` and `math.ceil()`?
234
+
235
+
`math.floor()` always rounds down toward negative infinity, while `math.ceil()` always rounds up toward positive infinity. For positive numbers, floor rounds are placed toward zero, and ceil rounds are placed away from zero.
236
+
237
+
### 2. Does `math.floor()` work with negative numbers?
238
+
239
+
Yes, but it's important to understand that `math.floor()` rounds toward negative infinity. This means -2.3 becomes -3, not -2, because -3 is the largest integer less than or equal to -2.3.
240
+
241
+
### 3. What happens if I pass an integer to `math.floor()`?
242
+
243
+
If you pass an integer to `math.floor()`, it returns the same integer unchanged. For example, `math.floor(5)` returns 5.
244
+
245
+
### 4. What happens if I pass a string to `math.floor()`?
246
+
247
+
Passing a string to `math.floor()` will result in an error. The function expects a numeric value. Use `tonumber()` to convert strings to numbers first if needed.
248
+
249
+
### 5. Is there a performance difference between `math.floor()` and other rounding methods?
250
+
251
+
`math.floor()` is highly optimized and generally very fast. It's typically more efficient than manual rounding approaches and is the standard way to perform floor operations in Lua.
0 commit comments