• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Math in Jass

Status
Not open for further replies.
Level 19
Joined
Oct 12, 2007
Messages
1,821
Small question about math in english.
I don't know the term for doing something like this:

X * X
or X * X * X
as in X^2 or X^3 etc..

You know what I mean?:D

And if you do, what's the Jass term for it?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
dont use power unless there is no other way to get the number
if you have 5 and you need to make it to the power of 3 or whatever, just use 5*5*5 and not power
the reason is that it is very slow function, and thats because it must be able to handle requests like Pow(74.11, 12.78) etc
 
Level 19
Joined
Oct 12, 2007
Messages
1,821
Hmm,
Well I gotta make negative values into positive values So I gotta do SquareRoot(Pow(x, 2))
Guess I can just do SquareRoot(x*x) then.

Thanks.
 
Level 19
Joined
Oct 12, 2007
Messages
1,821
Well, My X integer can be negative or positive, however I gotta make it positive at all times.
What do you suggest doing then? Since you say SquareRoot also isn't a good option for me.
IAbsBJ(integer)?? Is that doing the thing I need?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
JASS:
function IAbsBJ takes integer a returns integer
    if (a >= 0) then
        return a
    else
        return -a
    endif
endfunction

it does exactly what you want and it should be faster compared to stuff like SquareRoot or Pow even through it is BJ and Pow/SqRt are natives
 
Level 19
Joined
Oct 12, 2007
Messages
1,821
JASS:
function IAbsBJ takes integer a returns integer
    if (a >= 0) then
        return a
    else
        return -a
    endif
endfunction

it does exactly what you want and it should be faster compared to stuff like SquareRoot or Pow even through it is BJ and Pow/SqRt are natives

I see!
Thanks!
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
it does exactly what you want and it should be faster compared to stuff like SquareRoot or Pow even through it is BJ and Pow/SqRt are natives
I would not blindly say that. Unless you have done benchmarks it is quite possible that the power function would be faster than a custom 1 dimension abs function. Making a custom function call is considerably slow due to extensive name lookups and stack interaction while working out the power using floating points might be highly optimized in assembly.

Infact you can compute powers so fast that doing over 1,000,000 double (twice the precision of floats or WC3 real) power calculations in JAVA using random double values (generated in the range of 0.0 and 1.0) and summing the results only takes 0.289519014 seconds. I will repeat again, that is a million double precision IEEE compliant power calculations in under 0.289519014 and it is not only doing those (computes random doubles as well as virtual machine maintenance).

I doubt you can run a million lines of JASS in a second.

Code:
public class Test {

	public static void main(String[] args) {
		double timer = System.nanoTime();
		double a = 0;
		for(int i = 0 ; i < 1000000 ; i+= 1){
			a+= Math.pow(Math.random(), Math.random());
		}
		System.out.println(a);
		System.out.println((System.nanoTime()-timer)/1000000000);
	}

}
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
i did not know u could do x = -x but i would think it is if ur able to. i always did x = x * 1 never thought about trying x = -x. both r faster than the absolute value bj function tho

no, thats exactly what the BJ function does. Dont forget that you ONLY need to do "x = -x" if x is negative.
And im pretty sure "x = -x" is better than "x = x * -1".

I would not blindly say that. Unless you have done benchmarks it is quite possible that the power function would be faster than a custom 1 dimension abs function. Making a custom function call is considerably slow due to extensive name lookups and stack interaction while working out the power using floating points might be highly optimized in assembly.

Infact you can compute powers so fast that doing over 1,000,000 double (twice the precision of floats or WC3 real) power calculations in JAVA using random double values (generated in the range of 0.0 and 1.0) and summing the results only takes 0.289519014 seconds. I will repeat again, that is a million double precision IEEE compliant power calculations in under 0.289519014 and it is not only doing those (computes random doubles as well as virtual machine maintenance).

I doubt you can run a million lines of JASS in a second.

Code:
public class Test {

	public static void main(String[] args) {
		double timer = System.nanoTime();
		double a = 0;
		for(int i = 0 ; i < 1000000 ; i+= 1){
			a+= Math.pow(Math.random(), Math.random());
		}
		System.out.println(a);
		System.out.println((System.nanoTime()-timer)/1000000000);
	}

}

what the hell do u want again?
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
what the hell do u want again?
People to not spread lies such as the power of floating points being a costly computation. If the function is slower (which I doubt when compared to the abs function) then that is because of the JASS wrappers which drown out the actual computation of the power. Yes they are slow compared to basic integer operations (like all floating point operations) but they are still entire orders of magnitude faster than the JASS interpreter, so much so that you could ignore the computation entirly.
 
Level 19
Joined
Oct 12, 2007
Messages
1,821
I've got another math based question.

A player will have an integer with one of 8 numbers: 1, 2, 4, 8, 16, 32 ,64 or 128.
In the map there are items that are only useable by players with a specific id. This is registred in an other integer.
So let's say I've got an item with that integer being 35. That means, player 32, 2 and 1 can use the item.
Other example: item with it's integer being 140 would mean player 128, 8 and 4 can use it.

Now my question.
How can I create a check in an 'if, then, else' form that checks if the player is able to use the item? The variables are called Player_Id and Item_Id.
This with the event being Unit uses item. So before the item function starts running, I want to check if the item id suits the player or not.

Is my question clear, or should I rephrase myself?
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
u can do:

JASS:
if AND32(Item_Id, Player_Id) > 0 then
  ...
else
  ...
endif
using http://www.hiveworkshop.com/forums/jass-resources-412/snippet-byte-222868/

The idea is based on bitwise operations, lets assume Item_Id = 140 and Player_Id = 8, then you write down the binary representation and do the bitwise AND (&).:
140 = 0b10001100, 8 = 0b00001000
Code:
  0b10001100
& 0b00001000
= 0b00001000

This simply checks if the bit specified by the second argument is set in the first argument. Since the bitwise operator (&) is not available in Jass you have to use workarounds, like that AND32 Library. http://en.wikipedia.org/wiki/Bitwise_operation
 
Level 28
Joined
Jan 26, 2007
Messages
4,789
I don't know if this is the most efficient way to go about it, but I believe something like this should work:
JASS:
local integer i = 8

loop
    exitwhen i == 0 or ItemVar == 0
    set i = i-1
    if ItemVar <= PowersOf2[i] then
        if GetTriggerPlayer() == PlayerVar[PowersOf2[i]] then
            // Can be used
            set i = 0
        endif
        set ItemVar = ItemVar - PowersOf2[i]
    endif
endloop

ItemVar is the integer stored in the item, say 140.
PlayerVar[X] is the player stored in either 1, 2, 4, ..., 128
PowersOf2 is simply a variable that stores the powers of 2 :) (so PowersOf2[4] would be 24 = 16).
 
Level 19
Joined
Oct 12, 2007
Messages
1,821
u can do:

JASS:
if AND32(Item_Id, Player_Id) > 0 then
  ...
else
  ...
endif
using http://www.hiveworkshop.com/forums/jass-resources-412/snippet-byte-222868/

The idea is based on bitwise operations, lets assume Item_Id = 140 and Player_Id = 8, then you write down the binary representation and do the bitwise AND (&).:
140 = 0b10001100, 8 = 0b00001000
Code:
  0b10001100
& 0b00001000
= 0b00001000

This simply checks if the bit specified by the second argument is set in the first argument. Since the bitwise operator (&) is not available in Jass you have to use workarounds, like that AND32 Library. http://en.wikipedia.org/wiki/Bitwise_operation

So with that snippet you're talking about hmm.. Lemme check if I get it.

"AND32(140, 128)" would return 1
"AND32(120, 128)" would return 0
"AND32(5, 1)" would return 1
"AND32(5, 2)" would return 0

Right?:D
 
Status
Not open for further replies.
Top