# Proximity checking for number values?

**URL:** <https://forum.kirupa.com/t/proximity-checking-for-number-values/186459>\
**Category:** flash\
**Created:** [April 21, 2006, 11:47am UTC](https://forum.kirupa.com/t/proximity-checking-for-number-values/186459 "2006-04-21T11:47:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CEOofAEP](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@CEOofAEP](https://forum.kirupa.com/u/CEOofAEP)\
**Post date:** [April 21, 2006, 11:47am UTC](https://forum.kirupa.com/t/proximity-checking-for-number-values/186459/1 "2006-04-21T11:47:40Z")

</div>

Greetings all.

It’s been a while but I didn’t get to do too much flashy stuff lately so I kind of neglected the Kirupa world… but now I have another problem so of course I come crawling back 🙂

I have an array with a bunch of numbers in it. No, that’s not the problem yet 😃

Currently, the array contains “10”, “25” and “50”.

There’s a variable number that is divided by 3 and I need to check which of the values of the array is closest to the result of the division.

Say I have the value “130”… it’s divided by 3… so the result is 43.3

Now I need to write a code that compares this value to the array values and returns the one that is closest. Is there a way to do this easily?

I’m thinking maybe take the result of the division and substract the values from the array

```auto
43.3 - 10 = 33.3
43.3 - 25 = 18.3
43.3 - 50 = -6.7

```

and then compare those “remains” for which one is smaller than the others…

By the way- I know I’ve done it before sometime but isn’t there a way / function to automatically make all numbers positive? I’m a bit phased out right now and can’t think of what it was. I know I could add something like “if it’s lesser than zero, multiply it with -1”… but isn’t there something that does that automatically?

Thanks, all 🙂

---

<div class="post-metadata">

**Author:** ![CEOofAEP](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@CEOofAEP](https://forum.kirupa.com/u/CEOofAEP)\
**Post date:** [April 21, 2006, 2:49pm UTC](https://forum.kirupa.com/t/proximity-checking-for-number-values/186459/2 "2006-04-21T14:49:13Z")

</div>

I solved it myself- thanks for reading this anyways. I’ll post my solution later if anyone is interested.

~over.

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 21, 2006, 3:09pm UTC](https://forum.kirupa.com/t/proximity-checking-for-number-values/186459/3 "2006-04-21T15:09:14Z")

</div>

For “the by the way” question: Its Math.abs( expression )

For the first question:  
Your logic is good… to implement it try the following

```auto
function getClosest(a:Number, numbers:Array):Number {
	for (var i = 0; i<numbers.length; i++) {
		if (closest == undefined) {
			closest = [Math.abs(a-numbers*), numbers*];
		} else if (Math.abs(a-numbers*)<closest[0]) {
			closest = [Math.abs(a-numbers*), numbers*];
		}
	}
	return closest[1];
}
trace(getClosest(43.3, new Array(10, 25, 50)));

```

edit: lol
