This site requires JavaScript, please enable it in your browser!
Greenfoot back
8bitcarrotjuice
8bitcarrotjuice wrote ...

2013/8/19

Question

8bitcarrotjuice 8bitcarrotjuice

2013/8/19

#
What does math.hypot, math.abs and math.signmum do?
Gevater_Tod4711 Gevater_Tod4711

2013/8/19

#
Math.hypot uses the Pythagorean theorem. So Math.hypot(3, 4) will return 5 (radical of (3^2 + 4^2)) Math.abs returns the absolute value of the given number. Math.abs(3) will return 3 and Math.abs(-3) will also return 3. Math.signum returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
8bitcarrotjuice 8bitcarrotjuice

2013/8/19

#
So, ^ is to the power of, abs does not care about negatives and positives, but I don't get signum? Can you give me an example? Please^10000?
Gevater_Tod4711 Gevater_Tod4711

2013/8/19

#
If the value you give to the funktion as parameter is 0 also 0 is returned. If the value is less than 0 (e.g. Math.signum(-5)) -1 will be returned. And if the value is greater than 0 (e.g. Math.signum(5)) 1 is returned.
8bitcarrotjuice 8bitcarrotjuice

2013/8/19

#
So it's like a boolean, with 3 states. Can you do this?
public void act() 
{
      int rn=Greenfoot.getRandomNumber(100)-50;
      int sgnm=Math.signum(rn);
      if(sgnm==1) { 
           //code
      }
      if(sgn==0) {
           //code
      }
      if(sgn==-1) {
           //code
      }
}
Are there other uses?
Gevater_Tod4711 Gevater_Tod4711

2013/8/19

#
Math.signum returns a double value so you'll have to cast the value in line 4 but if you do this it should work.
danpost danpost

2013/8/19

#
(1) Can you do this? No problem there; if you wanted a 49 percent chance of 1, 50 percent chance of -1 and only 1 percent chance of 0. (2) Are there other uses? I have used it in some of my scenarios. Look at the Player class of my Toll-Tally Mad scenario (where I used it to determine the direction of movement in the vertical direction) and in the Ball class of my Pong Pinball scenario (where I used it in determining the world coordinate of the corner of the block that the ball is bouncing off of). It was useful in the second one because the location of the block minus half the width of the image did not always return the correct value since the block actors are rotated (when rotated 90 or 270 degrees the width of the image is actually the height of the actor and the height of the image is actually the width of the actor).
8bitcarrotjuice 8bitcarrotjuice

2013/8/20

#
Thanks!
You need to login to post a reply.