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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top