GML Math help

M

MuddyMuddy

Guest
Hi,
I would like to find the formula for finding a single number (0-9) in another number.

So to identify any number with a ‘9’ in it.
e.g. 9,19,29,39,49,59,69,79,89,90,99.

How can I code this as a formula for any number? Or is it even possible?
 
T

TimothyAllen

Guest
Just the last digit?
Code:
if (number_to_check % 10 == 0)
{
    // last digit is a 9
}
If you need to check any digit use a loop
Code:
var n = number_to_check;

while (n > 0)
{
    if (n % 10 == 9)
    {
        // found a digit that is 9
        break;
    }

    n = n div 10;
}
 
M

MuddyMuddy

Guest
I mean anywhere in the number. So 1907 would trigger it.

Or if im looking for the number 43, the variable 204377 would trigger it.
 

Simon Gust

Member
You could convert the numbers in question to strings, loop every string until you find a "9" inside
Code:
var flag = false;
var str_to_find = string(num_to_find); // input 0...9
var str_to_check = string(num_to_check); // input random number
for (var i = 1; i <= string_length(str_to_check); i++)
{
    if (string_char_at(str_to_check, i) == str_to_find)
    { 
         flag = true;
         break;
    }
}
If the flag is true after this code, you know that the number in question contains the number you're looking for.
 

Simon Gust

Member
I mean anywhere in the number. So 1907 would trigger it.

Or if im looking for the number 43, the variable 204377 would trigger it.
If it's numbers that are more than single digit it gets a little more complicated
Code:
var flag = false;
var str_to_find = string(num_to_find);
var str_to_check = string(num_to_check);
var len = string_length(str_to_find);

for (var i = 1; i <= string_length(str_to_check); i++)
{
    var str = "";
    for (var j = i; j < i+len; j++)
    {
        str += string_char_at(str_to_check, j);
    }
   
    if (str == str_to_find)
    {
        flag = true;
        break;
    }
}
Not sure if that works.
 
T

TimothyAllen

Guest
If you are going to take the performance hit of turning it into a string, use the string functions to look for a sub string. (Would be like one line of code).

Edit: but if you need it to be more efficient, you can still use the method that I posted above. (With modification)
 
Last edited by a moderator:
You could probably get away with just using string_count and transforming them into strings.

Code:
number = 102938;
number_to_check = 93;

if(string_count(string(number_to_check), string(number))) {
  // that number exists SOMEWHERE in the string
} else {
  // that number doesn't exists in that string
}
 
T

TimothyAllen

Guest
You could probably get away with just using string_count and transforming them into strings.

Code:
number = 102938;
number_to_check = 93;

if(string_count(string(number_to_check), string(number))) {
  // that number exists SOMEWHERE in the string
} else {
  // that number doesn't exists in that string
}
similar to the last "string post". Why do extra work? If you are going to use the string method, just check if the sub string exists at all. No reason to count all instances of the sub string.
Code:
var number = 143009;
var sub_number = 43;
var num_exists = string_pos(string(sub_number), string(number));
But if this is happening several times per step, I would not suggest converting to a string. I would write my own script that handles it as numbers. It would be pretty simple
 
Top