Page 1 of 2

My old nemesis, math

Posted: Mon Jan 03, 2011 6:45 am
by Taehl
I just need a function that would graph out like this:

Image

In case my graph isn't clear, the line switches from negative to positive at exactly 1/3. Does anyone know how to express that in math terms?

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 6:46 am
by Lap
I've learned to never do coding math at 1:45 am. So many hours wasted that way. We should probably go to bed ;)

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 6:50 am
by Taehl
Thanks, /mum/. Anyone else have an answer?

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 7:00 am
by TechnoCat

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 8:00 am
by Robin
Taehl wrote:In case my graph isn't clear, the line switches from negative to positive at exactly 1/3.
Is that the only requirement you have? Because TechnoCat's suggestion fulfils that requirement, but doesn't look much like your graph otherwise.

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 8:45 am
by kikito
I'd suggest asking the same question on http://math.stackexchange.com/ . Those guys are really good at math (and they answer really fast)

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 9:25 am
by Taehl
Robin wrote:
Taehl wrote:In case my graph isn't clear, the line switches from negative to positive at exactly 1/3.
Is that the only requirement you have? Because TechnoCat's suggestion fulfils that requirement, but doesn't look much like your graph otherwise.
No, it also needs to range from just -.1 to +.1, not from -infinity to +infinity.

Currently I'm kind of cheating around it by going (math.clamp(-1/3, .01/(n-1/3), 1/3))%1, but having the proper function would be better.

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 11:21 am
by giniu
Taehl wrote:I just need a function that would graph out like this [...] the line switches from negative to positive at exactly 1/3. Does anyone know how to express that in math terms?
Maybe something like

Code: Select all

(2-2*math.atan(x-1/3)/math.pi)%2-1
?

You can also use other sigmoid instead of math.atan, might be faster - http://en.wikipedia.org/wiki/Sigmoid_function - for example with x/(1+|x|):

Code: Select all

(2-(x-1/3)/(1+math.abs(x-1/3)))%2-1
You can also avoid using the "%2" part if you use some conditional instruction.

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 2:21 pm
by Taehl
Thank you for the resource, kikito. I quickly got an answer that seems correct. Now I just have to figure out, how the heck do I make this crazy "arccotangent" thing in Lua???

Re: My old nemesis, math

Posted: Mon Jan 03, 2011 2:39 pm
by giniu
Taehl wrote:Thank you for the resource, kikito. I quickly got an answer that seems correct. Now I just have to figure out, how the heck do I make this crazy "arccotangent" thing in Lua???
iirc in lua you only get atan, not acot - that's why I made you one with it instead of acot ;) Anyway, for sake of game - I'd say avoid trigonometric functions in not necessary, they are heavy to compute. You get similar looking result with any sigmoid function, and if you use one that is simple like proposed above, you get result a lot faster.

If you like Wolframs previews - the one based on absolute value looks quite similar and you avoid calculating complex expressions under the hood.