My old nemesis, math

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

My old nemesis, math

Post 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?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: My old nemesis, math

Post 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 ;)
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: My old nemesis, math

Post by Taehl »

Thanks, /mum/. Anyone else have an answer?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: My old nemesis, math

Post by TechnoCat »

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: My old nemesis, math

Post 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.
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: My old nemesis, math

Post 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)
When I write def I mean function.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: My old nemesis, math

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
giniu
Party member
Posts: 221
Joined: Mon Nov 30, 2009 4:44 pm

Re: My old nemesis, math

Post 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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: My old nemesis, math

Post 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???
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
giniu
Party member
Posts: 221
Joined: Mon Nov 30, 2009 4:44 pm

Re: My old nemesis, math

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 189 guests