Skip to content

Conversation

@karl-police
Copy link
Contributor

@karl-police karl-police commented Nov 27, 2025

math.truncate it cuts off digits after the decimal point, without having to use math.floor or math.clamp.
🤔

Render

@ishtar112
Copy link

ishtar112 commented Nov 28, 2025

This RFC aligns with an RFC I made a while back that was rejected, however, your proposal does a great job of "answering" the reasons why mine was rejected. Would be happy to see this happen.

@MagmaBurnsV
Copy link
Contributor

You can already achieve number truncation with math.modf.

Furthermore, it is trivial to construct a function that truncates to a specific decimal digit with this, which is what I assume you mean by the term "idp" in the RFC.

local function truncate(value: number, digits: number): number
	local factor = 10 ^ digits
	return math.modf(value * factor) / factor
end

The only value a new standard function could bring is streamlining arbitrary digit truncation, which I do not believe is a common enough operation to justify addition.

@karl-police
Copy link
Contributor Author

karl-police commented Nov 28, 2025

You can already achieve number truncation with math.modf.

Furthermore, it is trivial to construct a function that truncates to a specific decimal digit with this, which is what I assume you mean by the term "idp" in the RFC.

local function truncate(value: number, digits: number): number
	local factor = 10 ^ digits
	return math.modf(value * factor) / factor
end

The only value a new standard function could bring is streamlining arbitrary digit truncation, which I do not believe is a common enough operation to justify addition.

Since that one returns a tuple, What happens to the the second tuple in memory? Does it just get reliably garbage collected?

@aatxe
Copy link
Contributor

aatxe commented Nov 28, 2025

Since that one returns a tuple, What happens to the the second tuple in memory? Does it just get reliably garbage collected?

Luau does not have tuples. modf returns two numbers on the stack, and if they are not used, they are not used. There's no heap allocation associated with returning two numbers.

@karl-police
Copy link
Contributor Author

Since that one returns a tuple, What happens to the the second tuple in memory? Does it just get reliably garbage collected?

Luau does not have tuples. modf returns two numbers on the stack, and if they are not used, they are not used. There's no heap allocation associated with returning two numbers.

Ah, I see. Tuples to me are just a list of values, not sure if it has any other meanings.

You can already achieve number truncation with math.modf.

Is.modf more expensive than math.ceil and or math.floor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants