ROBOFROG
|
  |
| Joined: 04 Jan 2009 |
| Total Posts: 1519 |
|
|
| 06 Jul 2015 11:41 PM |
Basically, I'm reading from a table and am trying to find if it's within +- 8 of another random value being input.
How might I accomplish doing that in terms of an equation?
My current test is this (value is a table iteration value, newvalue is a float) --
if value + 8 <= newvalue + 8 or value - 8 >= newvalue - 8 then
However, since I can't temporarily modify a table value, this will not work. What is another way I can go about this? |
|
|
| Report Abuse |
|
|
maxomega3
|
  |
| Joined: 11 Jun 2010 |
| Total Posts: 10668 |
|
|
| 06 Jul 2015 11:43 PM |
if math.abs (newvalue-value) == 8 then
Hello World! |
|
|
| Report Abuse |
|
|
ROBOFROG
|
  |
| Joined: 04 Jan 2009 |
| Total Posts: 1519 |
|
|
| 06 Jul 2015 11:46 PM |
| Sadly, that won't work. I can't *technically* add or subtract anything to value, since it's part of a table and will return something about being unable to modify a table value. |
|
|
| Report Abuse |
|
|
|
| 06 Jul 2015 11:46 PM |
function Within(num, input) if input+8 >= num and input-8 <= num then return true else return false end end
I didn't test that, in fact I randomly threw together symbols that sounded right as I'm too tired to think. Give it a shot. |
|
|
| Report Abuse |
|
|
ROBOFROG
|
  |
| Joined: 04 Jan 2009 |
| Total Posts: 1519 |
|
|
| 07 Jul 2015 12:05 AM |
| It's not calculating exactly what I needed, but the keyword there is that it's calculating (better than I could get before). I'll mess around with the values and signs to get it correct, I appreciate the help getting me to that point however! |
|
|
| Report Abuse |
|
|
maxomega3
|
  |
| Joined: 11 Jun 2010 |
| Total Posts: 10668 |
|
|
| 07 Jul 2015 02:35 PM |
>function Within(num, input) if input+8 >= num and input-8 <= num then return true else return false end end
you ARE tired
function Within (num, input) return math.abs (num-input) == 8 end
Hello World! |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 02:45 PM |
The reason why you need to pass it through a function is because then you aren't referencing the same item. Exactly why you do this to copy a table;
function copy(t) return t; end
That way you aren't just passing a new reference, it creates a new item within the function's environment.
Current Date; July 7th 2015, 12:45:04 pm |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 04:09 PM |
@Max No I think you're tired...
That would never work LOL
@KOT You know nothing about tables do you... -_- |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 04:12 PM |
| @KOT, that doesn't copy the table, that just returns the one you passed along. |
|
|
| Report Abuse |
|
|
lordrambo
|
  |
| Joined: 16 Jun 2009 |
| Total Posts: 20628 |
|
|
| 07 Jul 2015 04:15 PM |
"if math.abs (newvalue-value) == 8 then" this does not modify anything, you're not writing to the table or any of it's elements |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 07:15 PM |
Oops, I assumed it created a copy of the table within the lower scope, but I guess it's all still the same with references to tables. Only works with non-table/userdata objects.
And Warspy, just like you were tired, as was I, you don't see me saying you know nothing about efficiency, now do you?
Current Date; July 7th 2015, 5:15:45 pm |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 07:44 PM |
The only data types that are referenced in Lua are tables, functions, threads, and userdata. Everything else is passed by value, including values within tables.
local t = {1, 2} print( t[1] + t[2] ) --> 3 print( t[1] ) --> 1
This also applies if you save the value into a variable.
local t = {1, 2} local a, b = t[1], t[2] print( a + b ) --> 3 print( t[1] ) --> 1
As for determining whether two numbers are within a certain range of each other, you could use math.abs combined with subtraction.
function NumsWithinRange(N1, N2, Range) return math.abs(N1-N2) <= Range end
print( NumsWithinRange(2, 10, 8) ) --> true print( NumsWithinRange(6, -3, 8) ) --> false (difference is 9 units) |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 09:46 PM |
"And Warspy, just like you were tired, as was I, you don't see me saying you know nothing about efficiency, now do you?"
I didn't complain about efficiency. I simply claimed that you knew nothing of tables. It doesn't matter the scope or environment. e.g.
a = 5 b = a a = 6 print(a, b)
a = {5} b = a a[1] = 6 print(a[1], b[1])
Scopes do not matter here. Nor do function environments. Variables and variables. If we had upvalues and such it would not matter either. |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 09:49 PM |
"I didn't complain about efficiency. I simply claimed that you knew nothing of tables. It doesn't matter the scope or environment. "
I think he was referring to some event I missed in which you were tired and really messed up your efficiency. He is saying he didn't complain about that, but now it is the other way around and you ARE harassing him. |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 09:59 PM |
| He implied that I HAD said something about efficiency. |
|
|
| Report Abuse |
|
|
maxomega3
|
  |
| Joined: 11 Jun 2010 |
| Total Posts: 10668 |
|
|
| 07 Jul 2015 10:14 PM |
War, what are you talking about? That works!
Hello World! |
|
|
| Report Abuse |
|
|
|
| 07 Jul 2015 10:16 PM |
No, you only checked if the difference was 8, no ranges at all.
-_- |
|
|
| Report Abuse |
|
|
maxomega3
|
  |
| Joined: 11 Jun 2010 |
| Total Posts: 10668 |
|
|
| 07 Jul 2015 10:17 PM |
oh, sue me
function Within (num, input) return math.abs (num-input) <= 8 end
Hello World! |
|
|
| Report Abuse |
|
|
ROBOFROG
|
  |
| Joined: 04 Jan 2009 |
| Total Posts: 1519 |
|
|
| 07 Jul 2015 11:03 PM |
To clear things up, I solved the issue a few hours ago by using the line --
if math.abs(value - newvalue) >= threshold then
I know it was referenced before now, and I appreciate the continued help (whether or not it was for me). |
|
|
| Report Abuse |
|
|
maxomega3
|
  |
| Joined: 11 Jun 2010 |
| Total Posts: 10668 |
|
|
| 07 Jul 2015 11:04 PM |
ha, rekt war hehehehehehehehe
Hello World! |
|
|
| Report Abuse |
|
|