Author Topic: Torque is weird with maths again  (Read 625 times)

So I attempted writing a function that would return the largest value in a string (and for some reason I feel like it's bad practice to do so, someone said something like that once maybe), and I got it to work, but it only works with positive floats and integers, but not negatives.

function returnLargestValue(%string)
{
   if(!strlen(%string))
      return false;
   %string = trim(%string);
   %count = getWordCount(%string);
   for(%i = 0; %i < %count; %i++)
   {
      %number = getWord(%string, %i);
      %value[%i] = %number;
      if(%value[%i] > %highestNumber)
         %highestNumber = %value[%i];
   }
   return %highestNumber;
}

What's going on here, and how do I fix it? I've tried bitwise or with the function, but that just screws it up entirely. I don't know what else to do.

because %highestNumber isn't defined, it defaults to "", which equates to 0. negative numbers are less than 0, so they won't trigger the if statement. if you change it to if(%value[%i] > %highestNumber || %highestNumber $= "") it should work

Also, %value[%i] = %number; is unnecessary.
Code: [Select]
      %number = getWord(%string, %i);
      if(%number > %highestNumber || %highestNumber $= "")
         %highestNumber = %number;