think 10

Decimal arithmetic - Demonstration


The short demonstration program linked from this page shows the results achieved using decimal arithmetic as compared to binary floating point arithmetic.

The program simply starts with the number nine (9), and repeatedly divides it by ten (10), and should display the following result:

    0.9        0.9
    0.09       0.089999996
    0.009      0.0090
    0.0009     9.0E-4
    0.00009    9.0E-5
    0.000009   9.0E-6
    9E-7       9.0000003E-7
    9E-8       9.0E-8
    9E-9       9.0E-9
    9E-10      8.9999996E-10
    
where the left-hand column is the results given using the new BigDecimal class, and the right-hand column is the results obtained using the binary float datatype.

You can use the program for testing your installation of the decimal package; the program is available in both Java and NetRexx:

Java

  1. Here's the source for the program. View it, and save it as decdemo.java in a convenient directory.
  2. If you haven't already done so, download and install the decimal.jar file.
  3. Compile the program using the command
    javac decdemo.java
  4. Run the program using the command
    java decdemo
    This should display the results shown above.

NetRexx

  1. Here's the source for the program. View it, and save it as decdemo.nrx in a convenient directory.
  2. If you haven't already done so, download and install the decimal.jar file.
  3. Compile the program using the command
    nrc decdemo
  4. Run the program using the command
    java decdemo
    This should display the results shown above.


Copyright © IBM Corporation, 1998. All rights reserved.