The idea of casting is to convert data to another type with little to no data loss via said conversion. To be all technical, we'll quote the Casting and Type Conversions page:
Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.
We'll focus here on "no data will be lost."
I think the issue you're facing here is where that conversion is happening, and more importantly, where we want to avoid data loss.
Following your example, a double
10.43245 cast to a long
, we face a case where no matter what, there's a type-implicit loss of data: 10.43245 is not 10, nor is it your random string of digits. double
will innately store more digits post-decimal place (particularly since long
stores a solid zero), so converting this number will, no matter what, cause a loss of data.
As an aside, it's kind of like converting spoken words into Morse Code. We do it, because it's useful. But we still lose some valuable qualities of speech in the process. It's a tradeoff, where we choose to favor the importance of data-transmittance over the importance of conveying speech in a natural and emotional way.
So after the fact is accepted that we're facing data loss, the choice has to be made of where makes most sense to lose that data. We can lose it in a human-readable form, by preserving the bit-order and all that, that's where you get your random string of digits, or we can lose it in its original, binary form and preserve "as much of the number" as we can, which is, in this case, the floor: ten.
In essence, it's just another simple tradeoff that was made when the first languages were being established, and nobody wants to change a good thing. It's way, way, way more useful to have (long)10.43245
resolve as 10
.
It's also important to remember, as a couple other's have pointed out, that this is just the default behavior. Because this cast exists, there's no reason to assume that you just can't convert direct binary values from one type to the other. There are methods in many languages, like .NET's BitConverter.DoubleToInt64Bits
, which will do this one-to-one bit conversion. It just doesn't make sense as a typical use case, and language design, particularly for high-level languages like we primarily deal with, is a matter of making the typical use case as efficient and effective as we can.