Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: support of square root and round functions to builtin functions #199

Merged
merged 1 commit into from Apr 2, 2021

Conversation

werbfred
Copy link
Contributor

@werbfred werbfred commented Apr 1, 2021

Added sqrt function to builtin ones as it could be usefull in other projects.

I implemented it because the SQRT approximation in 2cc_TrainsInNML.grf was giving sometimes weird results. Using math.sqrt is much more accurate.

raise generic.ScriptError("sqrt() requires 1 argument", pos)
elif len(args) != 1:
raise generic.ScriptError("sqrt() requires only 1 argument", pos)
return ConstantFloat(math.sqrt(args[0].value),pos)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs a check for args[0] being a constant value, similar as for the trigonometric functions above.

if not isinstance(val, (ConstantNumeric, ConstantFloat)):
        raise generic.ScriptError("Parameter for " + name + "() must be a constant", pos)

Actually, it may be easier to just extend the mapping in builtin_trigonometric, and rename it to builtin_math or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated Pull Request's commit with proposed updates

@werbfred werbfred changed the title Add support of square root function in builtin functions Add support of square root and round functions to builtin functions Apr 2, 2021
@werbfred werbfred changed the title Add support of square root and round functions to builtin functions Add: support of square root and round functions to builtin functions Apr 2, 2021
@werbfred
Copy link
Contributor Author

werbfred commented Apr 2, 2021

Added round builtin as well as it's better than using int. This way floats are rounds down/up to its nearest integer.

In my use case, this allows using formulas to determine base/running costs more accurate and ensuring them to be an 1 Byte integer.

@FLHerne
Copy link
Contributor

FLHerne commented Apr 2, 2021

You can simply add round to builtin_math also? It's exactly the same code.

Please reorder or squash the commits so you're not introducing an error and then fixing it. That should also make the commit-checker stop complaining.

@werbfred
Copy link
Contributor Author

werbfred commented Apr 2, 2021

Round should return a ConstantNumeric i.e. an integer value, while all other math functions as sqrt and existing sin, cos, tan return ConstantFloat s...

@FLHerne
Copy link
Contributor

FLHerne commented Apr 2, 2021

Round should return a ConstantNumeric i.e. an integer value, while all other math functions as sqrt and existing sin, cos, tan return ConstantFloat s...

Sorry, I missed that. My mistake.

These require constant arguments and map to the Python functions.
@frosch123 frosch123 merged commit 1fec776 into OpenTTD:master Apr 2, 2021
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.

None yet

3 participants