# number turning negative during calculation why???

• July 2nd, 2012, 08:07 PM
derekxec
number turning negative during calculation why???
so here is my calculation and when total equals 397,683 the numbers start turning negative....why at that number and why at all? it works fine up to 397,682

Code :

```long days, hours, minutes, seconds;   days = (long) (total / 86400); System.out.println("" + days); total = total - (days * 86400); System.out.println("" + total); hours = (long) (total / 3600); System.out.println("" + hours); total = total - (hours * 3600); System.out.println("" + total); minutes = (long) (total / 60); System.out.println("" + minutes); total = total - (minutes * 60); System.out.println("" + total); seconds = (long) total; System.out.println("" + seconds);       totalTime.setText("" + days + "d:" + hours + "h:" + minutes + "m:" + seconds + "s");```

-24855
-7096.0
-1
-3496.0
-58
-16.0
-16

thats what the println display when i do 397683

24855
10800.0
3
0.0
0
0.0
0

when i do 397682
• July 2nd, 2012, 09:10 PM
stdunbar
Re: number turning negative during calculation why???
Well, you've missed the absolute most important part - how is total defined? You didn't take the time to give us a SSCCE but I will:

Code :

```public class DayThing { public static void main( String argv[] ) { long total = 397683; long days, hours, minutes, seconds;   days = (long) (total / 86400); System.out.println("" + days + " days" ); total = total - (days * 86400); System.out.println("" + total + " total"); hours = (long) (total / 3600); System.out.println("" + hours + " hours"); total = total - (hours * 3600); System.out.println("" + total + " total" ); minutes = (long) (total / 60); System.out.println("" + minutes + " minutes"); total = total - (minutes * 60); System.out.println("" + total + " total" ); seconds = (long) total; System.out.println("" + seconds + " seconds" ); } }```

does not show the behavior you describe. Can you elaborate?
• July 2nd, 2012, 11:09 PM
derekxec
Re: number turning negative during calculation why???
sorry about that this is the whole code for the program here

Code :

```package testing;   import javax.swing.*; import java.awt.*; import java.awt.event.*;     public class Test extends JFrame implements ActionListener{   private static final long serialVersionUID = 1L;     JLabel troopLabel = new JLabel("Troops: "); JTextField troopField = new JTextField(7);   JLabel accelLabel = new JLabel("Accels: "); JTextField acceleratorField = new JTextField(7);   JButton button = new JButton("Calculate");     JLabel uraniumCost = new JLabel("Uranium Cost: "); JLabel totalCost = new JLabel();   JLabel spaceLabel = new JLabel("Space: "); JLabel space = new JLabel();   JLabel expLabel = new JLabel("Exp Gain: "); JLabel expGain = new JLabel();   JLabel totalTimeLabel = new JLabel("Time: "); JLabel totalTime = new JLabel();     public Test(String title) { setTitle(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   createGUI(); }   public void createGUI() {   setLayout(new GridLayout(7,1,10,10));     JPanel panel1 = new JPanel(); panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); panel1.add(troopLabel); panel1.add(troopField);   add(panel1);   JPanel panel2 = new JPanel(); panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); panel2.add(accelLabel); panel2.add(acceleratorField); add(panel2);   JPanel panel3 = new JPanel(); panel3.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); panel3.add(button);   add(panel3);     JPanel panel4 = new JPanel(); panel4.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); panel4.add(uraniumCost); panel4.add(totalCost); add(panel4);   JPanel panel5 = new JPanel(); panel5.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); panel5.add(spaceLabel); panel5.add(space); add(panel5);   JPanel panel6 = new JPanel(); panel6.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); panel6.add(expLabel); panel6.add(expGain); add(panel6);   JPanel panel7 = new JPanel(); panel7.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); panel7.add(totalTimeLabel); panel7.add(totalTime); add(panel7);   button.addActionListener(this);     }     public static void main(String[] args) {   Test gui = new Test("Chimera"); gui.setSize(430, 400); gui.setVisible(true); }     public void actionPerformed(ActionEvent event) {   totalCost.setText(""); space.setText(""); totalTime.setText(""); expGain.setText("");       String troopNumber = troopField.getText(); String accelNumber = acceleratorField.getText(); try {   long troopNumb = Long.parseLong(troopNumber); long exptotal = troopNumb * 717187;   int troopNum = Integer.parseInt(troopNumber); int accelNum = Integer.parseInt(accelNumber);   String pressed = event.getActionCommand(); if(pressed.equals("Calculate")) { long uraniumtotal = troopNum * 300; long spacetotal = troopNum * 2;   space.setText("" + spacetotal); totalCost.setText("" + uraniumtotal); expGain.setText("" + exptotal);   double total = troopNum * 5400;   while (accelNum > 0) { total = total *=0.7; accelNum--; }   long days, hours, minutes, seconds;   days = (long) (total / 86400); System.out.println("" + days); total = total - (days * 86400); System.out.println("" + total); hours = (long) (total / 3600); System.out.println("" + hours); total = total - (hours * 3600); System.out.println("" + total); minutes = (long) (total / 60); System.out.println("" + minutes); total = total - (minutes * 60); System.out.println("" + total); seconds = (long) total; System.out.println("" + seconds);       totalTime.setText("" + days + "d:" + hours + "h:" + minutes + "m:" + seconds + "s");     }   }catch(NumberFormatException e) { JOptionPane.showMessageDialog(null, "Numbers only!"); }   }     }```

everything works great except this part where im trying to change the seconds into days, hours, minutes, seconds..if you run it and put 397682 in the troop field and 0 in the accel field and hit calculate the time at the bottom calculates but if you use 397683 the time comes out to be negative numbers or maybe its another part of my code thats messing with it?

also thank you for taking the time to reply its much appreciated
• July 3rd, 2012, 09:48 AM
copeg
Re: number turning negative during calculation why???
Add some println's to debug and see what might be negative - this should lead you back to the culprit. You will see one of your variables becomes negative as it gets larger, and the following is a demonstration of (and big hint to) the problem:

Code java:

``` public static void main(String[] args) { System.out.println(Integer.MAX_VALUE); System.out.println(397682 * 5400); System.out.println(397683 * 5400); }```

You need to be explicit in stating that you do not want integer math, but math with respect to your data type (in this case, a double). You can do so by casting one of your values prior to doing the actual math.
• July 3rd, 2012, 11:35 AM
derekxec
Re: number turning negative during calculation why???
ahhhh found it :D i had troopnum as an int and it couldnt hold that big of a number cant believe i missed this thanks a lot :D