Is scientific notation interpreted as int or float?
Asked Answered
G

5

13

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?

Generalist answered 20/3, 2013 at 15:1 Comment(0)
V
17

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.

Vinavinaceous answered 20/3, 2013 at 15:3 Comment(0)
A
5

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;
Arnhem answered 20/3, 2013 at 15:2 Comment(0)
W
4

It will be considered as a floating point literal, and without a trailing f or F it will be a double.

Integer literals can't use scientific notation.

Weald answered 20/3, 2013 at 15:3 Comment(0)
E
3

It'll be 'double' unless you use 'f' or 'F' then it'll be float literal.

Electroballistics answered 20/3, 2013 at 15:4 Comment(0)
A
0

It is better to use Double. Because a it has decimal floating point is a computer arithmetic system closely related to scientific notation. for example: 0.000 000 007 51 in scientific notation it is 7.51×10-9. when you use it as int, 7.51 would be 7 while when you use int it will be 7.51.

Adverb answered 20/3, 2013 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.