<p><img src="https://img.php.cn/upload/article/000/887/227/170791165242514.jpg" alt="无效操作:v > max (type parameters T and > are not comparable)"></p>
<p>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. </p>
<h2 class="daan">Question content</h2>
<p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">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))
}</pre><div class="contentsignin">Copy after login</div></div>
<p>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 < min indicates that there may be a problem with the comparison operation within the function. In order to provide a precise solution, I need to look at the implementation of the findminmax function. From your description, the type t which is supposed to be comparable seems to be causing problems during comparison. </p>
<p>I expect the function findminmax to work properly. </p><h2 class="daan">Solution</h2><p>You used a <code></code>comarable<a href="https://www.php.cn/link/422e42d058a41c75062760d7d640debf" rel="nofollow noreferrer"><code> constraint on a </code>t</a> type parameter. <code>comparable</code> means: <em>comparable</em>. Therefore, you can use the <code>==</code> operator on values of this type. This does not mean that they are <em>ordered</em>, which is required to use the <code><</code> <code>></code> operator. </p>
<p>Ordered constraints are defined in <a href="https://www.php.cn/link/b753ced14094e73576b017d9323be362" rel="nofollow noreferrer"><code>golang.org/x/exp/ in the constraints</code></a> package, see <a href="https://www.php.cn/link/b753ced14094e73576b017d9323be362#ordered" rel="nofollow noreferrer"><code>constraints.ordered</code></a>. </p>
<p>Compile using your code: </p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">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
}</pre><div class="contentsignin">Copy after login</div></div>
<p>Try it on <a href="https://www.php.cn/link/b18336c06954cf0a92113517ca2bdd29" rel="nofollow noreferrer">go playground</a>. </p>
<p> It gives wrong results because you start with zero value <code>min</code> and <code>max</code> and if all the values in the passed slice are greater or less than zero value then <code>min</code> or <code>max</code> will remain zero. </p>
<p>A simple fix is to initialize <code>min</code> and <code>max</code> with the first value if the passed slice is not empty: </p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">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
}</pre><div class="contentsignin">Copy after login</div></div>
<p>This will output (try it on <a href="https://www.php.cn/link/ed7ec77be8029e30e8532d41448d1c52" rel="nofollow noreferrer">go playground</a>): </p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">1 9</pre><div class="contentsignin">Copy after login</div></div>
</p>
<p>Note that if you use floating-point types, you must explicitly handle <code>nan</code> values because their order with other floating-point numbers is not specified. </p>
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!