Issue with Sin and Cos

Homer

New Member
Reaction score
2
When ever i convert the data to integer it does not work and sets it to 0.

What am i doing wrong here?
JASS:
function Trig_maze_init_Actions takes nothing returns nothing
//local integer array g_grid
local integer array start
local integer array end
local integer x = 0
local integer y = 0
local integer i = 0
local integer xtemp = 0
local integer ytemp = 0
local real rad = 0
local boolean array options
local integer optionsused = 0
local boolean success = false
local string str = ""
loop
    exitwhen x > 20
    set y = 0
    loop
        exitwhen y > 20
        set g_grid[x][y] = -1
        set y = y + 1
    endloop
    set x = x + 1
endloop
set start[0] = 0
set start[1] = 0
set x = start[0]
set y = start[1]
set g_grid[x][y] = 0
loop
    set optionsused = 0 
    loop
        set options[0] = false
        set options[1] = false
        set options[2] = false
        set options[3] = false
        loop
            set i = GetRandomInt(0,3)
            exitwhen optionsused>3
            exitwhen options<i> == false
        endloop
        set rad = (i*bj_PI)/2
        set xtemp = R2I(Cos(rad))
        set ytemp = R2I(Sin(rad))
        call BJDebugMsg(I2S(xtemp)+&quot;,&quot;+R2S(Sin(rad)))
        call BJDebugMsg(&quot;rad = &quot;+R2S(rad))
        set success = false
        if (x &lt;=20) then
            if (x &gt;= 0) then
                if (y &lt;=20) then
                    if (y &gt;= 0) then
                        if (g_grid[x+xtemp][y+ytemp]==-1) then
                            set success = true
                            call BJDebugMsg(&quot;TEST&quot;)
                        endif
                    endif
                endif
            endif
        endif
        if (success==false) then
        set options<i> = true
        set optionsused = optionsused + 1
        endif
        exitwhen optionsused &gt; 3
        exitwhen success == true

        endloop
        if success == true then
            set i = i + (R2I(bj_PI)/2)
            set g_grid[x+R2I(Cos(i))][y+R2I(Sin(i))] = 1
            set i = i + ((3*R2I(bj_PI))/2)
            set g_grid[x+R2I(Cos(i))][y+R2I(Sin(i))] = 1
            set x = x + xtemp
            set y = y + ytemp
            set g_grid[x][y] = 0
            call BJDebugMsg(&quot;TEST222&quot;)
        endif
        
        
        exitwhen success == false
endloop
set x = 0
set y = 0
loop
    exitwhen y &gt; 5
    set x = 0
    set str = &quot;&quot;
    loop
        exitwhen x &gt; 20
        set str = str + I2S(g_grid[x][y])
        set x = x + 1
    endloop
    call BJDebugMsg(str)
    set y = y + 1
endloop
endfunction

//===========================================================================
function InitTrig_maze_init takes nothing returns nothing
    set gg_trg_maze_init = CreateTrigger(  )
    call TriggerAddAction( gg_trg_maze_init, function Trig_maze_init_Actions )
endfunction

</i></i>
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
I recommend that you stick with just using reals. There's a lot of problems associated with converting back and forth, if you don't do it correctly.

JASS:
set xtemp = R2I(Cos(rad))
set ytemp = R2I(Sin(rad))


Sine and Cosine always return a number between -1 and 1. Since integers are always round down, it will always be 0. Instead, make xtemp and ytemp reals.
 

Homer

New Member
Reaction score
2
Heres my newest code. Now i read off the values of a "position" 3 times durring the loop. And for some reason the second time its read off it returns -1... Anyhelp?
JASS:
globals
integer array g_grid[20][20]
endglobals
function Trig_maze_init_Actions takes nothing returns nothing
//local integer array g_grid
local integer array start
local integer array end
local integer x = 0
local integer y = 0
local real i = 0
local integer xtemp = 0
local integer ytemp = 0
local real rad = 0
local boolean array options
local integer optionsused = 0
local boolean success = false
local string str = &quot;&quot;
loop
    exitwhen x &gt; 20
    set y = 0
    loop
        exitwhen y &gt; 20
        set g_grid[x][y] = -1
        set y = y + 1
    endloop
    set x = x + 1
endloop
set start[0] = 0
set start[1] = 0
set x = start[0]
set y = start[1]
set g_grid[x][y] = 0

loop
     call PolledWait(.5)
     call SetTerrainTypeBJ( Location(x*125,y*125*-1), &#039;Dlav&#039;, -1, 1, 0 )
     call BJDebugMsg(I2S(x)+&quot;,&quot;+I2S(y))
     call BJDebugMsg(&quot;Value: &quot;+ I2S(g_grid[x][y]))
    set optionsused = 0
    set options[0] = false
        set options[1] = false
        set options[2] = false
        set options[3] = false 
    loop
        
        loop
            set i = R2I(GetRandomInt(0,3))
            exitwhen optionsused&gt;3
            exitwhen options[R2I(i)] == false
        endloop
        set rad = (i*bj_PI)/2
        set xtemp = R2I(Cos(rad)+.5*RSignBJ(Cos(rad)))
        set ytemp = R2I(Sin(rad)+.5*RSignBJ(Sin(rad)))
        if (xtemp&lt;0) then
            call BJDebugMsg(&quot;negative&quot;)
        endif
        //call BJDebugMsg(I2S(xtemp)+&quot;,&quot;+I2S(ytemp))
        //call BJDebugMsg(&quot;rad = &quot;+R2S(rad))
        set success = false
        if (x+xtemp &lt;=20) then
            if (x+xtemp &gt;= 0) then
                if (y+ytemp &lt;=20) then
                    if (y+ytemp &gt;= 0) then
                        if (g_grid[x+xtemp][y+ytemp]==-1) then
                            call BJDebugMsg(&quot;Value: &quot;+ I2S(g_grid[x+xtemp][y+ytemp]))
                            set success = true
                            //call BJDebugMsg(&quot;TEST&quot;)
                        endif
                    endif
                endif
            endif
        endif
        if (success==false) then
            if (options[R2I(i)] == false) then 
                set options[R2I(i)] = true
                set optionsused = optionsused + 1
            endif
        endif
        exitwhen optionsused &gt; 3
        exitwhen success == true

        endloop
        if success == true then
            //set rad = i + (bj_PI)/2
            //set g_grid[x+R2I(Cos(rad)+.5)][y+R2I(Sin(rad)+.5)] = 1
            //set rad = i + (3*bj_PI)/2
            //set g_grid[x+R2I(Cos(rad)+.5)][y+R2I(Sin(rad)+.5)] = 1
            set g_grid[x][y] = 0
            set x = x + xtemp
            set y = y + ytemp
            set g_grid[x][y] = 0
            call BJDebugMsg(&quot;Value: &quot;+ I2S(g_grid[x][y]))
            //call BJDebugMsg(&quot;TEST222&quot;)
        endif
        
        
        exitwhen success == false
endloop
set x = 0
set y = 0
loop
    exitwhen y &gt; 5
    set x = 0
    set str = &quot;&quot;
    loop
        exitwhen x &gt; 20
        set str = str + I2S(g_grid[x][y])
        set x = x + 1
    endloop
    call BJDebugMsg(str)
    set y = y + 1
endloop
endfunction

//===========================================================================
function InitTrig_maze_init takes nothing returns nothing
    set gg_trg_maze_init = CreateTrigger(  )
    call TriggerAddAction( gg_trg_maze_init, function Trig_maze_init_Actions )
endfunction
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
I had a silly math problem with using an integer with a real, an integer is also a real but sometimes it seems war3 have his own math rules ...

I mean instead of using
JASS:
set rad = (i*bj_PI)/2
, you should use that :
JASS:
rad = (i*bj_PI)/2.
.
Or use an integer variable instead of simply "2" (i've never had a problem with a variable but that doesn't mean it is perfectly safe).
At least using I2R should be safe ...
 

Joker(Div)

Always Here..
Reaction score
86
Any sort of mathamatical operation including both integers and reals return reals. No point in using I2R, ever. X*1. is faster than I2R(X). Vex got benchmarks and proved it.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Any sort of mathamatical operation including both integers and reals return reals. No point in using I2R, ever. X*1. is faster than I2R(X). Vex got benchmarks and proved it.


JASS:
scope Test initializer init

private function Display takes nothing returns nothing
    local integer i1 = 180
    local integer i2 = 200
    local integer i3 = 100
    
    call BJDebugMsg(R2S((180/200)*100)) // display 0
    call BJDebugMsg(R2S((180./200)*100)) // display 90
    call BJDebugMsg(R2S((i1/i2)*i3)) // display 0
    call BJDebugMsg(R2S((180./200.)*100.)) // display 90 
    call BJDebugMsg(R2S((I2R(180)/I2R(200))*I2R(100))) // display 90
endfunction

private function init takes nothing returns nothing
    local timer tim = CreateTimer()
    call TimerStart(tim,1.0,false,function Display)
    
endfunction

endscope


EDIT : Ok, i was wrong, hard to explain with my poor English, basically it only "fails" with integers only :
JASS:
call BJDebugMsg(R2S((180/200)*100)) // display 0

JASS:
call BJDebugMsg(R2S((i1/i2)*i3)) // display 0

It's just a thing to know, but it is not a bug.
 

Vestras

Retired
Reaction score
248
> an integer is also a real

No. When JASS is compiled into C++, integer becomes int, real becomes float, string becomes array of char, boolean becomes int (0 = false, 1 = true) and everything else becomes int (proof = a handle is just an id)

Edit: well, logically a whole number (1, 2, 3 so on) will also have ,0000 (1.000, 2.000, 3.000) but I just wanted you to know that it when compiled is what it actually looks like it is.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
> an integer is also a real

No. When JASS is compiled into C++, integer becomes int, real becomes float, string becomes array of char, boolean becomes int (0 = false, 1 = true) and everything else becomes int (proof = a handle is just an id)
ok, that's the reason, thx
 
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