“The strongest of all warriors are these two — Time and Patience.” ― Leo Tolstoy, War and Peace
Pete Letkeman wrote:Please remember that a double can handle factions/decimal numbers for instance 3.141592654 as well as whole numbers.
Where as long can not handle factions/decimal numbers, but only whole numbers.
Given your example code x is a whole number of 1000.
It then automatically gets converted to a double when you declare y.
Now y is 1000.00, or something close to that because it's a double.
1000.00 cannot be converted to int without the loosing the decimal places. In doing so you may lose some precision.
...
“The strongest of all warriors are these two — Time and Patience.” ― Leo Tolstoy, War and Peace
Daniele Barell wrote:So double cannot get back to long because of the floating point
Campbell Ritchie wrote:You are not using the number as a long. 1_000 is an int literal, which requires conversion to a long before you can use it. The long form of the number is 1_000L.
Campbell Ritchie wrote:What PL has said is the explanation for a basic language requirement: a double is regarded as a “wider” datatype that a long, so you can implicitly widen one way, but you cannot convert the other way without a cast. As PL has hinted, the cast will lose any fractional part of the number.
Campbell Ritchie wrote:You can also get loss of precision the other way; it is possible to fit 1234567891011121314L into a long, but there is no guarantee that it can be exactly represented as a double.
Liutauras Vilda wrote:
Daniele Barell wrote:So double cannot get back to long because of the floating point
It cannot go back without losing precision, obviously. In general it can, you just need explicitly cast it to long.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
That code has an implicit cast it. You have an int expression to the right of the = sign and the JVM will interpret that asDaniele Barell wrote:. . . I mean I can use x without convert the literal to long:
If you scroll down a bit more, you will find there are also 22 “narrowing primitive conversions”. You will see from the example that they all require casts; if you attempt any of those conversions without a cast, the compiler must fail to accept the code, but the compiler error message about “possible loss of precision” isn't very clear.A widening primitive conversion from int to float, or from long to float, or from long to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value.
As it said earlier, “widening” conversions are always permitted.. . . So why the compiler enables me to convert a long into double? . . .
Because you didn't specify any formatting, the output defaults to printing the fewest numbers necessary to identify the nearest distinct value. That turns out to be 1,234,567,890,101,114,100 but the default format is so‑called scientific format which on paper looks like 1.23456789101112141×10¹⁸, but older computers didn't have the pretty superscripts, so they write E18 instead of ×10¹⁸. Read that as meaning you move the decimal point 18 places to the right; in this case you will have to add a few 0s on the right.. . . What's the meaning of the last 'E18'?
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.