• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Solved] Rounding stopped working out of the blue

Status
Not open for further replies.

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,539
I'm a bit confused, everything was working just earlier today and now suddenly my numbers stopped rounding.

This still works for whatever reason:
  • Untitled Trigger 001
    • Events
    • Conditions
    • Actions
      • Set tempReal = (0.50 + ((Real(ShopTotalItems[ShopTracker])) / (Real(ShopItemsPerPage[ShopTracker]))))
      • Set ShopMaxPages[ShopTracker] = (Integer(tempReal))
And this one fails to round. I tested it ingame and it says that tempReal is equal to 2.70, but when I convert it back to an Integer it doesn't round up and goes back down to 2. Any ideas?

  • Untitled Trigger 002
    • Events
    • Conditions
    • Actions
      • Set CategoryTotalItems[unitCustomValue] = (1 + (CategoryEndRange[unitCustomValue] - CategoryStartRange[unitCustomValue]))
      • Set tempReal = (0.50 + ((Real(CategoryTotalItems[unitCustomValue])) / (Real(ShopItemsPerPage[unitCustomValue]))))
      • Set ShopMaxPages[unitCustomValue] = (Integer(tempReal))
EDIT: Found a solution. Posted it in the comments below.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
And this one fails to round. I tested it ingame and it says that tempReal is equal to 2.70, but when I convert it back to an Integer it doesn't round up and goes back down to 2. Any ideas?
Which is the right answer? because the initial value was 2.20 which rounds down to 2...

tempReal would need a value of 3.00 which means an initial value of 2.50 which rounds up to 3. Conversion from real to integer simply truncates fractional precision, hence one adds 0.5 to it before doing so in order to have it round towards nearest rather than always down. This applies to positive real values only, negative ones would need to subtract 0.5 from it for correct nearest rounding.
 

Uncle

Warcraft Moderator
Level 64
Joined
Aug 10, 2018
Messages
6,539
Which is the right answer? because the initial value was 2.20 which rounds down to 2...

tempReal would need a value of 3.00 which means an initial value of 2.50 which rounds up to 3. Conversion from real to integer simply truncates fractional precision, hence one adds 0.5 to it before doing so in order to have it round towards nearest rather than always down. This applies to positive real values only, negative ones would need to subtract 0.5 from it for correct nearest rounding.

Ah, I see, it must have been coming out to at least 2.50 and rounding up to 3.00. I guess I changed a number somewhere. I just got confused when one minute everything worked and the next it stopped, I could've sworn I didn't change anything.

Anyway, I came up with this ugly solution. I think it works (I really can't do math to save my life).
Is there an easier way to do this in GUI?

  • Untitled Trigger 001
    • Events
    • Conditions
    • Actions
      • Set tempR[1] = (0.50 + ((Real(ShopTotalItems[ShopTracker])) / (Real(ShopItemsPerPage[ShopTracker]))))
      • Set tempInt[1] = (Integer(tempR[1]))
      • Set tempR[2] = (Real(tempInt[1]))
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (tempR[1] - tempR[2]) Greater than 0.50
        • Then - Actions
          • Set tempInt[1] = (tempInt[1] + 1)
        • Else - Actions
      • Set ShopMaxPages[ShopTracker] = tempInt[1]
So if ShopTotalItems = 22 and ShopItemsPerPage = 10
Then the equation would go ((22/10) + 0.50) = 2.70
Then we convert that to an Integer so it's rounded down to 2
Then we convert 2 to a real 2.00
Then we subtract 2.70 by 2.00 giving us 0.70
Then we check if 0.70 is > than 0.50 and if it is we increase 2 by 1 to get our final answer.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
Anyway, I came up with this ugly solution. I think it works (I really can't do math to save my life).
Is there an easier way to do this in GUI?
The easier way is...

Set tempInt[1] = (((ShopTotalItems[ShopTracker]) + (ShopItemsPerPage[ShopTracker]) - 1) / (ShopItemsPerPage[ShopTracker])))

No conversion to real needed, no tests needed. Entirely integer mathematics and likely more correct than your current solution.

If ShopTotalItems is an exact multiple of ShopItemsPerPage then it is basically ShopTotalItems / ShopItemsPerPage. However if ShopTotalItems is even 1 more than that it will result in always rounding up to give the correct required page amount.
 
Status
Not open for further replies.
Top