Can someone help improve my math.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Coenster

Hi

I am not a master at math, so I would like it if some smart person could help me out.

I need to calculate the available space in my inventory and if there is enough space add resources, but if there is not enough space only add some resources until the storage is full. For example: I have 1.7 resources but there is space for only 1.5 in the inventory.
So what I did is:

var overflow = resources - available_space
var able_to _store = resources - overflow

Then i just add the able_ to_ store to the Inventory.
Can someone please show me how to write this simple math more proper? It just feels wrong leaving it like this.

Thanks in advance.

:bust_in_silhouette: Reply From: marcorexo

Not sure if this helps, but If we take both of your equations and simplify them we get (I’ve omitted var):

overflow = resources - available_space
able_to_store = resources - overflow

which is simplifies to :

able_to_store = resources - resources - available_space
able_to_store = - available_space which is a constant…you’ll always get the same output.

So you could do this:
var overflow = available_space - resources

e.g. in a scenario where we have more resources than the inventory can hold:
overflow = 1.5 - 1.7
overflow = - 0.2 so you can need to remove 0.2 resources from the inventory

or in a scenario where we have less resources than capacity:
overflow = 1.5 - 1.0
overflow = + 0.5 …meaning we have the capacity to store 0.5 more resources.

So I guess instead of using overflow, you could use the word capacity to make it more understandable (assuming that I’m using your terms correctly)

:bust_in_silhouette: Reply From: estebanmolca

Hi, I’m also bad for math, but I like … after thinking and trying several things, the best way I found is this. It can be added and subtracted without exceeding the upper and lower limits.
Sorry but I could not do it using only mathematical formulas.

 func store_resources(num_resources:float):
  var available_space:float =  total_capacity - stored_resources;
  stored_resources += clamp(num_resources, -stored_resources, available_space)	

If it is going to be used inside a loop, I think that it would be necessary to modify the line store_resources + =