• 🏆 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!

Integer/Real conversion weird bug

Status
Not open for further replies.
I will just post that out here. That's kinda strange about JASS.
I was coding Ghoul boss for TKoK. And I've loled along with IyD (TGM Team's algorithmizator and balancer) from these things.
To start off, here is the code:

JASS:
    private function P1_TimerCallback takes nothing returns nothing
        set P1_TimeRemaining = P1_TimeRemaining - .04
        set P1_TimePassed    = P1_TimePassed    + .04
        
        if I2R(R2I(P1_TimePassed/P1_MiniSpawnRateBuff)) == P1_TimePassed/P1_MiniSpawnRateBuff then
            if P1_MiniSpawnRate > 1 then
                set P1_MiniSpawnRate = P1_MiniSpawnRate - 1
                call DisplayTimedTextToPlayer(GetLocalPlayer(),0 , 0, 6, UNIT_MESSAGE_FONT_COLOR+"Ghoul:|r Come to the lunch!")
            endif
            call UnitAddBuff(Ghoul,Ghoul,BUFF_TYPE_FEAST,BUFF_PERMANENT)
        endif
        
        if P1_TimeRemaining <= 0 then
            set P1_TimeRemaining = 0
            call PauseTimer(P1_Timer)
            // Also delete all minis!
            call CleanUpBossUnits(P1_MinisGroup)
            // Phase 2 begins here.
            if not isDefeated then
                set Phase = 2
                call P2_Execute.execute()
            endif
        endif
    endfunction

The code is pretty simple and must work, huh? But no! This condition: I2R(R2I(P1_TimePassed/P1_MiniSpawnRateBuff)) == P1_TimePassed/P1_MiniSpawnRateBuff never fires. I was surprised actually, I always thought that R2I works as trunc function in different programming languages. What Trunc actually does? It converts real to integer by truncating every number after floating point. But R2I just rounds the number down. It's okay with me, it must work well then. But the rounding down there is something weird. For example, R2I(1.000) will return 0.000. Lol what?
It was confirmed with the debug: call BJDebugMsg(R2S(I2R(R2I(P1_TimePassed/P1_MiniSpawnRateBuff)))+ " == " + R2S(P1_TimePassed/P1_MiniSpawnRateBuff))
Along every other variants, it shown me 0.000 == 1.000. And this one is wrong because rounding down is nothing good like that. It rounded 1.000 down to 0.000, but it must stay 1.000 because 1 > 0.
That was just for you to think that over and not getting caught by this bug.
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
The chance to hit 1 with GetRandomReal(0, 1) would be tiny anyway.

Mathematically speaking the chance is zero, even if you might actually get that value. Thats why you use probability densities or quantiles for continuous data instead of the probabilities directly (as you can do with discrete data).
Of course, when it comes to numerical computing nothing is truely continuous, but the rules are still a very good approximation.

http://en.wikipedia.org/wiki/Probability_density_function
 
Status
Not open for further replies.
Top