Parcourir la source

[added] simple pow() function to calculate exponents.

Dank il y a 8 mois
Parent
commit
ae4d7ca339
1 fichiers modifiés avec 21 ajouts et 0 suppressions
  1. 21 0
      locations/shortgs.qsrc

+ 21 - 0
locations/shortgs.qsrc

@@ -918,5 +918,26 @@ end
 if $ARGS[0] = 'clamp':
 	result = min(max(ARGS[1], ARGS[2]), ARGS[3])
 end
+
+!! Recursively computes a simple exponent - https://en.wikipedia.org/wiki/Exponentiation_by_squaring
+!! use: func('shortgs', 's_pow', var, exp)
+!! returns var ^ exp
+!! exp must be an integer >= 0
+!! example: func('shortgs', 's_pow', 2, 3)
+if $ARGS[0] = 's_pow':
+	if(ARGS[2] > 10) : msg('Warning: s_pow operations with large exponents may cause integer overflows. <<ARGS[1]>> ^ <<ARGS[2]>>')
+	
+	if(ARGS[2]) < 0:
+		msg('Error: s_pow cannot accept negative exponents! <<ARGS[1]>> ^ <<ARGS[2]>>')
+	elseif ARGS[2] = 0:
+		result = 1
+	elseif ARGS[2] = 1:
+		result = ARGS[1]
+	elseif (ARGS[2] mod 2) = 0:
+		result = func('shortgs', 's_pow', ARGS[1] * ARGS[1], ARGS[2] / 2)
+	elseif (ARGS[2] mod 2) ! 0:
+		result = ARGS[1] * func('shortgs', 's_pow', ARGS[1] * ARGS[1], (ARGS[2] - 1) / 2)
+	end
+end
 --- shortgs ---------------------------------