48

In Go, what is the best strategy for converting int64 to int? I am having difficulty comparing the two

package main 

import (
    "math"
    "strings"
    "strconv"
)

type largestPrimeFactor struct {
    N      int
    Result int
}

func main() {
    base := largestPrimeFactor{N:13195}
    max := math.Sqrt(float64(base.N))

    maxStr := strconv.FormatFloat(max, 'E', 'G', 64)
    maxShift := strings.Split(maxStr, ".")[0]
    maxInt, err := strconv.ParseInt(maxShift, 10, 64)

    if (err != nil) {
        panic(err)
    }

on this next line

    for a := 2; a < maxInt; a++ {
        if isPrime(a) {
            if base.N % a == 0 {
                base.Result = a
            }
        }
    }

    println(base)
}

func isPrime(n int) bool {
    flag := false

    max := math.Sqrt(float64(n))

    maxStr := strconv.FormatFloat(max, 'E', 'G', 64)
    maxShift := strings.Split(maxStr, ".")[0]
    maxInt, err := strconv.ParseInt(maxShift, 10, 64)

    if (err != nil) {
        panic(err)
    }

    for a := 2; a < maxInt; a++ {
        if (n % a == 0) {
            flag := true
        }
    }
    return flag
}
pward
  • 1,049
  • 1
  • 8
  • 17

3 Answers3

74

You convert them with a type "conversion"

var a int
var b int64
int64(a) < b

When comparing values, you always want to convert the smaller type to the larger. Converting the other way will possibly truncate the value:

var x int32 = 0
var y int64 = math.MaxInt32 + 1 // y == 2147483648
if x < int32(y) {
// this evaluates to false, because int32(y) is -2147483648

Or in your case to convert the maxInt int64 value to an int, you could use

for a := 2; a < int(maxInt); a++ {

which would fail to execute correctly if maxInt overflows the max value of the int type on your system.

JimB
  • 104,193
  • 13
  • 262
  • 255
  • @pward here's a starting point for number types in go that might help https://golang.org/ref/spec#Numeric_types – Corbin Aug 03 '16 at 22:32
  • 11
    It's important to note that, though this answer is checked, it does not technically solve the problem posed by the question, which is how to convert int64 to int. – pward Aug 04 '16 at 18:28
  • 2
    @pward: It also shows you _why_ you should care about truncation with an example. You should understand how the integer types work, just like you should know why in javascript `0.1 * 0.2 == 0.020000000000000004`. You have an [X Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here -- what you want is for the code to work, but the solution you're asking for is the incorrect solution. – JimB Aug 04 '16 at 19:55
  • Why is it correct to assume that `int64` is bigger than `int`? – ceving Aug 20 '23 at 09:29
  • @ceving, it's not, it's correct to assume that `int64` is larger or _equal_ in _size_ to `int`, therefor converting from `int` to `int64` will never lose data, which is not true of the inverse case. – JimB Aug 21 '23 at 14:40
  • @JimB `int` is CPU specific. It is not correct to assume that a 128 bit CPU will never exist in the future. – ceving Aug 21 '23 at 18:15
  • @ceving: we are not talking about theoretical future CPU architectures, we are talking about the [Go language specification](https://go.dev/ref/spec#Numeric_types), which states that `int` and `uint` are either 32 or 64 bits. – JimB Aug 21 '23 at 18:25
25

I came here because of the title, "How to convert an int64 to int in Go?". The answer is,

int(int64Var)
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
  • 1
    Had a doubt but verified this is a lossless conversion https://go.dev/play/p/DjuxXVsysKy – avp Aug 30 '23 at 04:35
-2

It is correct to use the strconv package

strconv.FormatInt(int64Var, 10)
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 27 '22 at 10:47