This example would have no loss, this conversion is the call widening, then he’s taking something narrow and making it wider. You take something that exists in the other kind.
Accuracy is detail, the more accurate the closer to the number you need. Accuracy is not accuracy, this feature requires that the number is exact what it needs.
Loss of information or magnitude is you have a value 1000 for example and for some reason it turn 100, or -200 turn 200.
The integer allows numbers between -2,147,483,648 and 2,147,483,647. This is because all these numbers fit into slots from 2 to 32, so we have just over 4 billion slots. Where does this number come from? 2 is the possible values of the binary numbering system, and 32 is the amount of bits an integer has (4 bytes).
A Single
has also 32 bits, so you can imagine that because it has a decimal part it cannot have all these more than 4 billion integers. Don’t get me wrong, he can go beyond these limits of 2 billion positive and negative, but he can’t represent all the possible integers. It skips a few. It does this because it doesn’t depend on being precise, you can work with an approximate number and that’s okay. It has a complicated way for most people to understand what the possible numbers are, and what they are depends on some factors of the number, only occurs in very large numbers.
In some cases it may be that you can’t represent number 3, but you would get 3.0000000000002 (just one example, I’m not getting a real number that would happen this). That number is almost 3, but not 3. That’s why cannot use this type for monetary values. In certain problems almost 3 is good and serves the purpose. Note that in small numbers there will never be loss, all integers can be represented. Only with very large numbers would he have to use tricks that would not represent the exact number.
The Double
has 64 bits so it can represent all over 4 billion possible integers, plus a lot of others, and can also represent decimal part. That’s why he never loses that kind when he converts from Integer
.
But a Long
for Double
can have loss because this type also has 64 bits and can behave much more integers, so of course another type that can have whole part and decimal part of the same size in memory does not have to represent everything.
The Decimal
has 128 bits, so it’s even worse.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.
– Maniero