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

Proper rounding implementation #23

Merged
merged 8 commits into from
Mar 23, 2024

Conversation

davidfoerster
Copy link
Contributor

@davidfoerster davidfoerster commented Mar 14, 2024

…in accordance with Basisregelwerk page 13 (“Rundungen”).

The previous implementation relies on string.format("%.0f", x) whose behaviour is inconsistent across Lua versions.

Other rounding uses idioms like

  • math.floor(x + 0.5) which is fine for DSA number ranges but generally inexact,
  • (n + d2) // d (where d and d2 are positive integer literals and d2 is (d÷2) rounded down) which is a bit difficult to read and error-prone.

Potential caveat: this implementation rounds halfway cases away from zero while the rules state that (barring special cases) they should be rounded “up”, i. e. towards positive infinity. However, I have yet to see a rule that requires the rounding of negative values (to any other value than zero); instances where negative modifiers need rounding (e. g. encumbrance), the rounding is supposed to be applied to their absolute values, i. e. away from zero.

...in accordance with Basisregelwerk page 13 ("Rundungen").

The previous implementation relies on `string.format("%.0f", x)` whose
behaviour is inconsistent accross Lua versions.
- common.render_delta() used to generate the wrong sign for non-zero
  values in [-0.5, 0.5].
- Use local variable for sign character to better describe what's
  happening.
- Use a *local* variable to store intermediary results.
- Emit "&" and the integral value with a single tex.sprint() since the latter
  can't result in special characters.
that previously involved `math.floor(x + 0.5)` or `string.format("%.0f", x)`.
Copy link
Owner

@flyx flyx left a comment

Choose a reason for hiding this comment

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

Generell okay, da war schon viel Wildwuchs in den Rundungsmechanismen.

Allerdings zieht ungerade eBE jetzt einen Punkt zu viel von der AT ab, das müsstest du noch fixen.

@@ -158,14 +159,14 @@ nahkampf_render[11]= {false, function(v, talent, ebe)
if talent == nil or #talent < 4 or atb == "" or #v < 8 then
return
end
tex.sprint(-2, atpa_mod(atb - common.round(ebe/2, true), talent.AT, v["TP/KK Schwelle"], v["TP/KK Schritt"], v["WM AT"], art(v), talent.Spezialisierungen))
tex.sprint(-2, atpa_mod(atb - math.round(ebe/2, true), talent.AT, v["TP/KK Schwelle"], v["TP/KK Schritt"], v["WM AT"], art(v), talent.Spezialisierungen))
Copy link
Owner

Choose a reason for hiding this comment

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

math.round hat den zweiten Parameter nicht mehr, ist also an der Stelle falsch und müsste statt dessen math.floor sein.

Sonst wird bei einer ungeraden eBE ein Punkt zu viel von der AT abgezogen.

@@ -252,7 +253,7 @@ local waffenlos_render = {
added = added + 1
end
end
tex.sprint(-2, atpa_mod(atb - common.round(ebe/2, true), talent.AT, v["TP/KK Schwelle"], v["TP/KK Schritt"], 0))
tex.sprint(-2, atpa_mod(atb - math.round(ebe/2, true), talent.AT, v["TP/KK Schwelle"], v["TP/KK Schritt"], 0))
Copy link
Owner

Choose a reason for hiding this comment

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

Selbe Geschichte wie oben.

@davidfoerster
Copy link
Contributor Author

Danke für die Kommentare! Ich kümmere mich im Verlauf der Woche um die beiden Probleme.

Strictly speaking, the rules of distribution of effective encumberance onto
AT/PA values use integer floor/ceil division by two rather than rounding
(Basisregelwerk, p. 137). So that's what we'll use in the implementation.
@davidfoerster davidfoerster requested a review from flyx March 21, 2024 00:48
@flyx flyx merged commit 7809670 into flyx:master Mar 23, 2024
1 check passed
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.

2 participants