Float and int, one works but why not the other?

the code works perfectly the way i want, im just curious as to why when i initialise the size value in the form of float the code works, but it dosnt work as int size; can someone explain why? just sheer curiosity for the future.

(edit was just to make the text fit better)

                int y = 100;
                int speed = 3;
                float size = 50;

            void setup(){
              size(500,500);
              stroke(0);
              fill(175);
            }
            void draw(){
              background(255);
              y+= speed;
              if ((y>500) || (y<0)) {
                speed = speed * -1;
              }
              ellipse(y, y,size, size);
            } 
            void mouseReleased(){
              size = random(20,60);
              redraw();
            }
Tagged:

Answers

  • Answer ✓

    random() returns a float. you need to cast it to an int if you need it to be an int

    sz = (int)random(20, 60);
    

    (best not to use variables with the same name as processing reservered words)

  • edited August 2017 Answer ✓
    • Function random() returns a float value: https://Processing.org/reference/random_.html
    • Thus whole datatype variables, such as int, can't store it.
    • You can though downcast it via (int): (int) random(20, 60);.
    • Or even use round(), floor() or ceil(): round(random(20, 60));.
  • is it better to use int or float? or does it depend on the situation? i understand float is increments (technically i know its just a refresh not actually decimals as 2.5 or 2.7 etc dosnt exist)

  • thanks very much, what does downcast mean?

  • Answer ✓

    Is it better to use int or float?

    Whole datatypes, like byte, short, char, int & long, are slightly faster.
    And they are safer, b/c they're free of rounding errors.

  • why use float then? would int or its counterparts not be better in every situation or is there certain other commands that require float?

  • Answer ✓

    What does downcast mean?

    • Datatypes can have a hierarchy inheritance, from simpler to more complex.
    • For numbers, downcasting means coercing a more complex number type into a simpler 1.
    • However, downcasting may remove some precision. Thus losing information.
  • edited August 2017 Answer ✓

    Java automatically coerces a simpler number type to a complex 1 whenever needed. ~O)
    That's called automatic upcasting number type promotion. :-B

  • Thank you very much :) very informative and understandable thank you ^:)^

  • edited August 2017 Answer ✓

    why use float then? would int or its counterparts not be better in every situation or is there certain other commands that require float?

    There are many. Consider arc(). It takes an argument of how many radians around the circle you want it to start and stop.

    That means if you want a quarter of a circle, you need half of PI.

    arc(50, 55, 50, 50, 0, HALF_PI);
    

    You could also approximate this with 3.1415/2:

    arc(50, 55, 50, 50, 0, 3.1415/2);
    

    ...so that is just one example of where you need floats -- circles and angles.

  • (edit was just to make the text fit better)

    just don't indent your text and it'll show it like text. if you indent it it'll treat it as code and not wrap it.

    use a blank line between paragraphs.

  • If you check the markdown examples, you will understand why posting in the forum behaves the way it does: https://en.wikipedia.org/wiki/Markdown

    Notice this link is available right below any comment box: You can use Markdown in your post.

    Kf

Sign In or Register to comment.