3.7 Floating-point numbers (double)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

0.0/0.0

Not a number Floating-point division of zero by zero is a special case that results in not a number, or NaN.

Which statement best assigns the variable? cityRainfall is of type double.

cityRainfall = 0.97; Best to have the 0 before the decimal point so that the decimal point isn't overlooked. Just .97 might be seen as 97 by a person reading the code.

Which statement best assigns the variable? Both variables are of type double.

cityRainfall = measuredRain - 5.0; Best to use a floating-point literal like 5.0, rather than an integer literal like 5, when dealing with floating-point variables.

A person's height in centimeters.

double A measurement.

The current temperature in Celsius.

double A measurement.

The average number of kids per household.

double Nobody has exactly 2.2 kids, but the average almost certainly should involve a fraction.

3.7.2: Floating-point literals. Which statement best declares and initializes the double variable?

double currHumidity = 99.0; A floating-point literal should have a fractional part, even if 0.

Declare a double variable named packageWeight and initialize the variable to 7.1.

double packageWeight = 7.1; The compiler will allocate a particular memory location for packageWeight and store 7.1 in that memory location.

Declaring and assigning double variables.

double personHeight; The compiler will allocate a particular memory location for personHeight.

Floating-point versus integer. Choose the best type for a variable to represent each item. 1)The number of cars in a parking lot.

int Countable: 1 car, 2 cars, 3 cars, etc.

The number of hairs on a person's head.

int The number of hairs may be large, but is still countable: 1 hair, 2 hairs, etc.

A floating-point literal

is a number with a fractional part, even if the fraction is 0, as in 1.0, 0.0, or 99.573. Good practice is to always have a digit before the decimal point, as in 0.5, since .5 might mistakenly be viewed as 5. Scanner's nextDouble() method reads a floating-point value from input. Ex: currentTemp = scnr.nextDouble(); reads a floating-point value from the input and assigns currentTemp with that value.

Sphere volume Given sphereRadius, compute the volume of a sphere and assign sphereVolume with the result. Use (4.0 / 3.0) to perform floating-point division, instead of (4 / 3) which performs integer division.Volume of sphere = (4.0 / 3.0) π r3 (Hint: r3 can be computed using *)

sphereVolume = (4.0 / 3.0) * Math.PI *(sphereRadius * sphereRadius * sphereRadius);

A floating-point number

is a real number containing a decimal point that can appear anywhere (or "float") in the number. Ex: 98.6, 0.0001, or -55.667. A double variable stores a floating-point number. Ex: double milesTravel; declares a double variable.

Choosing a variable type (double vs. int) A programmer should choose a variable's type based on the type of value held.

-Integer variables are typically used for values that are counted, like 42 cars, 10 pizzas, or -95 days. -Floating-point variables are typically used for measurements, like 98.6 degrees, 0.00001 meters, or -55.667 degrees. -Floating-point variables are also used when dealing with fractions of countable items, such as the average number of cars per household.

0.0 / 5.00

0.0 0.0 divided by 5.0 is 0.0.

3.7.4: Floating-point division. 13.0 / 3.0

4.333333 Floating-point division retains the fractional value.

Floating-point division by zero

Dividing a nonzero floating-point number by zero is undefined in regular arithmetic. Many programming languages produce an error when performing floating-point division by 0, but Java does not. Java handles this operation by producing infinity or -infinity, depending on the signs of the operands. Printing a floating-point variable that holds infinity or -infinity outputs Infinity or -Infinity. If the dividend and divisor in floating-point division are both 0, the division results in a "not a number". Not a number (NaN) indicates an unrepresentable or undefined value. Printing a floating-point variable that is not a number outputs NaN.

12.0 / 0.0

Positive Infinity Dividing a positive floating-point value by 0.0 results in positive infinity.

Floating-point for money

Some programmers warn against using floating-point for money, as in 14.53 representing 14 dollars and 53 cents, because money is a countable item (reasons are discussed further in another section). int may be used to represent cents or to represent dollars when cents are not included as for an annual salary, as in 40000 dollars, which are countable.

Assign ballRadius with ballHeight multiplied by one half, namely (1.0 / 2.0). Use the parentheses around the fraction.

ballRadius = ballHeight * (1.0 / 2.0); or ballRadius = (1.0 / 2.0) * ballHeight; The statement first evaluates (1.0 / 2.0), which is 0.5, and then evaluates ballHeight * 0.5. Ex: If ballHeight is 12.0, the expression evaluates to 12.0 * (1.0 / 2.0), which is 12.0 * 0.5, or 6.0.

Assign ballRadius with ballHeight divided by 2.0. Do not use the fraction 1.0 / 2.0; instead, divide ballHeight directly by 2.0.

ballRadius = ballHeight / 2.0; Note that 2.0, not 2, should be used when dealing with floating-point numbers.


Kaugnay na mga set ng pag-aaral

Personal Finance Chapter 3 - Taxes in Your Financial Plan

View Set

Principles of Operations Management

View Set

1.Géneros y Formas Musicales del Barroco

View Set

Scientific Thinking: Can e-Cigarettes Help People Quit or Reduce Smoking?

View Set

Lecture 7 - Conscious and Unconscious Mind Working together

View Set