Why basic math functions aren't overloaded for different types in Java, Math? -
i have interesting question. figured out, math.pow() takes (double, double) args , wonder why there no overloaded functions other types combinations such (int, int) or (int, double) atc ...
i think big hole , paintfull weakness belive there reason. can explain me, please?
consider these 2 methods:
private int simplepow(int x) { return x * x; } private int harderpow(int x) { return (int) math.pow(x, 2); } first 1 faster second... problem is, if know, use power (int, int), calculation effective , don't need take care of problems double variables.
well 1 should concerned (int, int) overload; others take double parameter should result in doubles anyway (double, double) method do. why there no (int, int) overload? because exponentiation grows fast, , since ints 32 bits long, there limited number of parameters such 1 raised other in range. doubles have much larger range don't have similar problem (to clear, doubles can overflow too, can hold maximum value of 1.8e308; larger of int). can of course implement own method take integer power, done here (the accepted answer makes use of exponentiation squaring, can applied (double, int) case).
Comments
Post a Comment