bobhope - 7 months ago 17

Python Question

I'm not sure how to word the question correctly but I'll try.

I'm trying to find a formula to provide a multiplier based on the difference between 2 integers.

- 1 and 1 = distance is 0 = 1
- 1 and 2 = distance is +1 = 1.5
- 1 and 3 = distance is +2 = 1.75
- 1 and 4 = distance is +3 = 1.89
- 1 and 5 = distance is +4 = 1.95
- 1 and 6+ = distance is +5 = 2.0
- 20 and 21 = distance is +1 = 1.5

I need this to work for negative distances too.

- 2 and 1 = distance is -1 = 0.5
- 3 and 1 = distance is -2 = 0.75

all the multipliers I used above are examples.

I'm trying to implement this in Python so any sort of example in python would be ideal.

`from math import sqrt`

def multiplier(x, y):

dist = y - x

return 1 + dist/sqrt(5+dist**2)

This gives the output:

`Distance: -5 = 0.09`

Distance: -4 = 0.13

Distance: -3 = 0.20

Distance: -2 = 0.33

Distance: -1 = 0.59

Distance: 0 = 1.00

Distance: 1 = 1.41

Distance: 2 = 1.67

Distance: 3 = 1.80

Distance: 4 = 1.87

Distance: 5 = 1.91

Close enough, Thanks!

Answer

Give this a try: 1 + x/sqrt(1+x^2). This gives a trend like the one from your graph but it's y limits are 0 and 2, and x=0 gives y=1 as required.

http://m.wolframalpha.com/input/?i=1+%2B+x%2Fsqrt%281%2Bx%5E2%29&x=0&y=0

In Python:

```
import math
def multiplier(x, y):
dist = x - y
return 1 + dist/math.sqrt(1+dist**2)
```