Why are primitive floating-point types, when they are divided and multiplied by several multiple of 10, displayed in scientific notation?

In the example below, you can see that Java has a peculiarity when displaying the result in certain types of operations with primitive floating point types, such as dividing by multiples of 10:

int num1 = 5;
float num2 = num1 / 10000f;
System.out.println (num2);

As can be shown here , the result 5.0E-4, not 0.0005.

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

There is a piece of documentation that states:

(...) If m is less than 10-3 or greater than or equal to 107, then it is presented in the so-called "computerized scientific notation."

Is there any agreement or official motive for the language to accept such a display in the case mentioned in the quote? Or is it just for reading?

Note. This function does not occur with types intand long, such as here.

+4
source share
2 answers

I don't have a link to the doc for this, but overall it can be difficult to read very large or very small numbers. The reason is that, as you can test yourself, it is difficult to count the number of zeros when that number becomes large. In fact, one of the points of scientific notation is to avoid writing insignificant zeros.

5.0E-4, 0.0005. 5 , , . 5.0E-4, E-4, 5.0.

, Java , , , Java.

+4

, , ( String.format), %e %f . ,

System.out.printf("%.4f\n", num2);

, , , "" . . , C %g:

; e ( E) , , , -4 .

( "" - , , %20.11g 11.)

, -4, "F", "E" "G". , , , 0. , , 4 , e-05: 1e-05 , 0,00001.

"G" , , 1960- Fortran. IBM Fortran 4 , .

, , . . , , , , .

+1

Source: https://habr.com/ru/post/1696220/


All Articles