I'm stumped by the meaning of "overall magnitude" in the Java language specification:
Widening primitive conversions do not lose information about the overall magnitude of a numeric value.
Is it "order of magnitude", or "absolute value"? Or something else?
From the Wikipedia article, Magnitude (mathematics):
In mathematics, magnitude is the size of a mathematical object, a property by which the object can be compared as larger or smaller than other objects of the same kind.
In plain English, the magnitude of 32767 is 32767. The magnitude is equivalent to the value of the number.
In the JLS specification, I think they are using magnitude to mean the number of digits in the number. 32767 is the largest integer than can fit into a signed 16 bit field. If you move 32767 into a bit field with less than 15 bits, the number won't be 32767 any longer. That's called narrowing. If you move 32767 into a bit field with more than 15 bits (or 16 bits, signed), the value of 32767 will be retained. That's called widening.
An order of magnitude is the addition or subtraction of a digit from the number. For example, using base 10 integers, 32767 is an order of magnitude higher than 3276. 3276 is an order of magnitude higher than 327.
Okay, here's another attempt at a formal definition.
If f(x)
is a widening conversion then for every x <= y
, f(x) <= f(y)
. In other words, widening conversions preserve a partial ordering of the values.
That's what not losing information about the overall magnitude might mean.
Loss of precision in this framework means that for some x < y
, f(x) = f(y)
.
The only problem with this definition is the (float)Double.MAX_VALUE
overflow resulting Float.POSITIVE_INFINITY
, which technically fulfils the criteria above but shouldn't really count as not losing information about the overall magnitude.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With