# Thread: Having trouble with primitive type 'long'

1. ## Having trouble with primitive type 'long'

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.setSize(1000, 1000);
frame.setVisible(true);
}
}```
Thanks!  Reply With Quote

2. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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  Reply With Quote

3. ## 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?  Reply With Quote

4. ## 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.setSize(1000, 1000);
frame.setVisible(true);
}
}```  Reply With Quote

5. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## 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?  Reply With Quote

6. ## Re: Having trouble with primitive type 'long' Originally Posted by Tolls 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.  Reply With Quote

7. Member Join Date
Dec 2012
Posts
28
Rep Power
0

## 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.  Reply With Quote

8. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: Having trouble with primitive type 'long' Originally Posted by Sizzlewump 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.  Reply With Quote

#### Posting Permissions

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