I have been programming in google Golang and have been enjoying it due to its brevity but I find it surprising that almost all its Math standard library methods are for the floating point type. Is there any particular reason why these methods do not exist for ints?
The short answer is that Go is a successor to C, and C's standard math library is also defined almost exclusively in terms of single- and double-precision floating point values. The longer answer is that in a statically-typed language without polymorphism or function overloading, like Go (or C), you need to settle on a type for a function to take and return in advance, and once you're going to deal with specific types in your math library, there are a lot more interesting operations on floating point numbers than integers. To pick a few examples
Note that this is not as onerous as it might seem if your input values are integers -- an integral value can be converted to a floating point value with a simple typecast, and mostly accurately. So that's the story for languages like Go or C. Other languages have other options:
Go has none of these features, however. |
||||
|