From d150a2614650ac1690bb80e51b1397186a98f06b Mon Sep 17 00:00:00 2001 From: karl-police Date: Thu, 27 Nov 2025 17:29:08 +0100 Subject: [PATCH 1/6] Create function-math-truncate.md --- docs/function-math-truncate.md | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 docs/function-math-truncate.md diff --git a/docs/function-math-truncate.md b/docs/function-math-truncate.md new file mode 100644 index 000000000..a90e51134 --- /dev/null +++ b/docs/function-math-truncate.md @@ -0,0 +1,72 @@ +# math.truncate + +## Summary + +A method that returns the integer part of a number by removing any fractional digits. Along with specifying how many digits should be kept after the decimal point. + +## Motivation + +``math.floor`` does not give the same effect as, for instance ``Math.trunc()`` from JavaScript. But ``math.floor`` and ``math.ceil`` Can be used together. +But JavaScript only truncates, it doesn't feature + +For instance: +```lua +print( math.floor(-0.005) ) +-- Returns: -1 + +print( math.ceil(-0.005) ) +-- Returns: -0 +``` + +```js +Math.trunc(-0.005) +// Returns: -0 +``` + + +## Design + +``math.truncate(num, idp)`` +- ``num``: Number to truncate +- ``idp``: _(Optional)_, How many digits to keep after the decimal point + + +```lua +math.truncate(-0.005) +-- Expected output: -0 + +math.truncate(0.005) +-- Expected output: 0 + +math.truncate(1.23456789, 4) +-- Expected output: 1.2345 + +math.truncate(-math.huge) +-- Expected output: -inf +``` + + +## Drawbacks + +- Should ``idp`` be a thing? Since one can just do this _(JavaScript example)_ + +```js +function trunc_with_idp(num, idp) { + let mul = Math.pow(10, (idp || 0)) + return Math.trunc(num * mul) / mul +} +trunc_with_idp(1.2345, 4) +``` + +_or see alternative below_ + + +## Alternatives + +From [here](https://github.com/Facepunch/garrysmod/blob/8e96e31fc9ab9de053edc0513f9d7a6f3e4be1e1/garrysmod/lua/includes/extensions/math.lua#L168-L171) +```lua +function math.Truncate( num, idp ) + local mult = 10 ^ ( idp or 0 ) + return ( num < 0 and math.ceil or math.floor )( num * mult ) / mult +end +``` From 94cbcf9e86d1faafcd677cf3efde3c5a117b4f61 Mon Sep 17 00:00:00 2001 From: karl-police Date: Thu, 27 Nov 2025 17:31:45 +0100 Subject: [PATCH 2/6] Update function-math-truncate.md --- docs/function-math-truncate.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/function-math-truncate.md b/docs/function-math-truncate.md index a90e51134..d753b4ee2 100644 --- a/docs/function-math-truncate.md +++ b/docs/function-math-truncate.md @@ -2,12 +2,11 @@ ## Summary -A method that returns the integer part of a number by removing any fractional digits. Along with specifying how many digits should be kept after the decimal point. +A method that returns the integer part of a number by removing any fractional digits. Optionally, along with specifying how many digits should be kept after the decimal point. ## Motivation -``math.floor`` does not give the same effect as, for instance ``Math.trunc()`` from JavaScript. But ``math.floor`` and ``math.ceil`` Can be used together. -But JavaScript only truncates, it doesn't feature +``math.floor`` does not give the same effect as, for instance ``Math.trunc()`` from JavaScript. But ``math.floor`` and ``math.ceil`` can be used together, to re-produce the same result that ``Math.trunc()`` would give, but a function like ``math.truncate`` directly as a built-in would be more straightforward. For instance: ```lua From 97be25f632117739c663fce60693175b9629ff1d Mon Sep 17 00:00:00 2001 From: karl-police Date: Thu, 27 Nov 2025 17:40:32 +0100 Subject: [PATCH 3/6] Update function-math-truncate.md --- docs/function-math-truncate.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/function-math-truncate.md b/docs/function-math-truncate.md index d753b4ee2..dbff51e07 100644 --- a/docs/function-math-truncate.md +++ b/docs/function-math-truncate.md @@ -60,6 +60,10 @@ trunc_with_idp(1.2345, 4) _or see alternative below_ +Other questions to ask: +- Will a built-in offer optimization over the alternatives below? + + ## Alternatives From [here](https://github.com/Facepunch/garrysmod/blob/8e96e31fc9ab9de053edc0513f9d7a6f3e4be1e1/garrysmod/lua/includes/extensions/math.lua#L168-L171) From 4f54b8e975bc91bd0f002feb9ea30ef5c7a7ab7c Mon Sep 17 00:00:00 2001 From: karl-police Date: Thu, 27 Nov 2025 17:41:09 +0100 Subject: [PATCH 4/6] Update function-math-truncate.md --- docs/function-math-truncate.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/function-math-truncate.md b/docs/function-math-truncate.md index dbff51e07..2fac11177 100644 --- a/docs/function-math-truncate.md +++ b/docs/function-math-truncate.md @@ -66,7 +66,7 @@ Other questions to ask: ## Alternatives -From [here](https://github.com/Facepunch/garrysmod/blob/8e96e31fc9ab9de053edc0513f9d7a6f3e4be1e1/garrysmod/lua/includes/extensions/math.lua#L168-L171) +A Lua function from [here](https://github.com/Facepunch/garrysmod/blob/8e96e31fc9ab9de053edc0513f9d7a6f3e4be1e1/garrysmod/lua/includes/extensions/math.lua#L168-L171) ```lua function math.Truncate( num, idp ) local mult = 10 ^ ( idp or 0 ) From 11b690be1a2e0a4d6a6694edb1afcbafdf182b98 Mon Sep 17 00:00:00 2001 From: karl-police Date: Fri, 28 Nov 2025 14:40:15 +0100 Subject: [PATCH 5/6] Update function-math-truncate.md --- docs/function-math-truncate.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/function-math-truncate.md b/docs/function-math-truncate.md index 2fac11177..478298dac 100644 --- a/docs/function-math-truncate.md +++ b/docs/function-math-truncate.md @@ -6,7 +6,7 @@ A method that returns the integer part of a number by removing any fractional di ## Motivation -``math.floor`` does not give the same effect as, for instance ``Math.trunc()`` from JavaScript. But ``math.floor`` and ``math.ceil`` can be used together, to re-produce the same result that ``Math.trunc()`` would give, but a function like ``math.truncate`` directly as a built-in would be more straightforward. +``math.floor`` does not give the same effect as ``Math.trunc()`` from JavaScript, for instance. But ``math.floor`` and ``math.ceil`` can be used together, to re-produce the same result that ``Math.trunc()`` would give, but a function like ``math.truncate`` directly as a built-in would be more straightforward. For instance: ```lua From a0dd082dbc3ca904a624073a04d3e8666dacbd95 Mon Sep 17 00:00:00 2001 From: karl-police Date: Fri, 28 Nov 2025 14:41:38 +0100 Subject: [PATCH 6/6] Update function-math-truncate.md --- docs/function-math-truncate.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/function-math-truncate.md b/docs/function-math-truncate.md index 478298dac..ae2a3c9a3 100644 --- a/docs/function-math-truncate.md +++ b/docs/function-math-truncate.md @@ -8,7 +8,7 @@ A method that returns the integer part of a number by removing any fractional di ``math.floor`` does not give the same effect as ``Math.trunc()`` from JavaScript, for instance. But ``math.floor`` and ``math.ceil`` can be used together, to re-produce the same result that ``Math.trunc()`` would give, but a function like ``math.truncate`` directly as a built-in would be more straightforward. -For instance: +For instance, negative numbers: ```lua print( math.floor(-0.005) ) -- Returns: -1 @@ -17,11 +17,15 @@ print( math.ceil(-0.005) ) -- Returns: -0 ``` + + ```js Math.trunc(-0.005) // Returns: -0 ``` +Truncation specifically just cuts off the digits, while the others try to round. + ## Design