12
In this answer we can see that Java has a peculiarity when displaying the result in certain types of operations with primitive types that have floating point, such as division by multiples of 10, which can be seen in the example taken from the linked question, below:
int num1 = 5;
float num2 = num1 / 10000f;
System.out.println(num2);
How can it be seen here, the result is 5.0E-4
and not 0.0005.
I believe that this is a scientific notation, which means 5.0 * 10^-4
, that leads to the same value, but I don’t understand why Java makes this change in the display.
In the linked answer, there is an excerpt from the documentation that says:
(...) when the magnitude of the value is less than 10 -3 or more than 10 7 the value will be displayed with the scientific notation.
Is there any convention or official reason for language to adopt this kind of display in the case mentioned in the citation? Or as stated in the answer, it is only by readability?
Note: this characteristic does not occur with types
int
andlong
, how can it be seen here
I lean towards readability purely.
0.0000000523
or100000000
are very difficult to read and have a sense of greatness whereas5.23 × 10^-8
and1 × 10^8
are much easier.– Isac
It seems to me that this is an inheritance of
printf("%g", f)
of C. In it, it is specified that the shortest representation will be assumed between the absolute model and where magnitude. And the turning point is precisely0.001
, which has 4 characters, for1e-3
, with four as well. Therefore, the smallest decimal number would imply needing to be described with the notatione
to be the smallest possible string– Jefferson Quesado
In the documentation of
Float.toString
there’s this stretch: "How Many digits must be printed for the fractional part of m or a? There must be at least one Digit to represent the fractional part, and Beyond that as Many, but only as Many, more digits as are needed to uniquely distinguish the argument value from Adjacent values of type float." (adapted); which gives, in a way, support to Jefferson’s comment about always displaying as few characters as possible - in the excerpt it is talked about the number of decimals, but the idea is the same: to display what matters without introducing noises.– Woss
@Jeffersonquesado Still there is something inconsistent in this logic because
100000.0
is written as100000.0
instead of1.0E5
that would be shorter.– Isac
Maybe it’s in order to save memory... https://dealunoparaaluno.wordpress.com/2013/04/03/os-8-datatype/
– user75204