v icon indicating copy to clipboard operation
v copied to clipboard

wrong math results while looping

Open MY-RV opened this issue 3 years ago • 3 comments
trafficstars

V version: 0.3.1 1c63ce4 OS: linux, Zorin OS 16.1 Processor: 8 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz

What did you do? I was curious about vlang (structure, functionality, performance) so I did some tests. while testing performance with big operations I got strange numbers so I decided to test it in a simple loop.

What did you expect to see? I got smaller or negative numbers instead the expected results

What did you see instead? The results are the attached. image

PD

Great project 💯

MY-RV avatar Sep 11 '22 19:09 MY-RV

Where is the wrong?

(1) literal quantity has a default type (2) the calculation result may overflow

shove70 avatar Sep 12 '22 03:09 shove70

Go 'int's are 8 bytes in size. Need to use i64 for V https://stackoverflow.com/questions/51852678/int-vs-int32-return-value

import (
	"fmt"
	"runtime"
	"unsafe"
)

func main() {
	fmt.Println("arch", runtime.GOARCH)
	fmt.Println("int", unsafe.Sizeof(int(0)))
	fmt.Println("int32", unsafe.Sizeof(int32(0)))
}
arch amd64
int 8
int32 4

DevrosTheOne avatar Sep 12 '22 06:09 DevrosTheOne

I think this was a fairly recent change. Before, int was always 32 bits (4 bytes) in Go. Now, it is 32 bits (4 bytes) on 32-bit machines, and 64 bits (8 bytes) on 64-bit machines.

This is why V (mostly) makes you specify the exact size thing you want... to avoid this type of confusion.

JalonSolov avatar Sep 12 '22 13:09 JalonSolov