Source file src/internal/strconv/fp_test.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package strconv_test
     6  
     7  import (
     8  	"bufio"
     9  	_ "embed"
    10  	"fmt"
    11  	"internal/strconv"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func pow2(i int) float64 {
    17  	switch {
    18  	case i < 0:
    19  		return 1 / pow2(-i)
    20  	case i == 0:
    21  		return 1
    22  	case i == 1:
    23  		return 2
    24  	}
    25  	return pow2(i/2) * pow2(i-i/2)
    26  }
    27  
    28  // Wrapper around strconv.ParseFloat(x, 64).  Handles dddddp+ddd (binary exponent)
    29  // itself, passes the rest on to strconv.ParseFloat.
    30  func myatof64(s string) (f float64, ok bool) {
    31  	if mant, exp, ok := strings.Cut(s, "p"); ok {
    32  		n, err := strconv.ParseInt(mant, 10, 64)
    33  		if err != nil {
    34  			return 0, false
    35  		}
    36  		e, err1 := strconv.Atoi(exp)
    37  		if err1 != nil {
    38  			println("bad e", exp)
    39  			return 0, false
    40  		}
    41  		v := float64(n)
    42  		// We expect that v*pow2(e) fits in a float64,
    43  		// but pow2(e) by itself may not. Be careful.
    44  		if e <= -1000 {
    45  			v *= pow2(-1000)
    46  			e += 1000
    47  			for e < 0 {
    48  				v /= 2
    49  				e++
    50  			}
    51  			return v, true
    52  		}
    53  		if e >= 1000 {
    54  			v *= pow2(1000)
    55  			e -= 1000
    56  			for e > 0 {
    57  				v *= 2
    58  				e--
    59  			}
    60  			return v, true
    61  		}
    62  		return v * pow2(e), true
    63  	}
    64  	f1, err := strconv.ParseFloat(s, 64)
    65  	if err != nil {
    66  		return 0, false
    67  	}
    68  	return f1, true
    69  }
    70  
    71  // Wrapper around strconv.ParseFloat(x, 32).  Handles dddddp+ddd (binary exponent)
    72  // itself, passes the rest on to strconv.ParseFloat.
    73  func myatof32(s string) (f float32, ok bool) {
    74  	if mant, exp, ok := strings.Cut(s, "p"); ok {
    75  		n, err := strconv.Atoi(mant)
    76  		if err != nil {
    77  			println("bad n", mant)
    78  			return 0, false
    79  		}
    80  		e, err1 := strconv.Atoi(exp)
    81  		if err1 != nil {
    82  			println("bad p", exp)
    83  			return 0, false
    84  		}
    85  		return float32(float64(n) * pow2(e)), true
    86  	}
    87  	f64, err1 := strconv.ParseFloat(s, 32)
    88  	f1 := float32(f64)
    89  	if err1 != nil {
    90  		return 0, false
    91  	}
    92  	return f1, true
    93  }
    94  
    95  //go:embed testdata/testfp.txt
    96  var testfp string
    97  
    98  func TestFp(t *testing.T) {
    99  	s := bufio.NewScanner(strings.NewReader(testfp))
   100  	for lineno := 1; s.Scan(); lineno++ {
   101  		line := s.Text()
   102  		line, _, _ = strings.Cut(line, "#")
   103  		line = strings.TrimSpace(line)
   104  		if line == "" {
   105  			continue
   106  		}
   107  		a := strings.Split(line, " ")
   108  		if len(a) != 4 {
   109  			t.Errorf("testdata/testfp.txt:%d: wrong field count", lineno)
   110  			continue
   111  		}
   112  		var s string
   113  		var v float64
   114  		switch a[0] {
   115  		case "float64":
   116  			var ok bool
   117  			v, ok = myatof64(a[2])
   118  			if !ok {
   119  				t.Errorf("testdata/testfp.txt:%d: cannot atof64 %s", lineno, a[2])
   120  				continue
   121  			}
   122  			s = fmt.Sprintf(a[1], v)
   123  		case "float32":
   124  			v1, ok := myatof32(a[2])
   125  			if !ok {
   126  				t.Errorf("testdata/testfp.txt:%d: cannot atof32 %s", lineno, a[2])
   127  				continue
   128  			}
   129  			s = fmt.Sprintf(a[1], v1)
   130  			v = float64(v1)
   131  		}
   132  		if s != a[3] {
   133  			t.Errorf("testdata/testfp.txt:%d: %s %s %s %s: have %s want %s", lineno, a[0], a[1], a[2], a[3], s, a[3])
   134  		}
   135  	}
   136  	if s.Err() != nil {
   137  		t.Fatal("testfp: read testdata/testfp.txt: ", s.Err())
   138  	}
   139  }
   140  

View as plain text