This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
sancarn's profile picture

[Lua] [Question] Singling out the remainder from a division.

Started by sancarn, 24 July 2012 - 10:51 AM
sancarn #1
Posted 24 July 2012 - 12:51 PM
So I have the following problem:

I have a number n and then divide this number by 8

let, for example, n = 45

45/8 = 5 r 5 (or 5 & 5/8)

Now I want a way of singling out the 2 parts of this number to give a result like so:
a=5
r=5 (or 5/8(either way isn't a problem because I can just multiply by 8 with the latter))

Let me give 1 last example:
n=59
n/8 = 7 r 3

a=7
r=3

I know how to do everything except for defining 'r' and 'a' separately. Does anyone know how to do this quickly and efficiently without the need for a loop?

————————————————————————————————————————————————

These are some ways I know of making it work: Rounding down n/8 to the nearest integer naming that 'a' then do r = n/8 - 8*a

or

Subtract 1 from n/8 until negative and in the mean time do a=a+1. when negative a=a-1 and r=left over result*8
sancarn #2
Posted 24 July 2012 - 02:05 PM
as a comment the things at the bottom are the bits that could work but each one requires a loop of sorts. I was wondering if there was an expression which could do it instantly without the need for a loop

why? Because loops are slower than inbuilt functions generally
Lyqyd #3
Posted 24 July 2012 - 03:51 PM
You're probably looking for the modulus operator. It is %. 7 % 3 = 1.
sancarn #4
Posted 24 July 2012 - 05:14 PM
You're probably looking for the modulus operator. It is %. 7 % 3 = 1.

I thought modulus was magnitude? That is if the modulus is the same as the norm in maths… I'll have a look see =)