Why are floating-point primitives divided or multiplied by certain multiples of 10 displayed in scientific notation?

13

In this answer we can see that Java has a peculiarity when displaying the result in certain types of operations with types primitives that have a 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);

Since it can be seen here , the result is 5.0E-4 and not 0.0005.

I understand that this is a scientific notation, which means 5.0 * 10^-4 , which leads to the same value, but I did not understand why Java makes this change in the display.

In the linked response, there is an excerpt from the documentation that says:

  

(...) When the magnitude of the value is less than 10 ^ -3 or greater than 10 ^ 7 the value will be displayed with scientific notation.

Is there any official convention or motive for the language to adopt this type of display in the case mentioned in the citation? Or as stated in the answer, is it just for readability?

  

Note: This feature does not occur with types int and long , as can be seen here

    
asked by anonymous 21.12.2017 / 20:00

2 answers

3

It is purely arbitrary, each language has its own criterion, and aims to prevent a very large or very small number from generating a very large string (imagine 10 ^ 300, which is 1 followed by 300 zeros).

Now, this is the default formatter, which you never use in a "real" program, except for debugging. When showing the value to the user you should always use a specific formatter, then you take responsibility eg in an accounting program you can allow the display of very large numbers (up to trillion, maybe) but always with 2 fixed decimals

    
14.03.2018 / 08:35
1

It should be an inheritance of the c ++ language project, where an integer has the same memory space as a float.

    
14.02.2018 / 19:43