Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Why can't I declare integer types using scientific notation?

I can easily read 2e15 as "two quadrillion" at a glance, but for 2000000000000000 I have to count the zeroes, which takes longer and can lead to errors.

Why can't I declare an int or long using a literal such as 2e9 or 1.3e6? I understand that a negative power of 10, such as 2e-3, or a power of 10 that is less than the number of decimal places, such as 1.0003e3, would produce a floating point number, but why doesn't Java allow such declarations, and simply truncate the floating-point part and issue a mild warning in cases where the resulting value is non-integral?

Is there a technical reason why this is a bad idea, or is this all about type-safety? Wouldn't it be trivial for the compiler to simply parse a statement like

long x = 2e12 as long x = 2000000000000 //OK for long

and int y = 2.1234e3 as int y = 2123.4 //warning: loss of precision

like image 1000
odougs Avatar asked Jul 14 '13 05:07

odougs


People also ask

Can an integer be in scientific notation?

N is a number, either an integer or decimal, between 1 and 10.

How do you write numbers in scientific notation in Java?

The format of scientific notation in Java is exactly the same as you have learned and used in science and math classes. Remember that an uppercase 'E' or lowercase 'e' can be used to represent "10 to the power of". Scientific notation can be printed as output on the console if it passes a certain number.

How do you format numbers in scientific notation?

The Scientific format displays a number in exponential notation, replacing part of the number with E+n, in which E (exponent) multiplies the preceding number by 10 to the nth power. For example, a 2-decimal scientific format displays 12345678901 as 1.23E+10, which is 1.23 times 10 to the 10th power.

What data type is scientific notation?

Scientific notation is a way to write out a number, but not a specific data type on its own. So 3.6e+3 would be the same as 3600, just different ways of writing the same thing.


2 Answers

It's because when you use the scientific notation you create a floating point number (a double in your example). And you can't assign a floating point to an integer (that would be a narrowing primitive conversion, which is not a valid assignment conversion).

So this would not work either for example:

int y = 2d; //can't convert double to int

You have a few options:

  • explicitly cast the floating point to an integer: int y = (int) 2e6;
  • with Java 7+ use a thousand separator: int y = 2_000_000;
like image 136
assylias Avatar answered Oct 22 '22 12:10

assylias


Because it's a shortcoming of Java.

(Specifically, there is clearly a set of literals represented by scientific notation that are exactly represented by ints and longs, and it is reasonable to desire a way to express those literals as ints and longs. But, in Java there isn't a way to do that because all scientific notation literals are necessarily floats because of Java's language definition.)

like image 27
nmr Avatar answered Oct 22 '22 12:10

nmr