max{max=v}elseifv"/> max{max=v}elseifv">

Home  >  Article  >  Backend Development  >  Invalid operation: v > max (type parameters T and > are not comparable)

Invalid operation: v > max (type parameters T and > are not comparable)

WBOY
WBOYforward
2024-02-14 19:54:09597browse

无效操作:v > max (type parameters T and > are not comparable)

php editor Apple is here to analyze a common error message for you: "Invalid operation: v > max (type parameter T is not comparable with >)". When programming in the PHP language, we may encounter this error, especially when comparing type parameters. This article will explain in detail the cause of this error and how to handle it correctly to help readers better Understand and solve this problem. The invalid operation error occurs when comparing two incomparable types. The key to solving this problem is to ensure that the compared types are comparable. In the following, we will introduce the specific solution step by step.

Question content

package main

import (
    "fmt"
)

func findMinMax[T comparable](arr []T) (min, max T) {
    for _, v := range arr {
        if v > max {
            max = v
        } else if v < min {
            min = v
        }
    }
    return min, max
}

func main() {
    arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println(findMinMax(arr))
}

I'd be more than happy to help you solve a problem you're having with the findminmax function. An error message showing v > max or v 2ed7c9d8acb688eaf79eef6a9f8d6e7a operator.

Ordered constraints are defined in golang.org/x/exp/ in the constraints package, see constraints.ordered.

Compile using your code:

import (
    "fmt"

    "golang.org/x/exp/constraints"
)

func findminmax[t constraints.ordered](arr []t) (min, max t) {
    for _, v := range arr {
        if v > max {
            max = v
        } else if v < min {
            min = v
        }
    }
    return min, max
}

Try it on go playground.

It gives wrong results because you start with zero value min and max and if all the values ​​in the passed slice are greater or less than zero value then min or max will remain zero.

A simple fix is ​​to initialize min and max with the first value if the passed slice is not empty:

func findminmax[t constraints.ordered](arr []t) (min, max t) {
    if len(arr) > 0 {
        min, max = arr[0], arr[0]
    }

    for _, v := range arr {
        if v > max {
            max = v
        } else if v < min {
            min = v
        }
    }
    return min, max
}

This will output (try it on go playground):

1 9

Note that if you use floating-point types, you must explicitly handle nan values ​​because their order with other floating-point numbers is not specified.

The above is the detailed content of Invalid operation: v > max (type parameters T and > are not comparable). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete