Source file src/math/huge_test.go

     1  // Copyright 2018 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  // Disabled for s390x because it uses assembly routines that are not
     6  // accurate for huge arguments.
     7  
     8  //go:build !s390x
     9  
    10  package math_test
    11  
    12  import (
    13  	. "math"
    14  	"testing"
    15  )
    16  
    17  // Inputs to test trig_reduce
    18  var trigHuge = []float64{
    19  	1 << 28,
    20  	1 << 29,
    21  	1 << 30,
    22  	1 << 35,
    23  	1 << 120,
    24  	1 << 240,
    25  	1 << 480,
    26  	1234567891234567 << 180,
    27  	1234567891234567 << 300,
    28  	MaxFloat64,
    29  }
    30  
    31  // Results for trigHuge[i] calculated with https://github.com/robpike/ivy
    32  // using 4096 bits of working precision.   Values requiring less than
    33  // 102 decimal digits (1 << 120, 1 << 240, 1 << 480, 1234567891234567 << 180)
    34  // were confirmed via https://keisan.casio.com/
    35  var cosHuge = []float64{
    36  	-0.16556897949057876,
    37  	-0.94517382606089662,
    38  	0.78670712294118812,
    39  	-0.76466301249635305,
    40  	-0.92587902285483787,
    41  	0.93601042593353793,
    42  	-0.28282777640193788,
    43  	-0.14616431394103619,
    44  	-0.79456058210671406,
    45  	-0.99998768942655994,
    46  }
    47  
    48  var sinHuge = []float64{
    49  	-0.98619821183697566,
    50  	0.32656766301856334,
    51  	-0.61732641504604217,
    52  	-0.64443035102329113,
    53  	0.37782010936075202,
    54  	-0.35197227524865778,
    55  	0.95917070894368716,
    56  	0.98926032637023618,
    57  	-0.60718488235646949,
    58  	0.00496195478918406,
    59  }
    60  
    61  var tanHuge = []float64{
    62  	5.95641897939639421,
    63  	-0.34551069233430392,
    64  	-0.78469661331920043,
    65  	0.84276385870875983,
    66  	-0.40806638884180424,
    67  	-0.37603456702698076,
    68  	-3.39135965054779932,
    69  	-6.76813854009065030,
    70  	0.76417695016604922,
    71  	-0.00496201587444489,
    72  }
    73  
    74  // Check that trig values of huge angles return accurate results.
    75  // This confirms that argument reduction works for very large values
    76  // up to MaxFloat64.
    77  func TestHugeCos(t *testing.T) {
    78  	for i := 0; i < len(trigHuge); i++ {
    79  		f1 := cosHuge[i]
    80  		f2 := Cos(trigHuge[i])
    81  		if !close(f1, f2) {
    82  			t.Errorf("Cos(%g) = %g, want %g", trigHuge[i], f2, f1)
    83  		}
    84  		f3 := Cos(-trigHuge[i])
    85  		if !close(f1, f3) {
    86  			t.Errorf("Cos(%g) = %g, want %g", -trigHuge[i], f3, f1)
    87  		}
    88  	}
    89  }
    90  
    91  func TestHugeSin(t *testing.T) {
    92  	for i := 0; i < len(trigHuge); i++ {
    93  		f1 := sinHuge[i]
    94  		f2 := Sin(trigHuge[i])
    95  		if !close(f1, f2) {
    96  			t.Errorf("Sin(%g) = %g, want %g", trigHuge[i], f2, f1)
    97  		}
    98  		f3 := Sin(-trigHuge[i])
    99  		if !close(-f1, f3) {
   100  			t.Errorf("Sin(%g) = %g, want %g", -trigHuge[i], f3, -f1)
   101  		}
   102  	}
   103  }
   104  
   105  func TestHugeSinCos(t *testing.T) {
   106  	for i := 0; i < len(trigHuge); i++ {
   107  		f1, g1 := sinHuge[i], cosHuge[i]
   108  		f2, g2 := Sincos(trigHuge[i])
   109  		if !close(f1, f2) || !close(g1, g2) {
   110  			t.Errorf("Sincos(%g) = %g, %g, want %g, %g", trigHuge[i], f2, g2, f1, g1)
   111  		}
   112  		f3, g3 := Sincos(-trigHuge[i])
   113  		if !close(-f1, f3) || !close(g1, g3) {
   114  			t.Errorf("Sincos(%g) = %g, %g, want %g, %g", -trigHuge[i], f3, g3, -f1, g1)
   115  		}
   116  	}
   117  }
   118  
   119  func TestHugeTan(t *testing.T) {
   120  	for i := 0; i < len(trigHuge); i++ {
   121  		f1 := tanHuge[i]
   122  		f2 := Tan(trigHuge[i])
   123  		if !close(f1, f2) {
   124  			t.Errorf("Tan(%g) = %g, want %g", trigHuge[i], f2, f1)
   125  		}
   126  		f3 := Tan(-trigHuge[i])
   127  		if !close(-f1, f3) {
   128  			t.Errorf("Tan(%g) = %g, want %g", -trigHuge[i], f3, -f1)
   129  		}
   130  	}
   131  }
   132  

View as plain text