Addition of doubles in Java

Doubles are treated much the same way, but now you get to use decimal points in the numbers. This is a similar program that does addition and subtraction on doubles.

class AddDoubles {

  public static void main (String args[]) {

    double x = 7.5;
    double y = 5.4; 

    System.out.println("x is " + x);
    System.out.println("y is " + y);
  
    double z = x + y;
    System.out.println("x + y is " + z);
    
    z = x - y;
    System.out.println("x - y is " + z);

  }

 } 

Here's the result:

% javac AddDoubles.java
% java AddDoubles
x is 7.5
y is 5.4
x + y is 12.9
x - y is 2.0999999999999996

Previous | Next | Top | Cafe au Lait

Copyright 1997, 1999, 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 12, 2002