This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-06-06 10:23:14

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Rounding

What's the squeak code to round a number?
Is it something like this?:
round t1.

Offline

 

#2 2011-06-06 10:35:48

Pecola1
Scratcher
Registered: 2010-09-06
Posts: 1000+

Re: Rounding

I think t1 rounded, lemme check.
[edit]YEP![/edit]

Last edited by Pecola1 (2011-06-06 10:37:10)


If you are reading this, please read to the end, because if you don't you won't know what's at the end. Don't just skip to the end though otherwise you won't be able to read the middle, which is most important. Now you must be wondering why you just read all that, the reason is you may have not noticed something, read it again and see if you notice it this time  smile

Offline

 

#3 2011-06-06 10:35:56

Baderous
New Scratcher
Registered: 2011-04-14
Posts: 100+

Re: Rounding

Take a look at the "truncation and round" protocol of the class Number.

Offline

 

#4 2011-06-06 10:36:13

meew0
Scratcher
Registered: 2010-02-22
Posts: 1000+

Re: Rounding

t1 rounded.


http://i.imgur.com/mJV3j.pnghttp://i.imgur.com/HwWAX.pnghttp://i.imgur.com/sZ7Ui.pnghttp://i.imgur.com/0y6yh.pnghttp://i.imgur.com/nOC4l.png

Offline

 

#5 2011-06-06 10:37:13

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Offline

 

#6 2011-06-06 10:40:09

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Rounding

Hmm, I can't get this Panther block to work. Here's my code:
round $Number$ to nearest $Number$
r

Code:

| t3 |
t3 _  t1 /  t2 rounded.
abs t3 * t2 - t1 < abs t3 * t2 + 1 - t1 ifTrue: [^ true].
^ false

What's wrong?

Offline

 

#7 2011-06-06 13:05:23

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Rounding

scimonster wrote:

Hmm, I can't get this Panther block to work. Here's my code:
round $Number$ to nearest $Number$
r

Code:

| t3 |
t3 _  t1 /  t2 rounded.
abs t3 * t2 - t1 < abs t3 * t2 + 1 - t1 ifTrue: [^ true].
^ false

What's wrong?

Do I need brackets or something between them, to make sure it evaluates it like this? :
(abs ( ( t3 * t2) - t1) ) < (abs ( ( t3 * ( t2 + 1) ) - t1) )

Offline

 

#8 2011-06-06 13:32:44

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: Rounding

scimonster wrote:

scimonster wrote:

Hmm, I can't get this Panther block to work. Here's my code:
round $Number$ to nearest $Number$
r

Code:

| t3 |
t3 _  t1 /  t2 rounded.
abs t3 * t2 - t1 < abs t3 * t2 + 1 - t1 ifTrue: [^ true].
^ false

What's wrong?

Do I need brackets or something between them, to make sure it evaluates it like this? :
(abs ( ( t3 * t2) - t1) ) < (abs ( ( t3 * ( t2 + 1) ) - t1) )

I think needs to go after the equation.
Also, there's a shortcut for returning stuff like that. Instead of this:

( ( (t3 * t2) - t1) abs ) < ( ( (t3 * t2) + 1) abs - t1) ifTrue: [^ true].
^ false

Use this:

^ ( ( (t3 * t2) - t1) abs ) < ( ( (t3 * t2) + 1) abs - t1)


/* No comment */

Offline

 

#9 2011-06-06 14:08:40

Pecola1
Scratcher
Registered: 2010-09-06
Posts: 1000+

Re: Rounding

scimonster wrote:

scimonster wrote:

Hmm, I can't get this Panther block to work. Here's my code:
round $Number$ to nearest $Number$
r

Code:

| t3 |
t3 _  t1 /  t2 rounded.
abs t3 * t2 - t1 < abs t3 * t2 + 1 - t1 ifTrue: [^ true].
^ false

What's wrong?

Do I need brackets or something between them, to make sure it evaluates it like this? :
(abs ( ( t3 * t2) - t1) ) < (abs ( ( t3 * ( t2 + 1) ) - t1) )

Put the brackets, if it takes them away automatically you don't need them.


If you are reading this, please read to the end, because if you don't you won't know what's at the end. Don't just skip to the end though otherwise you won't be able to read the middle, which is most important. Now you must be wondering why you just read all that, the reason is you may have not noticed something, read it again and see if you notice it this time  smile

Offline

 

#10 2011-06-06 14:25:03

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Rounding

I currently have this as the code:

round $Number$ to nearest $Number$
-

Code:

| t3 |
t3 _  t1 /  t2 rounded.
 ( ( (t3 * t2) - t1) abs ) < ( ( (t3 * t2) + 1) - t1) abs ifTrue: [^  ( ( (t3 * t2) - t1) abs ) rounded].
^  ( ( (t3 * t2) + 1) - t1) abs rounded

It always reports 0 though!

Offline

 

#11 2011-06-06 14:39:07

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: Rounding

round $Number to nearest $Number$

^ ((t1 / t2) rounded) * t2

Does this work?


/* No comment */

Offline

 

#12 2011-06-06 14:41:06

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Rounding

TheSuccessor wrote:

round $Number to nearest $Number$

^ ((t1 / t2) rounded) * t2

Does this work?

Yes, thank you!
How was that so simple?  neutral

Offline

 

Board footer