Lesson 1.11
Keeping the fraction; when conversion loses information
Part 1: Programs that talkReading, about 4 min2 exercises in this lesson
Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.
Keeping the fraction and when conversion loses information
By the end of this you will know when a division keeps its fraction and when it drops it, and be able to explain what is lost when a decimal is turned into a whole number.
Keeping the fraction
In the last lesson, whole-number division threw away the fraction: 7 // 2 gave 3. Sometimes you need the 3.5.
In Python, plain / always gives a decimal: 7 / 2 is 3.5, even though both sides are whole numbers. Even 9 / 3 is 3.0, still a decimal, not 3.
In Java, / between two ints gives a whole number. To keep the fraction, make at least one side a double.
This program divides 7 by 2 and keeps the fraction:
total = 7 count = 2 avg = total / count print(avg)
public static void main(String[] args) {
int total = 7;
int count = 2;
double avg = (double) total / count;
System.out.println(avg);
}It prints 3.5. In Java, the (double) before total turns it into a decimal before the division happens. Without it, Java computes 7 / 2 as 3 and then stores 3.0 in the double, which is too late.
What conversion loses
Turning a decimal into a whole number drops the fraction. Python's int(7.9) gives 7, not 8: it cuts toward zero, it does not round. In Java, (int) 7.9 does the same.
This is called a lossy conversion: information (the .9) is gone and cannot be recovered. An exam question may ask you to identify when this happens and what is lost.
Now try it yourself on the questions below.
Exercise 1.11.11.11.1 · Write itSplit bill
Write a program that reads a total bill (a whole number) on the first line and the number of people (a whole number, at least 1) on the second line, and prints each person's share on one line.
- The share is the total divided by the number of people, keeping the fraction.
- Print only the share, as a number. Spaces at the end are ignored.
Your program is run several times. Only what it prints is checked.
In Python, plain / keeps the fraction. In Java, at least one side of the / must be a double: cast the total with (double) before dividing.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
a total that does not halve evenly (rule 1)
You type
- typed: 9
- typed: 4
It prints
2.25
a total that does not divide evenly (rule 1)
You type
- typed: 10
- typed: 4
It prints
2.5
Exercise 1.11.21.11.2 · Answer itData loss
A program stores a temperature reading of 36.7 as a float, then converts it to an int.
(a) State the value after the conversion.
(b) State what information is lost by the conversion.
[2]