Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
This post has been closed. You can still view previous posts, but you can't post any new replies.

Truncate in macro

Hello! I'm sorely missing a truncate function in macros. In essence, a result of 2.5 would become 2, -2.5 would become -2, 3.14159 becomes 3 etc. Floor and Ceil doesn't work when both negative and positive values are possible, as floor would be used for positive numbers and ceil for negative, and there's no way beyond API to do conditional floor/ceil macros.
1386381792

Edited 1386382029
Sam
Pro
Sheet Author
If your numbers are being generated from a division then what you're essentially looking for is the ability to do what is called "Integer Division". In programming, the integer datatype only can display whole numbers. If you were to assign a number with a fractional portion to an integer, then only the the whole number is stored, the fraction portion is lost. It why in programming languages we have another datatype called "float". A float can store an integer plus any fractional portion to a certain degree of precision. Other data types give you more precision. The big issue you have here is in Roll20 we have no way to do simple inline logical statements in macros. Were that the case you could detect if the attribute you were using were negative or positive and use the correct function. I did try to find a solution for you that would work with current macros but unfortunately it also fails with negatives. The function is (a - (a % b)) / b . Where a is your original divisor or numerator and b is your dividend or denominator. The percent sign (%) is the modulus operator and yes that does work in Roll20 macros. If you had access to javascript then you could do either ~~(a / b) or (a / b >> 0) . They both take advantage of bit manipulation and the first is faster than the second. Doing roughly 900 million tests, The first one which is a double negation was about 5% faster than the second one which is a Right Shift operation. That being said, the Right Shift operation which was the slower of those two was 11% faster than the Subtraction Remainder shown above. If your values aren't generated from a division that you do yourself and you need to just truncate them then I think you would have to have a trunc(x) function, just like we have ceil(x) and floor(x). The truc(x) function would call floor(x) if the parameter is positive and ceil(x) if it were negative. I hope this gains some traction, it could be very helpful for macro writers.
It would be immensely helpful, in particular in systems where the result of a skill check can be both negative and positive. The macro I have at the moment (which is essentially what I'd truncate if available) is "/r (?{Final check value}-d100)/10". This macro lets you input your check value (which, for example, could be 40). It then subtracts your roll value and divides the value by ten. What we're interested in is the amount of 10's between the roll and the check value. If we roll 60, that's -2 tens, thus 2 degrees of failure. 20 would be two degrees of success. As it stands, when the result becomes -2.5 or 2.5, or similar, we have to take a mental step to remove the decimals, essentially truncating the result ourselves. While certainly doable and not very hard at all, macros are all about simplifying things like this. I can't imagine it being terribly difficult to add trunc() as a function either, as you said it simply requires a logical operator not available in macros. I whipped up some quick and dirty code below, in no language in particular, showing how I'd do it if I had access to proper coding in Roll20. if val>0 then floor(val) elseif val<0 then ceil(val)