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 .
×
Create a free account

Multiplication Error in Macros

I use the initiative tracker in my games. If two people have the same initiative, their initiative modifier is the tie breaker. (Higher mod wins.) Standard pathfinder stuff. With my macro, it multiplies their init modifier by 1.01. This way a roll of 3 would come out as [[d20+3.03]] At least, that's the idea. However, this keeps happening: It still functions the same, but it spams the turn tracker awfully. I tried to work around by rounding, so my current macro looks like this: /em will proceed at initiative [[d20+round(@{selected|init}*101)/100 &{tracker}]] The rounding helped. It happens about once every 30 rolls or so, instead of once every 3-4. But that's still bizarre. We were joking today that the Roll20 servers must be running on old Pentium 1 processors. In any case, however you can help would be awesome.
The forum killed the resolution, so I grabbed a piecemeal screenshot. I actually didn't know that functionality existed :) Downside was no mouse. So you can't see the formula. But I gave it to you up there.
1422422036
Gen Kitty
Forum Champion
That's funny ^_^ Query: What happens when you divide the init bonus by 100 and then add it? See if that causes the same error?
1422424685
Lithl
Pro
Sheet Author
API Scripter
This is an artifact of how computers handle fractional numbers. It's the same concept as how you can't represent 1/3 perfectly with decimals (0.333... repeating), only in base-2 instead of base-10. For example, 1/10 (0.1 in base-10) becomes, essentially, 0.111... repeating in base-2. You may be able to get the same mathematical result with a different set of operations that gets rounded differently at the binary level, so that you don't have this problem. Try: d20 + @{selected|init} + @{selected|init} / 100
huh. 1/100 = 1/110010, which would be 0.000001...grabs the back of an envelope...then works it out to 12 places and says it's too late to be doing binary math on an envelope. But I will try the workaround. Makes good sense. Though I'm not sure how that will be any better, since I'm still dividing by 100, I'll give it a go. Thanks! Stephen
1422463125

Edited 1422463144
Stephen Koontz
Forum Champion
Marketplace Creator
Sheet Author
API Scripter
Compendium Curator
Stephen, as Brian said that's what's called a rounding error and it's a common part of programming languages. It's not something that's a bug with Roll20. I'm going to move this post to the Specific Use Questions forum where I'm sure lots of people will tell you the math to prevent rounding errors. Our community is helpful and awesome.
The "Specific Use Questions" folks need not bother; thanks though! Brian's and GenKitty's workaround does fine. I don't know why I didn't think to do it that way (although I had no reason to believe that it would change the rounding issue.) I just thought you might want to know that your code was throwing rounding errors so you could tweak it and capture them. You don't want to do that, so it's cool.