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

View as plain text