Java Number Cruncher. The Java Programmer's Guide to Numerical Computing
Authors: Mak R.
Published year: 2001
Pages: 14-17/141
Buy this book on amazon.com >>
   

 
Java Number Cruncher: The Java Programmer's Guide to Numerical Computing
By Ronald  Mak

Table of Contents
Chapter  1.   Floating-Point Numbers Are Not Real!


1.6 And What about Those Integers?

We've seen how the floating-point numbers can get us into trouble if we're not careful. We may reasonably conclude that, at best, the floating-point numbers are only a crude simulation of the real number system. It's quite amazing that we can make them work as well as they do, as we'll see in the following chapters. But what about the integer numbers?

Java represents the whole numbers with integer types. Integer numbers are certainly simpler and generally faster than floating-point numbers in computations , but are they absolutely safe to use? Of course they're not. After all, we are dealing with computers here. We'll examine the integer types in detail in the next chapter before we return to floating-point numbers in Chapter 3.


   
Top
 
   

 
Java Number Cruncher: The Java Programmer's Guide to Numerical Computing
By Ronald  Mak

Table of Contents
Chapter  1.   Floating-Point Numbers Are Not Real!

References

Chapra, Steven C., and Raymond P. Canale, Numerical Methods for Engineers , 3rd edition, New York: WCB/McGraw-Hill, 1998.
Chapra uses a clever illustration to explain the differences between accuracy and precision on pages 59 C60.

Hamming, Richard W., Numerical Methods for Scientists and Engineers , 2nd edition, New York: Dover, 1986.
This text discusses the quadratic formula and how to rearrange formulas in general in Sections 3-1 through 3-5.

Sterbenz, Pat H., Floating-Point Computation , Englewood Cliffs, NJ: Prentice-Hall, 1974.
This entire book is on floating-point arithmetic. It proves that floating-point arithmetic fails certain laws of algebra in Section 1.6.


   
Top
 
   

 
Java Number Cruncher: The Java Programmer's Guide to Numerical Computing
By Ronald  Mak

Table of Contents
Part  I.   Why Good Computations Go Bad


Chapter 2. How Wholesome Are the Integers?

Compared with what we saw of the floating-point types in Chapter 1, the integer types must seem pretty tame and safe! We'll see how valid that assumption is in this chapter.

Java has five integer types named by the keywords byte, short, char, int, and long. The values of these types represent the whole numbers of pure mathematics, which we use for counting and for doing arithmetic that doesn't involve fractions. We must use the word represent because Java's integers, unlike the whole numbers, are not infinite.


   
Top
   

 
Java Number Cruncher: The Java Programmer's Guide to Numerical Computing
By Ronald  Mak

Table of Contents
Chapter  2.   How Wholesome Are the Integers?

2.1 The Integer Types and Operations

Table 2-1 shows the bit size , the minimum value, and the maximum value of each of Java's integer types.

Except for type char, which does not have negative values, the absolute value of the minimum value of each type is one greater than the maximum value of that type. (We'll see in Section 2.2 why this is so.) Except for char, exactly one half of each type's values are negative, and the other half consists of 0 and the positive values.

Table 2-1. Java's integer types.

Integer Type

Size (Bits)

Minimum Value

Maximum Value

byte

8

-128

127

short

16

-32,768

32,767

char

16

65,535

int

32

-2,147,483,648

2,147,483,647

long

64

-9,223,372,036,854,775,808

9,223,372,036,854,775,807

Table 2-2. The type of the result depends on the types of the operands when performing the integer addition, subtraction, multiplication, division, and remainder arithmetic operations.

Operand 1

Operand 2

Result

int

int

int

int

long

long

long

int

long

long

long

long

Java supports various arithmetic operations on int and long values. At run time, it automatically converts byte, short, and char values to int before it performs an operation. The additive operations are addition and subtraction, and the multiplicative operations are multiplication, division, and remainder. The postfix operations are postincrement and postdecrement, and the unary operations include negation, pre-increment , and predecrement.

The result type of a postfix or a unary operation is the same as the type of its single operand. Table 2-2 shows how the result type of an additive or a multiplicative operation depends on the types of its two operands.

The result is type int only if both operands are type int. Even if we multiply two int values, the product is an int, not a long. Integer arithmetic never throws an exception if an overflow occurs. (We'll see in Section 2.3 what really happens during an overflow.) The only exception that it does throw is an ArithmeticException if we attempt to divide by zero during a division or a remainder operation.


   
Top
 
Java Number Cruncher. The Java Programmer's Guide to Numerical Computing
Authors: Mak R.
Published year: 2001
Pages: 14-17/141
Buy this book on amazon.com >>

Similar books on Amazon