scala.Math apparently has two methods named round. But the documentation looks wrong to me.
Am I confused?
The method declared to return type Int is documented to return the closest Long.
How can I specify which method I want to call if they only distinguish by return value? In the following local function, scale is a value declared a few lines up as Double. My intention is to round to the nearest integer, but IntelliJ tells me that my function declared as returning Int does not conform to the type Long. What’s the correct way to fix my problem?
Thanks for the push into the right direction. But I’m still not quite seeing how to make
the result of my calculation be an Int. What should I change in the following function so that the function really is (Int,Int,Double)=>Int ?
I’ll just throw out that the operations you are doing, like min, max, and round, are also methods on Double and Float (Int and Long have min and max). So you could consider writing your method in the following way.
0L max 255L min lower + ((upper - lower) * scale).round
Note that I had to make the bounds be Longs because min and max require the same types.
As for getting out an Int, it has already been asked how you want to handle overflow because Double can hold larger whole numbers than Int can. The simplest approach is the toInt method, which just ignores overflows, and the value wraps around. However, if you call toInt after your min and max have occurred, you know that the value is between 0 and 255, so there is no possibility that part of the expression can overflow and toInt is safe.
(0L max 255L min lower + ((upper - lower) * scale).round).toInt
Granted, the round can still have problems if the Double expression is larger than Long.MaxValue. The round method simply truncates, so 1e100.round is Long.MaxValue.
Why do I need Int? It is because I’m using another class which I didn’t write, and I am forbidden to change, that class needs its fields as Int’s.
But even that is curios to me. The class that I’m using is a Color class which has red, green, and blue fields which are Int between 0 and 255. Is Int really the best numerical value for values x: 0 <= x <= 255 ? Even if that’s not appropriate, I can’t change it. But I am curious.
Is Int the most efficient? Short also covers 0 to 255. A decade or two
ago, most hardware architecture was 32 bit, so Float/Int were likely the
most efficient types. Now, most architectures are 64 bit, so now
Double/Long are probably the most efficient, but code remains that uses
Float/Int.