Results 1 to 8 of 8
  1. #1
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Having trouble with primitive type 'long'

    Please help with my coding chaos! I'm getting some errors in my code like:

    Spiral.java:25: possible loss of precision
    found : long
    required: int
    int yNeg = hCen - 20 - (20 * fib(j));

    I understand that I gave it the wrong data type but I'm not sure how to cast (is that the right term?) an int to a long. What are your recommendations? Here is my code:
    Java Code:
    /* Created by John Thrush and user3266259 (of stackOverflow)
    4/06/2016
    Spiral is an app that displays a rectangular spiral
    and should incorporate growth in the fibonacci series
    based on the hardcoded value of "N" so as not to draw 
    through infinity. */
    import java.awt.*; 
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    public class Spiral extends JPanel
    {
       public void paintComponent(Graphics g)
       {
          long w = getSize().width;
          long h = getSize().height;
          long wCen = w / 2;
          long hCen = h / 2;
             int N = 13;
             for (int j = 1; j <= N; j++)
    		   {
    			   int xPos = wCen + (20 * fib(j));
    		      int xNeg = wCen - 20 - (20 * fib(j));
    		      int yPos = hCen + 20 + (20 * fib(j));
    		      int yNeg = hCen - 20 - (20 * fib(j));
    				
                System.out.println(j + ": " + fib(j));
                g.drawLine(xPos, yNeg, xPos, yPos);
    		      g.setColor(Color.blue);
                g.drawLine(xPos, yPos, xNeg, yPos);
    			   g.setColor(Color.red);
                g.drawLine(xNeg, yPos, xNeg, yNeg);
    			   g.setColor(Color.green);
                g.drawLine(xNeg, yNeg, xPos, yNeg);
    		      g.setColor(Color.cyan);
    			}	
       }
    	
       public static long fib(int n) 
    	{
          if (n <= 1) return n;
          else return fib(n-1) + fib(n-2);
       }
    	
       public static void main(String[] args)
       {
          Spiral panel = new Spiral();
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(panel);      
          frame.setSize(1000, 1000); 
          frame.setVisible(true);     
       }
    }
    Thanks!
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Having trouble with primitive type 'long'

    Yes you cast it like this.

    long a = 20;
    int b = (int)a;

    In your case the entire right side is converted to a long so you should put it in () and cast the entire expression.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Having trouble with primitive type 'long'

    Thanks Jim! But when I do:
    Java Code:
    int xPos = int(wCen + (20 * fib(j)));
    for each of of my variables, I get errors like:

    Spiral.java:22: '.class' expected
    int xPos = int(wCen + (20 * fib(j)));

    Have I done something wrong?
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  4. #4
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Having trouble with primitive type 'long'

    Oops, I misinterpreted your advice, Jim. This seems to work now, thanks again:

    Java Code:
    /* Created by John Thrush and user3266259 (of stackOverflow)
    4/06/2016
    Spiral is an app that displays a rectangular spiral
    and should incorporate growth in the fibonacci series
    based on the hardcoded value of "N" so as not to draw 
    through infinity. */
    import java.awt.*; 
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    public class Spiral extends JPanel
    {
       public void paintComponent(Graphics g)
       {
          int w = getSize().width;
          int h = getSize().height;
          int wCen = w / 2;
          int hCen = h / 2;
          int N = 13;
             for (int j = 1; j <= N; j++)
    		   {
    			   int xPos = (int)(wCen + (20 * fib(j)));
    		      int xNeg = (int)(wCen - 20 - (20 * fib(j)));
    		      int yPos = (int)(hCen + 20 + (20 * fib(j)));
    		      int yNeg = (int)(hCen - 20 - (20 * fib(j)));
    				
                System.out.println(j + ": " + fib(j));
                g.drawLine(xPos, yNeg, xPos, yPos);
    		      g.setColor(Color.blue);
                g.drawLine(xPos, yPos, xNeg, yPos);
    			   g.setColor(Color.red);
                g.drawLine(xNeg, yPos, xNeg, yNeg);
    			   g.setColor(Color.green);
                g.drawLine(xNeg, yNeg, xPos, yNeg);
    		      g.setColor(Color.cyan);
    			}	
       }
    	
       public static long fib(int n) 
    	{
          if (n <= 1) return n;
          else return fib(n-1) + fib(n-2);
       }
    	
       public static void main(String[] args)
       {
          Spiral panel = new Spiral();
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.add(panel);      
          frame.setSize(1000, 1000); 
          frame.setVisible(true);     
       }
    }
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Having trouble with primitive type 'long'

    A simpler solution, since you are not using longs anyway, would be to get fib to return an int, unless it simply has to return a long for some reason?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Having trouble with primitive type 'long'

    Quote Originally Posted by Tolls View Post
    A simpler solution, since you are not using longs anyway, would be to get fib to return an int, unless it simply has to return a long for some reason?
    Yes, I it's necessary to use long for when I change the value of N to larger values. I was kind of just playing with the upper limits of memory in Java. When N > 42, you can start to notice the time taken for the calculations of fib.
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  7. #7
    Pavlo is offline Member
    Join Date
    Dec 2012
    Posts
    28
    Rep Power
    0

    Default Re: Having trouble with primitive type 'long'

    Hello

    On left side of your equation
    Java Code:
    int yNeg = hCen - 20 - (20 * fib(j));
    you state int type, but on right side the type is long. This means if you cast to int, you can lost your long value! So, I would consider such cast only if this *** hCen - 20 - (20 * fib(j)) *** always smaller than int type limit. Any way I would think about appropriate use of int and long types, instead of cast.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Having trouble with primitive type 'long'

    Quote Originally Posted by Sizzlewump View Post
    Yes, I it's necessary to use long for when I change the value of N to larger values. I was kind of just playing with the upper limits of memory in Java. When N > 42, you can start to notice the time taken for the calculations of fib.
    But then you have values > an int, so when you cast it'll be wrong anyway.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. syntax error on token '?', invalid primitive type
    By passy in forum Advanced Java
    Replies: 4
    Last Post: 09-25-2014, 02:07 PM
  2. Passing Primitive Data Type Arguments
    By tariqm in forum New To Java
    Replies: 6
    Last Post: 04-15-2012, 01:58 PM
  3. Replies: 5
    Last Post: 04-02-2012, 03:26 PM
  4. Primitive data type and class
    By Roselicious in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 04:27 PM
  5. JNI accessing non primitive data type
    By H_P in forum Advanced Java
    Replies: 1
    Last Post: 04-14-2010, 06:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •