Warcraft III resources & community, 2003–2006 · archived

Math and String JASS function pack

not rated
Submitted by [XGM]MarkizCalculations0 downloads
Script which I made for my own needs.
It "can":
- Convert numbers in different numeration notations
(currently supports decimal,binary and hexadecimal numbers)
- Do different operations with strings
And other...
version: 0.75b

Source

//***************************************************************************
//*
//*  Custom Script Code
//*
//***************************************************************************
//****************************
//*     Math and String      *
//*      Function Pack       *
//****************************
//* Copyright (C)Markiz 2004 *
//****************************
//*        Attention!        *
//* ======================== *
//* If you want to use this  *
//* function Pack or any of  *
//* components, you MUST wri-*
//* te my name in CREDITS    *
//****************************
//===========================================
function Symbol takes string S,integer pos returns string
 return SubStringBJ(S,pos,pos)
endfunction
//===========================================
function Pos takes string source,string what returns integer
local integer l=StringLength(what)
local integer l2=StringLength(source)
local integer i=0
local boolean b=false
 loop
  exitwhen (i==l2) or b
  set i=i+1
  if SubStringBJ(source,i,i+l-1)==what then
   set b=true
  endif
 endloop
 if not b then
  return 0
 else
  return i
 endif
endfunction
//===========================================
function ReverseS takes string s returns string
local string z=""
local integer i=0
 loop
  exitwhen i==StringLength(s)
  set i=i+1
  set z=z+Symbol(s,StringLength(s)-i+1)
 endloop
 return z
endfunction
//===========================================
function Trunc takes real R returns integer
local string s=R2S(R)
local integer i=Pos(s,".")
 return S2I(SubStringBJ(s,1,i-1))
endfunction
//===========================================
function Percent takes real what, real from returns integer
 return Trunc(what/(from/100))
endfunction
//===========================================
function PowR takes integer M,integer N returns real
 if M==0 then
  return 0.0
 elseif N==0 then
  return 1.0
 endif
 if N<0 then 
  return PowR(M,N+1)/M
 elseif N>0 then
  return M*PowR(M,N-1)
 endif
 return 100.0
endfunction
//===========================================
function PowI takes integer M,integer N returns integer
local integer i=0
local integer X=1
 loop
  exitwhen i>=N
  set i=i+1
  set X=X*M
 endloop
 return X
endfunction
//===========================================
function Bin2Dec takes string Bin returns integer
local string base="01"
local integer Dec=0
local integer i=StringLength(Bin)+1
local integer j=0
 loop
  exitwhen i==1
  set i=i-1
  set j=j+1
  set Dec=Dec+(Pos(base,Symbol(Bin,i))-1)*PowI(2,j-1)
 endloop
 return Dec
endfunction
//===========================================
function Dec2Bin takes integer Dec returns string
local string base="01"
local string Bin=""
local integer i=0
local integer X=Dec
 loop
  exitwhen Trunc(X/2)==0
  set i=i+1
  set Bin=Bin+Symbol(base,(X-Trunc(X/2)*2)+1)
  set X=Trunc(X/2)
 endloop
 set Bin=Bin+I2S(X)
 return ReverseS(Bin)
endfunction
//===========================================
function NOD takes integer M,integer N returns integer
 if M==N then
  return M
 elseif M>N then
  return NOD(M-N,N)
 else
  return NOD(M,N-M)
 endif
endfunction
//===========================================
function NOK takes integer M,integer N returns integer
 return (M*N)/NOD(M,N)
endfunction
//===========================================
function Dec2Hex takes integer Dec returns string
local string base="0123456789ABCDEF"
local string Hex=""
local integer i=0
local integer X=Dec
 loop
  exitwhen Trunc(X/16)==0
  set i=i+1
  set Hex=Hex+Symbol(base,(X-Trunc(X/16)*16)+1)
  set X=Trunc(X/16)
 endloop
 set Hex=Hex+I2S(X)
 return ReverseS(Hex)
endfunction
//===========================================
function Hex2Dec takes string Hex returns integer
local string base="0123456789ABCDEF"
local integer Dec=0
local integer i=StringLength(Hex)+1
local integer j=0
 loop
  exitwhen i==1
  set i=i-1
  set j=j+1
  set Dec=Dec+(Pos(base,Symbol(Hex,i))-1)*PowI(16,j-1)
 endloop
 return Dec
endfunction
//===========================================
function Hex2Bin takes string Hex returns string
 return Dec2Bin(Hex2Dec(Hex))
endfunction
//===========================================
function Bin2Hex takes string Bin returns string
 return Dec2Hex(Bin2Dec(Bin))
endfunction

Comments

6
... this is to confusing - jass makes no sence to me and I need a load script so badley, anybody think they can help me? PS this is pretty cool, I can't figure out what it does, but it's way better than I can do. PPS I don't mean to be an idiot, but whats a checksum?
Just a quick comment on your PowR function. It should probably be rewritten as:

function PowR takes integer M,integer N returns real  
   if M==0 then  
      return 0.0 
   elseif N==0 then  
      return 1.0 
   endif  
   if N<0 then  
      return PowR(M,N+1)/M 
   endif
   // If we have made it to this point, N >= 0
   return M*PowR(M,N-1)  
endfunction

This way you don't need that useless return 100.0 line at the end of the function.
then just use I2R

I don't know surely but...doesn't it ROUND numbers?
What will R2I(0.51) return?
0 or 1?
Trunc is removing decimals? then just use I2R
I2R(value+0.5)

???
And if value=0.75 for example?
Trunc will return 0 and I2R(value+0.5) will return 1.
I think there was a int2base function already

Really??? I had an idea to wirte such a function :D

BTW, sorry for NOD and NOK functions, i should have renamed them (NOD = greatest common divisor, NOK = smalles common aliquot, e.g.:
NOD(62,34)= 2, NOK(62,34)=1054
I think that I2R(value+0.5) can replace your trunc function

The dec hex functions are welcome, though I think there was a int2base function already, I think it would be better if you splitted them instead of submitting all of them as a whole script