Course / The terminal and the language
When a number does not fit
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach lab: download it, edit the file named in the instructions, then submit.
gonomicon fetch week-1/08-04-small-box-overflow
cd 08-04-small-box-overflow
gonomicon submitGoal
Fill in three functions that show how int8 and uint8 hold their biggest value, and what happens when you push one past it.
Example
If a function were meant to show the same wrap for int16, you would write:
func WrappedInt16() int16 {
var y int16 = 32767
y = y + 1
return y
}
32767 is the biggest int16 can hold. Adding 1 pushes it one past the top, and it wraps around to the smallest value the type can hold.
Rules
- Edit
sizes.go. Each function has a// TODOcomment and a return line you will change. Largestshould declare anint8variable holding the biggest value anint8can hold, and return it. Write it asvar x int8 = ...or asx := int8(...): either way the variable must be anint8.Wrappedshould declare anint8variable holding127, add1to it, and return the result.Byteshould declare auint8variable holding the biggest value auint8can hold, and return it.- You can write the limit as a number or use
math.MaxInt8/math.MaxUint8from the lesson. - The function names and what they return are fixed. Do not change the lines that start each function.
What you'll practice
Picking int8 and uint8, and watching a value silently wrap when it overflows.
Run it
gonomicon fetch week-1/08-04-small-box-overflow
cd 08-04-small-box-overflow
gonomicon test
gonomicon submit
Hints
- Largest and Byte just need the biggest value their type can hold — you can write the number directly or use math.MaxInt8 / math.MaxUint8.
- Wrapped needs a real int8 variable holding 127, with 1 added to it afterward. The wrap only happens if the addition itself happens inside an int8 box, not a wider one.
- var largest int8 = 127 (or math.MaxInt8) and return it. var wrapped int8 = 127; wrapped = wrapped + 1; return wrapped. var b uint8 = 255 (or math.MaxUint8) and return it.