modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/float_lit3.go (about)

     1  // errorcheck
     2  
     3  // Check flagging of invalid conversion of constant to float32/float64 near min/max boundaries.
     4  
     5  // Copyright 2014 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  
     9  package main
    10  
    11  // See float_lit2.go for motivation for these values.
    12  const (
    13  	two24   = 1.0 * (1 << 24)
    14  	two53   = 1.0 * (1 << 53)
    15  	two64   = 1.0 * (1 << 64)
    16  	two128  = two64 * two64
    17  	two256  = two128 * two128
    18  	two512  = two256 * two256
    19  	two768  = two512 * two256
    20  	two1024 = two512 * two512
    21  
    22  	ulp32 = two128 / two24
    23  	max32 = two128 - ulp32
    24  
    25  	ulp64 = two1024 / two53
    26  	max64 = two1024 - ulp64
    27  )
    28  
    29  var x = []interface{}{
    30  	float32(max32 + ulp32/2 - 1),             // ok
    31  	float32(max32 + ulp32/2 - two128/two256), // ok
    32  	float32(max32 + ulp32/2),                 // ERROR "constant 3\.40282e\+38 overflows float32"
    33  
    34  	float32(-max32 - ulp32/2 + 1),             // ok
    35  	float32(-max32 - ulp32/2 + two128/two256), // ok
    36  	float32(-max32 - ulp32/2),                 // ERROR "constant -3\.40282e\+38 overflows float32"
    37  
    38  	// If the compiler's internal floating point representation
    39  	// is shorter than 1024 bits, it cannot distinguish max64+ulp64/2-1 and max64+ulp64/2.
    40  	// gc uses fewer than 1024 bits, so allow it to print the overflow error for the -1 case.
    41  	float64(max64 + ulp64/2 - two1024/two256), // ok
    42  	float64(max64 + ulp64/2 - 1),              // GC_ERROR "constant 1\.79769e\+308 overflows float64"
    43  	float64(max64 + ulp64/2),                  // ERROR "constant 1\.79769e\+308 overflows float64"
    44  
    45  	float64(-max64 - ulp64/2 + two1024/two256), // ok
    46  	float64(-max64 - ulp64/2 + 1),              // GC_ERROR "constant -1\.79769e\+308 overflows float64"
    47  	float64(-max64 - ulp64/2),                  // ERROR "constant -1\.79769e\+308 overflows float64"
    48  }