Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a modulo operator #192

Open
sylvainpolletvillard opened this issue Jan 21, 2019 · 9 comments
Open

add a modulo operator #192

sylvainpolletvillard opened this issue Jan 21, 2019 · 9 comments
Milestone

Comments

@sylvainpolletvillard
Copy link

Hi there,

We can already do pretty cool stuff using rockstar, but some basic features are lacking. At the top of my list: a modulo operator.

I know that you can already declare one using this function:

Modulus takes Number and Divisor
While Number is as high as Divisor
Put Number minus Divisor into Number
    (blank line ending While block)
Give back Number

But unsurprisingly, it is painfully slow, especially when dealing with huge numbers.

Proposal: {a} remains {b} - modulo . Open to any other aliases

As encouragement, know that this is the last thing that prevent me to finish my Game of Life implementation in Rockstar 👾

@dylanbeattie
Copy link
Collaborator

Hey - Game of Life sounds really interesting. Which compiler/interpreter are you using to develop it?

I like the idea of remains purely because it would mean The song remains the same would be a valid expression... it might get in based on that alone :)

Right now, though, I'm working to get a stable 1.0 release candidate along with a proper regression test suite, which should be done in the next few days - once we've got that, I'm gonna triage any other feature suggestions for 1.0 and see if it's easy enough to add them without breaking anything.

@dylanbeattie dylanbeattie added this to the 1.0 milestone Jan 21, 2019
@sylvainpolletvillard
Copy link
Author

sylvainpolletvillard commented Jan 21, 2019

I use https://github.com/yanorestes/rockstar-py ; it gave me the best results so far, even if every interpreter has its own issues.

Here's the Game of life implementation for anyone interested: https://github.com/sylvainpolletvillard/gameofrockstars

@ascheja
Copy link
Contributor

ascheja commented Jan 21, 2019

Depending on the size of your numbers you could try something like this:

Modulus takes Number and Divisor
Put Divisor times 1000 into Big Divisor
While Number is as high as Big Divisor
Put Number minus Big Divisor into Number
    (blank line ending While block)
While Number is as high as Divisor
Put Number minus Divisor into Number
    (blank line ending While block)
Give back Number

@sylvainpolletvillard
Copy link
Author

My numbers go from 0 to 2^64, so I would need a couple of these intermediate divisors, and it would still be very slow.

@dylanbeattie
Copy link
Collaborator

That is absolutely amazing. Just... wow

\m/

@gaborsch
Copy link

gaborsch commented Jan 22, 2019

I'm not against the Modulo operator, but let me show you another, quicker solution for the Modulus function:

Modulus takes Number and Divisor
Put Divisor into Big Divisor
While Big Divisor is less than Number
Put Big Divisor times 2 into Big Divisor
    (blank line ending While block)
While Big Divisor is as high as Divisor
If Number is as high as Big Divisor
Put Number minus Big Divisor into Number
    (blank line ending If block)
Put Big Divisor over 2 into Big Divisor
    (blank line ending While block)
Give back Number

For a big number of 2^64 mod 1, it will end in 2*64 cycles. For smaller Numbers and bigger Divisors it is way faster.

@gaborsch
Copy link

BTW, a Floor function would be useful, too. With the Floor, we can express the Modulo function, too.:

a mod b = a - b * Floor(a/b))

@sylvainpolletvillard
Copy link
Author

sylvainpolletvillard commented Jan 22, 2019

Yes, or vice versa, Floor(n) = n - n%1

Your method is indeed way faster, good enough for my usage. But it only works for integers, so it cannot be applied for the Floor function shown above.

@gaborsch
Copy link

@sylvainpolletvillard Glad to help you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants