Thread Tools Display Modes
11-24-13, 12:47 PM   #1
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Need help with a math function

Hi,

I need to compare a number with a list of numbers and work out which one of the numbers in the list is closest to the one I want to compare. If there are two numbers that have the same amount of difference then pick the higher number. All numbers in this list will be unique.

Is there a function that can help me? I need recommendations on what to use or some help as I'm not brilliant with programming.

Basically I have this:
currentIndex = GetCurrentResolution()

and I need to compare the currentIndex with a list of numbers {7, 11, 12, 13, 15, 16}

Thank you for reading!
  Reply With Quote
11-24-13, 01:24 PM   #2
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Why do you exactly need this?
  Reply With Quote
11-24-13, 01:30 PM   #3
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Looks pretty straightforward algorithm to me... go over the list, compare the numbers, save the best fit
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!
  Reply With Quote
11-24-13, 01:33 PM   #4
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Originally Posted by Malsomnus View Post
Looks pretty straightforward algorithm to me... go over the list, compare the numbers, save the best fit
Is there some sort of function I could use to see if one number is closer to a number than another number?
Probably does not help that I have a massive head ache at the moment lol

EDIT: Thank you Resike! I will try using that

EDIT2: No that does not work Resike. Will look into another solution

Last edited by Mayron : 11-24-13 at 01:50 PM.
  Reply With Quote
11-24-13, 01:57 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
If this were me, I would just put the resolutions in a dropdown menu.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
11-24-13, 02:08 PM   #6
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Originally Posted by Seerah View Post
If this were me, I would just put the resolutions in a dropdown menu.
Well sadly I'm not you. I'm not that advanced with Lua yet haha. Otherwise yes that would solve everything but the way my addon works. Not sure how I would add a drop down box onto the artwork.
  Reply With Quote
11-24-13, 01:31 PM   #7
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Originally Posted by Resike View Post
Why do you exactly need this?
So that it finds your current resolution and sees whether it is in the list of resolutions that the UI supports and if not then it picks the closest resolution to yours and sets the UI up accordingly. So that it may only need slightly adjusting.
  Reply With Quote
11-24-13, 01:32 PM   #8
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Lua Code:
  1. local math = math
  2.  
  3. local x = 14
  4. local y = {7, 11, 12, 13, 15, 16}
  5.  
  6. function ReturnStuff(n, t)
  7.     local closest = t[#t]
  8.     local closestId
  9.     for i = 1, #t do
  10.         if closest >= math.abs(t[i] - n) then
  11.             closest = math.abs(t[i] - n)
  12.             closestId = i
  13.         end
  14.     end
  15.     return t[closestId]
  16. end
  17.  
  18. print(ReturnStuff(x, y))

Edit: Try this.

Last edited by Resike : 11-25-13 at 07:47 AM.
  Reply With Quote
11-24-13, 02:22 PM   #9
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Originally Posted by Resike View Post
Lua Code:
  1. --Code

Edit: Try this.
Thank you very much for your work! That works perfectly Really appreciate your help!
  Reply With Quote
11-24-13, 05:40 PM   #10
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Mayron View Post
Thank you very much for your work! That works perfectly Really appreciate your help!
Actually that code has errors in it. Other than the obvious, it assumes that the array you are passing in is sorted which was never a stated condition. This will fix the errors as well as that assumption:
Code:
local math_abs = math.abs

local resArray = { 7, 11, 12, 13, 15, 16 }
local resIndex = GetCurrentResolution()

local function GetClosestNumber(num, array)
    local id = 1
    local closest = math_abs(array[id] - num)
    for index = 2, #array do
        local delta = math_abs(array[index] - num)
        if delta < closest or (closest == delta and array[index] > array[id]) then
            closest, id = delta, index
        end
    end
    return array[id]
end

print(GetClosestNumber(resIndex, resArray))
If the array will always be pre-sorted then this is what you want:
Code:
local function GetClosestMatch(num, array)
    local id = 1
    local closest = math_abs(array[id] - num)
    for index = 2, #array do
        local delta = math_abs(array[index] - num)
        if delta < closest then
            closest, id = delta, index
        elseif delta == 0 or closest == delta then
            id = index
            break
        else
            break
        end
    end
    return array[id]
end

Last edited by Vrul : 11-25-13 at 09:05 AM. Reason: Fixed an error
  Reply With Quote
11-25-13, 08:29 PM   #11
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
I meant the bit about the assumption that the array is sorted. I don't see anything here that the sorting of the array would have any impact on:
Lua Code:
  1. local math = math
  2.  
  3. local x = 14
  4. local y = {7, 11, 12, 13, 15, 16}
  5.  
  6. function ReturnStuff(n, t)
  7.     local closest = t[#t]
  8.     local closestId
  9.     for i = 1, #t do
  10.         if closest >= math.abs(t[i] - n) then
  11.             closest = math.abs(t[i] - n)
  12.             closestId = i
  13.         end
  14.     end
  15.     return t[closestId]
  16. end
  17.  
  18. print(ReturnStuff(x, y))
This is the snippet you were commenting, yes? Pretty sure that's how it looked when I first posted bar the table -> t.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
11-25-13, 08:58 PM   #12
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Lombra View Post
I meant the bit about the assumption that the array is sorted. I don't see anything here that the sorting of the array would have any impact on:
Reverse the sample array and you will get 13 instead of 15, which is not the desired stated behavior.

Originally Posted by Lombra View Post
This is the snippet you were commenting, yes? Pretty sure that's how it looked when I first posted bar the table -> t.
The n was instead the variable x from outside the function, making the passed value to search for be ignored. Sure the code as posted worked but taking the function, the whole point of this thread, and pasting it into another addon would not work as intended.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Need help with a math function


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off