+50 XP
instructions06 / 63
taught in: basic types
Easytypesnumbersmath~15m+50 XP
Type Conversions
Go has no implicit numeric conversion. An int is not an int64, and neither
is a float64. If you want a value in another type you say so, every time:
var n int = 7
var f float64 = float64(n) // float64(n), never just n
That looks like noise until you meet the bug it prevents. In a language that
converts silently, sum / count quietly gives you an integer division, or a
number too large for its new type wraps around without a word. Go makes both of
those visible: the first will not compile, and the second is spelled out in the
conversion you had to write.
Two things a conversion does that are worth knowing before you write one:
int(f)on a float truncates toward zero. It does not round.int(2.9)is2, andint(-2.9)is-2.int8(n)on a value too big forint8wraps, silently, using the low bits.int8(200)is-56. Nothing panics. If the value has to survive the trip, you check the range yourself.
Task
Finish the three functions in main.go.
Truncate(f float64) intdrops the fractional part toward zero.Average(nums []int) float64returns the mean. An empty slice averages to0. Watch the division: the whole point of this one is that the sum and the count are bothint.FitsInt8(n int) (int8, bool)returnsint8(n)and whether that conversion kept the value. Out of range, return the wrapped result Go gives you andfalse- the caller wants to see what happened, not a zero.
Hints
math.MinInt8andmath.MaxInt8are the bounds, and they read better than-128and127.main.godoes not importmathyet, so add it: an import you do not use is a compile error in Go, which is why it was left out.- Convert before you divide, not after.
float64(sum / len(nums))does the integer division first and then converts a result that already lost its fraction.float64(sum) / float64(len(nums))is the one you want. Average(nil)must return0rather than dividing by zero. Integer division by zero panics; float division by zero would give youNaN. Neither is an average.- The tests compare floats with a tolerance rather than
==, because binary floating point rarely lands on an exact decimal. Do the same in your own code when you get there.