Golang int to uint8 converison, not detected overflow - go

I have been making 'A Tour of Go' and occurred on some strange behavior inside the Pic function. It concerns int -> uint8 conversion. Values of dx and dy are 256 by default on program execution. So that x+y inside the nested for loop goes up to 510! (255+255)
Nevertheless Golang sees no problem while converting overflowing x+y to uint8, but when I change it into some hard-coded value, lets say uint8(321), I immediately obtain an error of an overflow.
Can someone explain me that strange behavior?
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
canvas := make([][]uint8, dy)
for y := 0; y < dy; y++ {
canvas[y] = make([]uint8, dx)
for x := 0; x < dx; x++ {
canvas[y][x] = uint8(x+y) // <- here it is
}
}
return canvas;
}
func main() {
pic.Show(Pic)
}
Error obtained:
go: finding module for package golang.org/x/tour/pic
go: downloading golang.org/x/tour v0.0.0-20200508155540-0608babe047d
go: found golang.org/x/tour/pic in golang.org/x/tour v0.0.0-20200508155540-0608babe047d
./prog.go:11:24: constant 321 overflows uint8
Go build failed.

This is from the language spec:
The values of typed constants must always be accurately representable by values of the constant type. The following constant expressions are illegal:
uint(-1) // -1 cannot be represented as a unit
int(3.14) // 3.14 cannot be represented as an int
In your case, x and y are int, so x+y is also int, and uint8(x+y) simply truncates the result. However, uint8(321) is not valid according to the language spec. However, this is valid:
i:=321
x:=uint8(i)

Related

How is the value of dx and dy 256 in tour.golang.org/moretypes/18?

I've been learning golang using tour.golang.org.
In the following code, when I print the value of dx and dy which are both of type integer, the value comes out to be 256.
package main
import ("golang.org/x/tour/pic"
"fmt"
)
func Pic(dx, dy int) [][]uint8 {
fmt.Printf("%T\n",dx)
fmt.Println(dx)
ret := make([][]uint8, dy)
for i := range(ret){
ret[i] = make([]uint8, dx)
}
return ret
}
func main() {
pic.Show(Pic)
}
How does the golang compiler know the value of those two integers declared in the function Pic?
From main, you're calling pic.Show with your function, Pic. Looking at the source code for pic.Show, it calls the function it's given with 256 for each dimension.

Why doesn't Scanf() work correctly for me?

I'm trying to move from Python to GO and with my minimal knowledge I tried to make a basic calculator. However i for some reason can't get Scanf to work properly. It only seems to accept the first scanf but the second one is completely ignored
package main
import (
"fmt"
)
var x int
var y int
var result int
var input float64
func add(x int, y int) int {
sum := x + y
return sum
}
func sub(x int, y int) int {
sum := x - y
return sum
}
func div(x int, y int) int {
sum := x / y
return sum
}
func mul(x int, y int) int {
sum := x * y
return sum
}
func main() {
fmt.Println("Which type?\n1: Add\n2: Subtract\n3: Divide\n4:
Multiply")
fmt.Scanf("%d", &input)
fmt.Println("Input numbers seperated by space")
fmt.Scanf("%d", x, y)
switch input {
case 1:
result = add(x, y)
case 2:
result = sub(x, y)
case 3:
result = div(x, y)
case 4:
result = mul(x, y)
}
fmt.Println(result)
}
The second call to Scanf, Scanf("%d", x, y) only provides one conversion specifier but was given two variables.
Moreover, this second call only passes the variables' values, not their addresses.
It seems the correct call would be Scanf("%d %d", &x, &y)
In the first call to Scanf you said: Scanf("%d", &input). The second argument's syntax, & variable, denotes a reference to the named variable.
input was declared global, but is only visible after its declaration. Since input is in scope within main but not within Scanf, in order for Scanf to change the value in another scope, the address must be given as an argument, rather than its value.
The recipient of the address (here Scanf) can then change the value of the variable in the frame in which it is still in scope; in this case, main.
See Go's documentation for a similar explanation: https://golang.org/ref/spec#Address_operators

Golang exercise slices how does it deal with big values

I'm going through the Golang tutorial and I'm a little bit confused as to what it is doing with some of the values in the slices exercise. https://tour.golang.org/moretypes/18
Here is the code that I am confused with:
A value of 0 is a perfectly blue pixel and a value of 255 is a perfectly white pixel. So what is happening here when the value displayed is some form of x*y (I did /20 to make the image a little bit bigger and easier to see).
If you follow the image horizontally, you will see that at some point in the process, the ever increasing x and y values seem to revert to blue (0 value) If I type a static value like 256 in the return I get a compile error. So it obviously does not allow the numbers to go off the scale and revert to 0 or anything. So how does it get the blue curves in the picture?
imported source here: https://github.com/golang/tour/blob/master/pic/pic.go#L15
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
//First, the array has to be made so we can put some values in it later
//This only makes the second dimension of the array ([[uint8 dy]])?
image := make([][]uint8, dy)
//The inputs into the function are Int's, so it is ok to have a non uint8
//loop initializer
for x := 0; x < dy; x++ {
//once we are in the loop we have to make the first dimension of the array
//based on the dx values
image[x] = make([]uint8, dx)
for y := 0; y < dx; y++ {
//This is a function +to assign the pixel values to the array
image[x][y] = uint8((x * y) /20)
}
}
return image
}
func main() {
pic.Show(Pic)
}
Imagine i is of type int, uint8(i) returns Least Significant Byte (LSB) of i:
When x is in range [0, 255] , meaning: 0 <= x <= 255
and y is in range [0, 255],
then x*y is in range [0, 255*255] = [0, 65025]
so x*y/20 is in range [0, 255*255/20] = [0, 65025/20] = [0, 3251]
and value of uint8(x*y/20) is equal to (x*y/20)%256 meaning exactly LSB byte:
uint8(3251) = uint8(0XCB3) = 0XB3 = 179
3251 = 12*256 + 179
So every time the x*y/20 is bigger than 255 it counts from 0 again: (x*y/20) % 256 this is why your image is repeated circles.
Try this working sample code:
package main
import "fmt"
func main() {
for y := 0; y <= 255; y++ {
for x := 0; x <= 255; x++ {
v := x * y / 20
if int(uint8(v)) != v%256 {
fmt.Println(v, v%256)
}
}
}
fmt.Println("Done.")
}
output:
Done.
Let's simplify you example, see this working sample code:
package main
import (
"bytes"
"image"
"image/png"
"os"
)
func main() {
const dx = 256
const dy = 256
m := image.NewNRGBA(image.Rect(0, 0, dx, dy))
for y := 0; y < dy; y++ {
for x := 0; x < dx; x++ {
v := uint8(x * y / 20)
i := y*m.Stride + x*4
m.Pix[i] = v //R
m.Pix[i+1] = v //G
m.Pix[i+2] = 255 //B
m.Pix[i+3] = 255 //A
}
}
var buf bytes.Buffer
err := png.Encode(&buf, m)
if err != nil {
panic(err)
}
os.Stdout.Write(buf.Bytes())
}
And redirect the output to a file like main > b.png or, go run main.go > b.png
see output file b.png:
uint8(anotherIntValue) conversion will take the last byte of anotherIntValue. That is why your code can produce many blue (0). For example, following code would print 'val = 0'.
dx, dy := 128, 2
fmt.Println("val =", uint8(dx*dy))
Constant conversion will be checked by compiler for out of range errors.

iterating over over a 2D slice in go

I am taking the "Tour of Go", and had a question regarding the Exercise: Slices example. Currently I can create the picture by iterating over each index using the the [] operator, just like you could in C.
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for i := range pic {
pic[i] = make([]uint8, dx)
for j := range pic[i] {
pic[i][j] = uint8(1)
}
}
return pic
}
However, when I try to do something like below, I get an panic: runtime error: index out of range error. I tried adding print statements and calling Pic(3, 3), which printed out a 3x3 array just fine.
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for _, y := range pic {
y = make([]uint8, dx)
for _, x := range y {
x = uint8(1)
_ = x // x has to be used
//fmt.Print("1")
}
//fmt.Print("\n")
}
return pic
}
Any thoughts on what I am doing wrong?
The main problem is your attempt to do assignment. Check my example using your code; https://play.golang.org/p/lwoe79jQ70
What you actually get out of the latter implementation is a 3x0 array, all of the inner arrays are empty. The reason for this is because you're using the range variable for assignment which doesn't work. If the current index is 0, y != pic[0], pic[0] is assigned to y however, y is temporary storage, it typically is the same address and is over written on each iteration. So after the latter example executes, all your x direction arrays are empty, indexing into one causes a panic.
Basically you should just be using your first implementation because it works fine and is the way you would typically do this. But the take away is, when you do a, b := range Something b != Something[a], it is it's on instance, it goes out of scope at the bottom of the loop and assigning to it will not cause a state change to the collection Something, instead you must assign to Something[a] if you want to modify Something[a].
range copies the values from the slice you're iterating over.
See: http://golang.org/ref/spec#RangeClause
To clarify what happens see this simple code example and its output:
package main
import "fmt"
func main() {
s := "hi"
//s[0] = 'H' // cannot assign to s[0]
for _, v := range s {
fmt.Printf("%T, %[1]v, %X\n", v, &v)
v = 'H' // has no effect: this is local var not ref
}
fmt.Println(s)
}
The output is:
int32, 104, C0820042D4
int32, 105, C0820042D4
hi
As you see the address of variable v is not changing (C0820042D4) and v is local variable and range copies value to it, so changing v has no effect.
Here v is rune (int32 alias), A rune is an integer value identifying a Unicode code point, and you cannot assign to s[0] and this won’t compile: s[0] = 'H'
so v = 'H' has no effect on s, it is just local variable.

Idiomatic Type Conversion in Go

I was playing around with Go and was wondering what the best way is to perform idiomatic type conversions in Go. Basically my problem lays within automatic type conversions between uint8, uint64, and float64. From my experience with other languages a multiplication of a uint8 with a uint64 will yield a uint64 value, but not so in go.
Here is an example that I build and I ask if this is the idiomatic way of writing this code or if I'm missing an important language construct.
package main
import ("math";"fmt")
const(Width=64)
func main() {
var index uint32
var bits uint8
index = 100
bits = 3
var c uint64
// This is the line of interest vvvv
c = uint64(math.Ceil(float64(index * uint32(bits))/float64(Width)))
fmt.Println("Test: %v\n", c)
}
From my point of view the calculation of the ceiling value seems unnecessary complex because of all the explicit type conversions.
Thanks!
There are no implicit type conversions for non-constant values.
You can write
var x float64
x = 1
But you cannot write
var x float64
var y int
y = 1
x = y
See the spec for reference.
There's a good reason, to not allow automatic/implicit type conversions, as they can
become very messy and one has to learn many rules to circumvent the various caveats
that may occur. Take the Integer Conversion Rules in C for example.
For example,
package main
import "fmt"
func CeilUint(a, b uint64) uint64 {
return (a + (b - 1)) / b
}
func main() {
const Width = 64
var index uint32 = 100
var bits uint8 = 3
var c uint64 = CeilUint(uint64(index)*uint64(bits), Width)
fmt.Println("Test:", c)
}
Output:
Test: 5
To add to #nemo terrific answer. The convenience of automatic conversion between numeric types in C is outweighed by the confusion it causes. See https://Golang.org/doc/faq#conversions. Thats why you can't even convert from int to int32 implicitly. See https://stackoverflow.com/a/13852456/12817546.
package main
import (
. "fmt"
. "strconv"
)
func main() {
i := 71
c := []interface{}{byte(i), []byte(string(i)), float64(i), i, rune(i), Itoa(i), i != 0}
checkType(c)
}
func checkType(s []interface{}) {
for k, _ := range s {
Printf("%T %v\n", s[k], s[k])
}
}
byte(i) creates a uint8 with a value of 71, []byte(string(i)) a []uint8 with [71], float64(i) float64 71, i int 71, rune(i) int32 71, Itoa(i) string 71 and i != 0 a bool with a value of true.
Since Go won't convert numeric types automatically for you (See https://stackoverflow.com/a/13851553/12817546) you have to convert between types manually. See https://stackoverflow.com/a/41419962/12817546. Note, Itoa(i) sets an "Integer to an ASCII". See comment in https://stackoverflow.com/a/10105983/12817546.

Resources