Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is scientific notation interpreted as int or float?

If I hardcode a number in my code using the scientific notation (e.g. 1e9) what will the type of that number be (int, long, float, double..)?

When the significand or the exponent are floating point numbers it can't be an integer obviously, but what in the above case?

like image 262
Jawap Avatar asked Mar 20 '13 15:03

Jawap


People also ask

Is scientific notation an integer?

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

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.

What format is 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.


2 Answers

The e makes it a floating-point literal. From the JLS (§3.10.2. Floating-Point Literals):

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d (§4.2.3).

Therefore, 1e9 of type double, as is 1e9d. On the other hand, 1e9f is of type float.

like image 84
NPE Avatar answered Oct 03 '22 15:10

NPE


These will usually be of type double. If you put an f (of F) behind it, it's a float.

double d = 1e9; 
float f = 1e9f;
like image 23
poitroae Avatar answered Oct 03 '22 15:10

poitroae