home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
language-affects-choice--arithmetic
Why study programming languages?
You can never understand one language until you understand at least two.
—John Searle
The limits of my language are the limits of my world.
—Ludwig Wittgenstein
Why study programming languages?
-
learn other approaches; lets you understand your native tongue better;
-
practice at learning new technologies/ideas;
-
makes you think about: when is language helping, and when getting in the way;
e.g. sorting: scheme vs Java
-
Help you choose a language appropriate to the task
Languages steer programmer's decisions
Java encourages choosing bad integer arithmetic
- 2 billion + 2 billion: in Java, python, and racket.
By looking at other languages, it's clear this is by no means an intrinsic limitation on computers.
- How to add properly in Java?
Just Use java.math.BigInteger.
Which is easier?
What does the language encourage you to do, for integer arithmetic?
- Why did Java designers choose to make limited-arithmetic the default, and
correct-arithmetic difficult?
- Does it matter?
Ask Boeing about the latest, greatest
787
Dreamliner re-booting.
The Y2K problem is a version of integer-overflow.
Integer overflow can be a security risk,
if large, malicious input can overflow a counter
(e.g. 20-year-old
bug long present in most all decompression software).
Java, python encourage bad rational arithmetic
Consider: 7 · 25 ÷ 25.
Also: 2 + x - x + 3.
Java, python, racket encourage bad irrational arithmetic
What is √2 · √2 ?
(Give the racket expression.)
In sage:
-
sqrt(2)
-
n(sqrt(2))
-
n(sqrt(2), digits=50)
-
sqrt(2) * sqrt(2)
Note a different language issue, in that third example: we provided
the argument “n(…, digits=50)”,
rather than just passing 50 as the second argument.
That's “keyword arguments”; why/when might it be helpful?
(It turns out python
allows this, and sage is built on python.
Why the difference?
You've probably guessed that the different behaviors above stem from how the data is represented
internally.
Java (and many languages) represent primitive ints and floats using
a fixed 32-bits
(and using 64-bits for long and double).
If a number can't fit in 32-bits, it can't be represented by that type.
For arbitrary-precision integers,
you can keep track of a list-of-digits
This is what racket and python do by default, and what Java uses
java.math.BigInteger for.
In the same vein, representing
(In dynamically-typed languages: the machine representation might include a tag, which requires a
few bits.
For example, a 32-bit quantity starting with the bits 101 might happen to let the
language know that this quantity is the start of a string.
For common types, a shorter tag might be used;
since int is a very common type, a single leading bit of 0
might indicate a small-integer (one that fits in the remaining bits),
and that lets the language-implementation use existing integer-arithmetic-hardware
for that value!)
Java encourages you to use arrays (even when lists are better)
Suppose you want to keep list of temperatures on recent days (in Celcius, of course).
List<Integer> nums = new ArrayList<Integer>();
nums.add(22);
nums.add(17);
nums.add(24);
nums.add(30);
nums.add(5);
if (nums.contains(n)) {
...
}
|
Oh my goodness!
What if you want to use an array?
Java makes that much easier for you:
Integer[] nums2 = {22, 17, 24, 30, 5};
|
So if you want to sort a bunch of items, it's concise to use an array,
and verbose to use a List — Java is encouraging the programmer to use
an array, even if a List is more appropriate.
It's worth mentioning that although Arrays are easy to declare, iterate over, and statically-initalize in Java,
they're also a bit underdone:
there are many useful functions for arrays
that Java didn't include (not even when they added the utility-class java.util.Arrays);
a contains method is one of them (!).
You can download a
third-party libraries
(like guava
or apache-commons
(written since others were fed up with this shortcoming),
or you can often convert the array to a list, and then use the standard java.lang.Collections methods:
Integer[] nums2 = {22, 17, 24, 30, 5};
List<Integer> nums1 = java.utils.Arrays.asList( nums2 );
|
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)