Source file test/typeparam/absdiff.go

     1  // run
     2  
     3  // Copyright 2020 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  type Numeric interface {
    10  	~int | ~int8 | ~int16 | ~int32 | ~int64 |
    11  		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
    12  		~float32 | ~float64 |
    13  		~complex64 | ~complex128
    14  }
    15  
    16  // numericAbs matches numeric types with an Abs method.
    17  type numericAbs[T any] interface {
    18  	Numeric
    19  	Abs() T
    20  }
    21  
    22  // AbsDifference computes the absolute value of the difference of
    23  // a and b, where the absolute value is determined by the Abs method.
    24  func absDifference[T numericAbs[T]](a, b T) T {
    25  	d := a - b
    26  	return d.Abs()
    27  }
    28  
    29  // orderedNumeric matches numeric types that support the < operator.
    30  type orderedNumeric interface {
    31  	~int | ~int8 | ~int16 | ~int32 | ~int64 |
    32  		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
    33  		~float32 | ~float64
    34  }
    35  
    36  // Complex matches the two complex types, which do not have a < operator.
    37  type Complex interface {
    38  	~complex64 | ~complex128
    39  }
    40  
    41  // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
    42  // // orderedAbs is a helper type that defines an Abs method for
    43  // // ordered numeric types.
    44  // type orderedAbs[T orderedNumeric] T
    45  //
    46  // func (a orderedAbs[T]) Abs() orderedAbs[T] {
    47  // 	if a < 0 {
    48  // 		return -a
    49  // 	}
    50  // 	return a
    51  // }
    52  //
    53  // // complexAbs is a helper type that defines an Abs method for
    54  // // complex types.
    55  // type complexAbs[T Complex] T
    56  //
    57  // func (a complexAbs[T]) Abs() complexAbs[T] {
    58  // 	r := float64(real(a))
    59  // 	i := float64(imag(a))
    60  // 	d := math.Sqrt(r*r + i*i)
    61  // 	return complexAbs[T](complex(d, 0))
    62  // }
    63  //
    64  // // OrderedAbsDifference returns the absolute value of the difference
    65  // // between a and b, where a and b are of an ordered type.
    66  // func orderedAbsDifference[T orderedNumeric](a, b T) T {
    67  // 	return T(absDifference(orderedAbs[T](a), orderedAbs[T](b)))
    68  // }
    69  //
    70  // // ComplexAbsDifference returns the absolute value of the difference
    71  // // between a and b, where a and b are of a complex type.
    72  // func complexAbsDifference[T Complex](a, b T) T {
    73  // 	return T(absDifference(complexAbs[T](a), complexAbs[T](b)))
    74  // }
    75  
    76  func main() {
    77  	// // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
    78  	// if got, want := orderedAbsDifference(1.0, -2.0), 3.0; got != want {
    79  	// 	panic(fmt.Sprintf("got = %v, want = %v", got, want))
    80  	// }
    81  	// if got, want := orderedAbsDifference(-1.0, 2.0), 3.0; got != want {
    82  	// 	panic(fmt.Sprintf("got = %v, want = %v", got, want))
    83  	// }
    84  	// if got, want := orderedAbsDifference(-20, 15), 35; got != want {
    85  	// 	panic(fmt.Sprintf("got = %v, want = %v", got, want))
    86  	// }
    87  	//
    88  	// if got, want := complexAbsDifference(5.0+2.0i, 2.0-2.0i), 5+0i; got != want {
    89  	// 	panic(fmt.Sprintf("got = %v, want = %v", got, want))
    90  	// }
    91  	// if got, want := complexAbsDifference(2.0-2.0i, 5.0+2.0i), 5+0i; got != want {
    92  	// 	panic(fmt.Sprintf("got = %v, want = %v", got, want))
    93  	// }
    94  }
    95  

View as plain text