Atom Feed SITE FEED   ADD TO GOOGLE READER

Floating point equality

In working through some bugs in Harmony, one thing that's been consistent is my surprise. Floating point's special values slightly complicate things...

Negative Zero
Floats and doubles have two distinct representations for nada: 0.0F and -0.0F.

NaN
Rather than risking an ArithmeticException, floats model error values natively. There are multiple binary representations of NaN; use floatToRawIntBits() to see yours. Most developers will prefer floatToIntBits(), which returns the same bits for all NaNs.


There's also a variety of ways to store floating point values. Each representation has its own merits and drawbacks:
Primitives
Fast, memory efficient, and nonnull.

In wrappers
Classes like Float and Double can be used in collections. Wrapper variables are nullable and implement convenient APIs from Number and Comparable.

In buffers
FloatBuffer and DoubleBuffer store nonnull values in bulk. Buffers provide direct access to data in files and streams.


Unfortunately, the interplay between the values and their representation is imperfect. Consider how equality differs by value and representation.
ValuesPrimitives
==
In wrappers
Object.equals()
In buffers
Buffer.equals()
-0.0, +0.0equalnot equalequal
NaN, NaNnot equalequalequal

This behaviour is inconsistent but I suspect the differences weren't introduced lightly. Primitive types need to work in expressions so that 5/0 doesn't equal 0/0. But wrapper types need to work in collections so that Set.add() is consistent with Set.contains().

On their own, exotic values and exotic representations both make sense. But since complexity is the N×M product of the feature set, our result is inevitable pain. Would be language designers should prefer fewer representations and fewer special values.

iPhone Storage

John Gruber praises the iPhone's lack of upgradeable storage:
I simply don’t understand why Motorola doesn’t follow Apple’s lead and provide ample built-in storage rather than relying on removable SD cards.

He overlooks an obvious benefit of having SD cards for storage: they're cheaper! Compare what it costs to equip a device with a substantial amount of memory:
iPhone 3G SDroid
16GB$199$199
32GB$299$279

Apple has a long history of gouging its customers on non-upgradeable memory. 32GB of data is the maximum amount that iPhone users may accumulate. Droid users who have >32GB of data can shuffle multiple cards.