Check if a real number is an integer?

Tyrulan

Ultra Cool Member
Reaction score
37
Got a timer loop on periodic (0.1), but every 1 seconds I want to play a sound effect. I have a running total of the timer to add up to 15 seconds but my question is, how can I tell if the total is an integer?

I was thinking maybe modular division...



EDIT: Solved.
 

Tyrulan

Ultra Cool Member
Reaction score
37
But.. (Truncating)

4.300 = 4
4.700 = 4

I only want to check when 4.000 is == 4. And 5.000 == 5. Every 1 second. Yeah?
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
In that case, you more likely want to keep track of ticks with an integer instead of direct time tracking; real operations in warcraft 3 can give whacky results that don't equate to something that they should.
 

Jesus4Lyf

Good Idea™
Reaction score
397
[LJASS]R2I(r)==r[/LJASS] is perfect (edit: well, nearly); it will tell you if it is an integer.

Example:
Case: 4.5
R2I(4.5)==4.5
4==4.5
false

Case: 4.0
R2I(4.0)==4.0
4==4.0
true

Case: 3.99
R2I(3.99)==3.99
3==3.99
false

The only nasty stuff is:
Case: 4.00001
R2I(4.00001)==4.00001
4==4.00001
[LJASS]true // Gogo wc3.[/LJASS]

I think that's how it works, off memory. :)
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
[LJASS]R2I(r)==r[/LJASS] is perfect (edit: well, nearly)

Yes, but real operations unfortunately aren't.

JASS:

scope Reals initializer Init
globals
    private real r = 0
endglobals

private function CB takes nothing returns nothing
    set r = r + .1
    call BJDebugMsg("//===========================")
    call BJDebugMsg(R2S(r))
    if r == R2I(r) then
        call BJDebugMsg("Integer")
    endif
endfunction

private function Init takes nothing returns nothing
    call TimerStart(CreateTimer(),1,true,function CB)
endfunction
endscope


Hence why I'm saying that he should use an integer counter instead of a real and then just use a mod function or something.
 

Tyrulan

Ultra Cool Member
Reaction score
37
Alternatively I could use a buff with a looping sound effect. However the duration of the .wav file is too short so I edited it to be longer. Is there a way to create custom sound sets which I can add into the object editor?
 

Jesus4Lyf

Good Idea™
Reaction score
397

Tyrulan

Ultra Cool Member
Reaction score
37
Just thought of a way of possibly making that work.

The function works fine however the 'r' value being passed in would have to be 1, 2, 3, 4, 5, 6, 7.... etc. I think I'll use a loop to do it.

In effect making a modular division function checking each integer.


Oh, and the callback timer: the condition is never true.
Because:

R2I(1.000) = 0
R2I(2.000) = 1

This works:

JASS:
scope Reals initializer Init
globals
    private real r = 0
endglobals

private function CB takes nothing returns nothing
    set r = r + .1
    call BJDebugMsg("//===========================")
    call BJDebugMsg("Real: " + R2S(r))
    call BJDebugMsg("Trun: " + I2S(R2I(r)))
    if (r - 1) == R2I(r) then
        call BJDebugMsg("Integer")
    endif
endfunction

private function Init takes nothing returns nothing
    call TimerStart(CreateTimer(),1,true,function CB)
endfunction
endscope
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
As it happens, you figured out an extremely interesting solution for the wrong reasons. R2I(1.000) is NOT equal to 0. You can try it out in the editor; just plug 1.000 in for the comparison. It certainly does not become zero. The 1.000 that becomes zero isn't actually 1.000.

The weirdest part is that the comparison comes out successful, since (r-1) in my function block is NOT technically equal to an integer EVER. This is probably similar to what happens in J4L's example of r=R2I(r) behaving strangely.


If you want to see visually what I'm saying, just multiply your reals by 100000 or something when you display them. It's weird.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Long story short use integers.
JASS:
scope Unreals initializer Init
globals
    private integer r = 0
endglobals

private function CB takes nothing returns nothing
    set r = r + 1
    if (r/10)*10 == r then
        call BJDebugMsg("Big fat 10")
    endif
endfunction

private function Init takes nothing returns nothing
    call TimerStart(CreateTimer(),1,true,function CB)
endfunction
endscope

?
 

Tyrulan

Ultra Cool Member
Reaction score
37
Yeah I found it strange as well, but I added the extra debug message to see what the truncated integer would look like. Hence the r-1. Here's my solution to the entire thread.

JASS:
        if (IsInteger(data.TimerIndex)) then
            set i = GetRandomInt(1,3)                    
            if (i == 1) then                
                call PlaySoundOnUnitBJ(gg_snd_MetalLightChopStone1, 100, data.caster)
            elseif (i == 2) then                
                call PlaySoundOnUnitBJ(gg_snd_MetalLightChopStone2, 100, data.caster)
            elseif (i == 3) then                
                call PlaySoundOnUnitBJ(gg_snd_MetalLightChopStone3, 100, data.caster)
            endif
        endif


JASS:
    function IsInteger takes real r returns boolean
        //call BJDebugMsg("Real: " + R2S(r))
        //call BJDebugMsg("Trun: " + I2S(R2I(r)))
        return (r - 1) == R2I(r)
    endfunction
 

Jesus4Lyf

Good Idea™
Reaction score
397
JASS:
function IsInteger takes real r returns boolean
        //call BJDebugMsg("Real: " + R2S(r))
        //call BJDebugMsg("Trun: " + I2S(R2I(r)))
        return (r - 1) == R2I(r)
    endfunction

-->
JASS:
function IsInteger takes real r returns boolean
        return r==R2I(r+0.5)
    endfunction

?
I dunno. Use what you find works today... lol
 

Tyrulan

Ultra Cool Member
Reaction score
37
You don't need to add 0.5, it could be 0.000000000001 indefinitely. The truncated real just can't be let's say, 7.0000 as the R2I(7.00) is 6.
 

uberfoop

~=Admiral Stukov=~
Reaction score
177
You don't need to add 0.5, it could be 0.000000000001 indefinitely. The truncated real just can't be let's say, 7.0000 as the R2I(7.00) is 6.

Wrong, again.

Let me explain myself fully this time so that you don't just ignore me.

I've mentioned time and time again that real operations in warcraft 3 don't return correct values, and somehow you're still convincing yourself that the reason that your solution works is that the R2I of a value that's an integer is equal to that integer minus 1. This is false, and can be very easily demonstrated as false by plug-and-chugging [ljass]call BJDebugMsg(I2S(R2I(2.000)))[/ljass] into your editor. The reason the R2I is returning a value 1 down is that it's truncating. The actual r being plugged in is actually less than the integer you think it is, but you don't notice that because you're only showing the first few decimal points (because you ignored my advice about multiplying by 100000 on the BJDebugMsg's), and Warcraft 3 rounds extreme cases like 4.99999 when R2S is used and would show 5 instead.
Anyway, your integer drops down to the lower value because of correctly-executed truncation, thus forcing you to reduce your r by 1 to make the comparison. Then, through some quirkyness similar to how J4L's comparisons worked (involving values like 4.00001 working for R2I(r)==r), the comparison returned true (as it happens, the r-1 value on the first successful check in my scope is equal to something slightly below zero).
How exactly this quirkiness functions is a mystery to me at the moment, though based on the examples of what works and what doesn't, I wouldn't be surprised if it's a round-then-digit-by-digit-check sort of thing.



Anyway, .00000000000000000...0001 with an arbitrary number of zeroes doesn't work for the reasons I just described; you need a big enough value to force the real above the 1 line, which, for at least the first few successful checks, requires that you have no more than 5 zeroes (Yes, I tested it).
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top