|
| 12 May 2015 08:36 AM |
function Modulus(n1, n2) local n1 = n1 local counter = 0 repeat counter = counter + 1 n1 = n1 - n2 wait() until n1 - n2 < 0 return n1, counter end
So basically, I need help making the above support decimals and negatives. I also have a division function that relies heavily on the above:
function Divide(n1, n2) local ans = "" local rem, n2, nxt = n1, n2, nil repeat rem, nxt = Modulus(n1, n2) ans = ans .. nxt n1 = tonumber(tostring(rem) .. "0") if not ans:find("%.") then ans = ans .. "." end wait() until rem == 0 or #ans == 18 return tonumber(ans) end
So as much help as I can get would be appreciated. |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 08:37 AM |
WHY ARE YOU RECREATING THE WHEEL
kthx |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 08:39 AM |
| I have my reasons. But those reasons do not matter for the above. I need help making the above support decimals and negatives. |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 09:39 AM |
Are you trying to recreate modulo?
local function Mod(Number, By) local A = Number / By return (A - math.floor(A)) * By end |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 09:44 AM |
@Jarod I can't use /
If I use / that means Divide will have to rely on itself, and that just won't work. |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 09:46 AM |
Hmm. A simple search gives this as a first result: homepage.cs.uiowa.edu/~jones/bcd/mod.shtml
This is another relevant result: stackoverflow.com/questions/26047196 |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 09:49 AM |
| Jarod, my function works, I just need to add decimal and negative support. How do you suggest I do that? |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 09:52 AM |
I don't know, really. You could try something with math.floor. I need to go though, so I can't really help. Not to mention I haven't really even read your script (I tried three times, but each time I forgot that I wanted to pay attention to it and got distracted) |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 09:52 AM |
Multiply the number by 10 or something then use your function then divide it by the amount you multiplied it.
I hope I'm right...
|
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 12 May 2015 09:55 AM |
Division a / b could theoretically be expressed as a * b^-1
Thank reciprocals. |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 09:56 AM |
function Modulus(n1, n2) local n1 = n1 local counter = 0 local neg = not (math.abs(n2) == n2) repeat counter = counter + 1 n1 = n1 - n2 wait() until n1 - n2 < 0 return neg and -n1 or n1, counter end
I think the above supports negatives. I might be wrong though. Would the above work if I start dividing negativesÉ |
|
|
| Report Abuse |
|
|
|
| 12 May 2015 10:00 AM |
@eLunate
Sounds promising, but first I'd have to add decimal support to my Multiply and negative support to my Exponent LOL |
|
|
| Report Abuse |
|
|