Discuss Scratch

sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

When I am trying to multiply large numbers, the Scratch multiply operator gives something like 1.06111641815467e+45, but that breaks my project.

Is there a script that I could use to multiply two numbers?
My project is here.
1000000*10000000000Not this, but something else that does, essentially, the same thing.
BKFighter
Scratcher
1000+ posts

Multiply without the multiply operator

Try this: http://wiki.scratch.mit.edu/wiki/()_*_()_(block)#Workaround
Phosphor
Scratcher
71 posts

Multiply without the multiply operator

The real question, it seems, is “How can I use really big numbers in Scratch?”

This project by daffy1234 should be helpful: Big Integer V2.0

One way to use the sprite for your project is to use broadcasts to tell the math engine when to multiply two numbers. Then you can retrieve the result with this block:
multiplyofSprite1

Let me know if you need more help!
sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

BKFighter wrote:

Try this: http://wiki.scratch.mit.edu/wiki/()_*_()_(block)#Workaround
The one where it adds repeatedly crashes with large numbers with Run without screen refresh enabled. Without it, it takes a really long time. (as in forever)
Dividing by the reciprocal does the same thing as just having multiplication.
I am really stuck here.

Last edited by sithsiri (Feb. 11, 2016 00:08:26)

sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

Bump
sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

Bump

Phosphor wrote:

The real question, it seems, is “How can I use really big numbers in Scratch?”

This project by daffy1234 should be helpful: Big Integer V2.0

One way to use the sprite for your project is to use broadcasts to tell the math engine when to multiply two numbers. Then you can retrieve the result with this block:
multiplyofSprite1

Let me know if you need more help!
The multiply block has a maximum digit number of 35, at least in my testing. You can try at my project. I will try removing zeroes from the password, which should probably help, but not much, especially with 8 digit passwords.
gtoal
Scratcher
1000+ posts

Multiply without the multiply operator

You can't calculate integer numbers that are too large for Scratch to store as integers. The number you are seeing is the nearest floating point value to the product you want.

If you absolutely must use huge integer numbers you need to represent them differently, such as by text strings or by arrays of digits.

If you do this, you'll need to supply all the maths operations that you will ever use with your own custom blocks to do not just multiply, but add and subtract and everything else, as well as possibly also needing to supply I/O procedures for your own number representation. Don't forget when you do this that you'll also want to handle negative numbers, and decide from the start if you want to handle numbers with decimal parts.

This is quite doable by someone at your level and an interesting project to take on. This is usually called a “bignum” package, or if you prefer ‘extended integer precision’.

Related to this is handling fractions rather than decimals (eg storing 1/3 as a ratio rather than 0.3333333333333333333333333… etc) - but that's probably too advanced and something to contemplate after first doing just an integer bignum package.

Graham

Last edited by gtoal (Feb. 12, 2016 04:52:55)

sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

Maybe binary?
I am going to try using something that can convert large decimal numbers to binary and back and this project together.

For example, 100011000011101001111101101001011001000011011111010111111010110001111001000000100000000 goes to a number…

Or maybe hex? But I'm still on binary.

Last edited by sithsiri (Feb. 13, 2016 02:05:48)

sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

gtoal wrote:

You can't calculate integer numbers that are too large for Scratch to store as integers. The number you are seeing is the nearest floating point value to the product you want.

If you absolutely must use huge integer numbers you need to represent them differently, such as by text strings or by arrays of digits.

If you do this, you'll need to supply all the maths operations that you will ever use with your own custom blocks to do not just multiply, but add and subtract and everything else, as well as possibly also needing to supply I/O procedures for your own number representation. Don't forget when you do this that you'll also want to handle negative numbers, and decide from the start if you want to handle numbers with decimal parts.

This is quite doable by someone at your level and an interesting project to take on. This is usually called a “bignum” package, or if you prefer ‘extended integer precision’.

Related to this is handling fractions rather than decimals (eg storing 1/3 as a ratio rather than 0.3333333333333333333333333… etc) - but that's probably too advanced and something to contemplate after first doing just an integer bignum package.

Graham

How would I do this? Not using any Scratch blocks for addition seems pretty hard. I have no idea. Thanks, though!

Last edited by sithsiri (Feb. 13, 2016 17:10:15)

gtoal
Scratcher
1000+ posts

Multiply without the multiply operator

sithsiri wrote:

How would I do this? Not using any Scratch blocks for addition seems pretty hard. I have no idea. Thanks, though!

Just to be clear, you're not talking about one of those puzzle competitions where you're not allowed to use the “+” operator at all? My understanding of what you're asking for is to be able to do integer addition with really big numbers for which the normal “+” does not work, meaning that you have to create your own ‘add’ block to do the addition?

If that is correct, then I'll show you a trivial solution just for adding and anything else you might want to extend it with, you can work out yourself once you understand the basic method.

I'll be back in about 30 minutes once I've written something.

G
sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

gtoal wrote:

Just to be clear, you're not talking about one of those puzzle competitions where you're not allowed to use the “+” operator at all? My understanding of what you're asking for is to be able to do integer addition with really big numbers for which the normal “+” does not work, meaning that you have to create your own ‘add’ block to do the addition?

If that is correct, then I'll show you a trivial solution just for adding and anything else you might want to extend it with, you can work out yourself once you understand the basic method.

I'll be back in about 30 minutes once I've written something.

G
No, but I should try one
Take a look here.
I actually think I got addition down here. I couldn't understand some things that other people used, so I used the stuff that you learn in elementary, adding each place. No decimals now, that would break it. And if I can do addition, well:
definemultiplyx*ysetoutputto0repeatychangeoutputbyxIf y is smaller, this is faster. Otherwise, x and y switch

Last edited by sithsiri (Feb. 13, 2016 20:18:50)

deck26
Scratcher
1000+ posts

Multiply without the multiply operator

sithsiri wrote:

gtoal wrote:

Just to be clear, you're not talking about one of those puzzle competitions where you're not allowed to use the “+” operator at all? My understanding of what you're asking for is to be able to do integer addition with really big numbers for which the normal “+” does not work, meaning that you have to create your own ‘add’ block to do the addition?

If that is correct, then I'll show you a trivial solution just for adding and anything else you might want to extend it with, you can work out yourself once you understand the basic method.

I'll be back in about 30 minutes once I've written something.

G
No
Take a look here.
I actually think I got addition down here. I couldn't understand some things that other people used, so I used the stuff that you learn in elementary, adding each place. No decimals now, that would break it. And if I can do addition, well:
definemultiplyx*ysetoutputto0repeatychangeoutputbyxIf y is smaller, this is faster. Otherwise, x and y switch
That'll still give the same problem for large numbers though.
sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

deck26 wrote:

sithsiri wrote:

No
Take a look here.
I actually think I got addition down here. I couldn't understand some things that other people used, so I used the stuff that you learn in elementary, adding each place. No decimals now, that would break it. And if I can do addition, well:
definemultiplyx*ysetoutputto0repeatychangeoutputbyxIf y is smaller, this is faster. Otherwise, x and y switch
That'll still give the same problem for large numbers though.
Oh. I meant:
definemultiplyx*ysetoutputto0repeatyoutputx+yIf y is smaller, this is faster. Otherwise, x and y switch

Last edited by sithsiri (Feb. 13, 2016 20:28:04)

gtoal
Scratcher
1000+ posts

Multiply without the multiply operator

whenclickedaskEnter a large positive integerandwaitsetnum1tojoinansweraskEnter another large positive integerandwaitsetnum2tojoinanswerAddnum1num2sayjoinjoinnum1 + joinnum2join = add:resultfor10secsdefineAddn1n2setadd: resultto0setadd: n1ton1setadd: n2ton2repeatuntilnotlengthofadd:n1<lengthofadd:n2setadd: n1tojoin0add:n1setadd: n1tojoin0add:n1repeatuntillengthofadd:n2=lengthofadd:n1setadd: n2tojoin0add:n2setadd: indextolengthofadd:n1setadd: resulttosetadd: carryto0repeatuntiladd:index=0setadd: digit 1toletteradd:indexofadd:n1setadd: digit 2toletteradd:indexofadd:n2setadd: digit sumtoadd:digit1+add:digit2+add:carryifadd:index>1ornotadd:digitsummod10=0thensetadd: resulttojoinadd:digitsummod10add:resultsetadd: carrytofloorofadd:digitsum/10changeadd: indexby-1
PS there are comments in the actual project: https://scratch.mit.edu/projects/97834341/

Last edited by gtoal (Feb. 13, 2016 20:39:28)

deck26
Scratcher
1000+ posts

Multiply without the multiply operator

sithsiri wrote:

[
Oh. I meant:
definemultiplyx*ysetoutputto0repeatyoutputx+yIf y is smaller, this is faster. Otherwise, x and y switch
Hard to comment without knowing what ‘output’ is doing but potentially OK!
sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

deck26 wrote:

sithsiri wrote:

[
Oh. I meant:
definemultiplyx*ysetoutputto0repeatyoutputx+yIf y is smaller, this is faster. Otherwise, x and y switch
Hard to comment without knowing what ‘output’ is doing but potentially OK!
It's at this link.
gtoal
Scratcher
1000+ posts

Multiply without the multiply operator

sithsiri wrote:

deck26 wrote:

sithsiri wrote:

No
Take a look here.
I actually think I got addition down here. I couldn't understand some things that other people used, so I used the stuff that you learn in elementary, adding each place. No decimals now, that would break it. And if I can do addition, well:
definemultiplyx*ysetoutputto0repeatychangeoutputbyxIf y is smaller, this is faster. Otherwise, x and y switch
That'll still give the same problem for large numbers though.
Oh. I meant:
definemultiplyx*ysetoutputto0repeatyoutputx+yIf y is smaller, this is faster. Otherwise, x and y switch

Yes, you can simulate multiplication by repeated addition but it will be very slow unless you also take advantage of the fact that multiplying a decimal number by 10 can be done by joining the number-string to ‘0’ !

But you're right, it's like elementary school math - just do shift and add for each digit. I.e. to multiply by 57, add the original number 7 times, then take the original number and append a ‘0’ to it, and add that 5 times to your running total. Worst-case performance is 9 * (number of digits in string).
gtoal
Scratcher
1000+ posts

Multiply without the multiply operator

sithsiri wrote:

It's at this link.

Good. You're using arrays rather than strings, but that's a perfectly valid choice.

Now you'll need to think about how you structure your applications to be able to call these replacement operators. Since Scratch has no function results that can be used in expressions, it makes normal infix calculations quite tedious if you call any operator more than once in an expression. (You have to do all the calls first, assign them to temporaries, then write your expression in terms of the pre-calculated temporaries)

An alternative style that you might try is to support a stack, and do all your calculations in reverse polish… eg Push(123456789); Push(987654321); Add(); Print() - you might get some hints here: https://scratch.mit.edu/projects/30342314/

G
gtoal
Scratcher
1000+ posts

Multiply without the multiply operator

sithsiri wrote:

gtoal wrote:

Just to be clear, you're not talking about one of those puzzle competitions where you're not allowed to use the “+” operator at all? My understanding of what you're asking for is to be able to do integer addition with really big numbers for which the normal “+” does not work, meaning that you have to create your own ‘add’ block to do the addition?

If that is correct, then I'll show you a trivial solution just for adding and anything else you might want to extend it with, you can work out yourself once you understand the basic method.

I'll be back in about 30 minutes once I've written something.

G
No, but I should try one

This is my favourite hack for multiplying:

definemulrepeatuntilx=0ory=0changexby-1changeyby-1changeresultby1changeresultbyxchangeresultbyy

Derived as a recurrence relation from the expansion of (x+1) * (y+1)
sithsiri
Scratcher
100+ posts

Multiply without the multiply operator

gtoal wrote:

This is my favourite hack for multiplying:

definemulrepeatuntilx=0ory=0changexby-1changeyby-1changeresultby1changeresultbyxchangeresultbyy

Derived as a recurrence relation from the expansion of (x+1) * (y+1)
Wouldn't this have the same limitations as using the add block?

Powered by DjangoBB