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

Support for fix() or abs() #83

Closed
Ardnived opened this issue Apr 22, 2014 · 7 comments
Closed

Support for fix() or abs() #83

Ardnived opened this issue Apr 22, 2014 · 7 comments

Comments

@Ardnived
Copy link
Contributor

In GURPS you should be able to "spend" negative values of points to reduce your stats. However, since floor() always rounds to the lower number, the calculation gets messed up.

For example, if the stat has a base value of 10, and costs 20 points the raise/lower. I'd use a formula like this.

10 + floor(@{points} / 20)

For positive values this works fine, but for most negative values the output is 1 lower than it should be.

10 + floor(24 / 20) = 11
10 + floor(-24 / 20) = 8

Could you implement a fix() function, which always rounds the value towards 0.

Or alternatively add an absolute value function. In which case I could emulate fix() like so:

@{points}/abs(@{points}) * floor(abs(@{points}) / 20)
@Lithl
Copy link
Contributor

Lithl commented Apr 26, 2014

floor becomes round if you add 0.5 to the value. For example: floor((-24 / 20) + 0.5) = floor(-1.2 + 0.5) = floor(-0.7) = 1. The same thing happens with ceil if you subtract 0.5.

I also seem to recall Riley including a round function on the dev server, but I can't find his post about that, so I'm not sure.

@Ardnived
Copy link
Contributor Author

I thought about that, but it doesn't always work.

floor((30 / 20) + 0.5) = floor(1.5 + 0.5) = 2
fix((30 / 20)) = fix(1.5) = 1

To be clear, I'm looking for a function that always rounds towards 0. Not a function that rounds naturally.
ie. 1.9 should become 1, 1.5 should become 1, -2.1 should become -2.

@Lithl
Copy link
Contributor

Lithl commented Apr 27, 2014

Ah, yeah; if you want "towards 0," that wouldn't work. Adding half and flooring the number is round, not towards 0.

An option other than fix (which I've honestly never heard of being a standard implementation) would be sign. sign(x) = 1 if x >= 0 and sign(x) = -1 if x < 0. Thus, x * sign(x) = abs(x), and func(x * sign(x)) * sign(x) maintains the sign of x for the final result.

@Ardnived
Copy link
Contributor Author

Yeah, what you pointed out with sign() is essentially what I was suggesting could be done using abs().

func(x * sign(x)) * sign(x)   vs   func(abs(x)) * x/abs(x)

sign(x) and x/abs(x) would have the same result.
Unfortunately I don't think Roll20 offers the sign() function either.

I've also never heard of fix being regularly available, I just googled to see what such a function is usually called, and "fix" is what came up.

@victor
Copy link
Contributor

victor commented Apr 27, 2014

Math.sign(x) is only in the candidate stages of the specification, and thus it is not available although some browsers do implement it. But as you know, it can easily be substituted for equivalent expressions.

I think the fix function name comes from MATLAB.
Anyway, FWIW, I'd also like this this functionality to be available, to be able to calculate degrees of success in Deathwatch rolls

@Ardnived
Copy link
Contributor Author

Ardnived commented May 2, 2014

Another solution would be to add a tertiary operator (ie. x == 0 ? y : z which translates to if x is 0 then y else z). Then we could do x > 0 ? floor(x) : ceil(y)

Which would probably be somewhat more complicated to implement, though also more powerful for those developing character sheets.

@rileydutton
Copy link
Member

Support for abs() was added today: https://app.roll20.net/forum/post/827711/#post-827711

wing-it pushed a commit that referenced this issue Sep 14, 2020
* Fix new sheets incorrectly initializing as AI and Transhuman.

* Changelog and version.

* Add missing translation key.

* Fix Effort bug

* Str/Dex mod bug

* Weapon/attack description field.

* Add toggle (default to off) to calculate ship price.

* Reinstate drones fix that got lost in the worker split.

* Drones support for NPCs. Closes #82.

* Add NPC Shock damage. Closes #83.

* Fix NPC attack display wrong range.
mperes pushed a commit that referenced this issue Oct 5, 2020
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

No branches or pull requests

4 participants