Home : Tutorials : Java Tutorial


created 02/15/98; revised: 07/31/99; 01/18/00; 06/01/00; 05/11/03

CHAPTER 11 — Floating Point

The Java primitive types include the floating point types float and double. Floating point numbers are those that include a fractional part. On paper, a floating point number includes a decimal point. For examples, 3.14159 and -0.718802.

In main storage, and in disk storage, a float is represented with a 32-bit pattern and a double is represented with a 64-bit pattern. For input from the keyboard, character data must be converted to floating point data. For output to the monitor or to a text file, floating point data are converted to characters.

All of the familiar mathematical functions such as sine, log, and square root are available to your program in the Java Math class. These functions typically expect floating point data.

If you don't like math, you can skip most of this chapter with little damage. But read the first few pages about how to read floating point numbers.

Chapter Topics:

  • Converting strings to double.
  • Keyboard input of floating point numbers.
  • The Math class.
  • Square root.
  • Trig functions.
  • PI
  • Not a Number (NaN).

Some of the methods in this chapter work only with Java versions 1.2 and higher.

QUESTION 1:

The data type int and the data type float both use 32 bits.

Is the pattern for the int value 221 the same as the pattern for the float value 221.0?


Home : Tutorials : Java Tutorial

 

Copyright© 1998-2004 All Rights Reserved. No portion of this site may be reproduced or redistributed without prior written permission from VistaEdge Technologies

All registered trademarks appearing on this site are the property of their respective owners. Java is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries. This site is not connected to Sun Microsystems, Inc. and is not sponsored by Sun Microsystems, Inc.