Home | History | Annotate | Download | only in big
      1 // Copyright 2017 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 big
      6 
      7 import (
      8 	"fmt"
      9 	"math"
     10 	"math/rand"
     11 	"testing"
     12 )
     13 
     14 // TestFloatSqrt64 tests that Float.Sqrt of numbers with 53bit mantissa
     15 // behaves like float math.Sqrt.
     16 func TestFloatSqrt64(t *testing.T) {
     17 	for i := 0; i < 1e5; i++ {
     18 		r := rand.Float64()
     19 
     20 		got := new(Float).SetPrec(53)
     21 		got.Sqrt(NewFloat(r))
     22 		want := NewFloat(math.Sqrt(r))
     23 		if got.Cmp(want) != 0 {
     24 			t.Fatalf("Sqrt(%g) =\n got %g;\nwant %g", r, got, want)
     25 		}
     26 	}
     27 }
     28 
     29 func TestFloatSqrt(t *testing.T) {
     30 	for _, test := range []struct {
     31 		x    string
     32 		want string
     33 	}{
     34 		// Test values were generated on Wolfram Alpha using query
     35 		//   'sqrt(N) to 350 digits'
     36 		// 350 decimal digits give up to 1000 binary digits.
     37 		{"0.03125", "0.17677669529663688110021109052621225982120898442211850914708496724884155980776337985629844179095519659187673077886403712811560450698134215158051518713749197892665283324093819909447499381264409775757143376369499645074628431682460775184106467733011114982619404115381053858929018135497032545349940642599871090667456829147610370507757690729404938184321879"},
     38 		{"0.125", "0.35355339059327376220042218105242451964241796884423701829416993449768311961552675971259688358191039318375346155772807425623120901396268430316103037427498395785330566648187639818894998762528819551514286752738999290149256863364921550368212935466022229965238808230762107717858036270994065090699881285199742181334913658295220741015515381458809876368643757"},
     39 		{"0.5", "0.70710678118654752440084436210484903928483593768847403658833986899536623923105351942519376716382078636750692311545614851246241802792536860632206074854996791570661133296375279637789997525057639103028573505477998580298513726729843100736425870932044459930477616461524215435716072541988130181399762570399484362669827316590441482031030762917619752737287514"},
     40 		{"2.0", "1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457503"},
     41 		{"3.0", "1.7320508075688772935274463415058723669428052538103806280558069794519330169088000370811461867572485756756261414154067030299699450949989524788116555120943736485280932319023055820679748201010846749232650153123432669033228866506722546689218379712270471316603678615880190499865373798593894676503475065760507566183481296061009476021871903250831458295239598"},
     42 		{"4.0", "2.0"},
     43 
     44 		{"1p512", "1p256"},
     45 		{"4p1024", "2p512"},
     46 		{"9p2048", "3p1024"},
     47 
     48 		{"1p-1024", "1p-512"},
     49 		{"4p-2048", "2p-1024"},
     50 		{"9p-4096", "3p-2048"},
     51 	} {
     52 		for _, prec := range []uint{24, 53, 64, 65, 100, 128, 129, 200, 256, 400, 600, 800, 1000} {
     53 			x := new(Float).SetPrec(prec)
     54 			x.Parse(test.x, 10)
     55 
     56 			got := new(Float).SetPrec(prec).Sqrt(x)
     57 			want := new(Float).SetPrec(prec)
     58 			want.Parse(test.want, 10)
     59 			if got.Cmp(want) != 0 {
     60 				t.Errorf("prec = %d, Sqrt(%v) =\ngot  %g;\nwant %g",
     61 					prec, test.x, got, want)
     62 			}
     63 
     64 			// Square test.
     65 			// If got holds the square root of x to precision p, then
     66 			//   got = x + k
     67 			// for some k such that |k| < 2**(-p). Thus,
     68 			//   got = (x + k) = x + 2kn + k
     69 			// and the error must satisfy
     70 			//   err = |got - x|  | 2kn | < 2**(-p+1)*n
     71 			// Ignoring the k term for simplicity.
     72 
     73 			// err = |got - x|
     74 			// (but do intermediate steps with 32 guard digits to
     75 			// avoid introducing spurious rounding-related errors)
     76 			sq := new(Float).SetPrec(prec+32).Mul(got, got)
     77 			diff := new(Float).Sub(sq, x)
     78 			err := diff.Abs(diff).SetPrec(prec)
     79 
     80 			// maxErr = 2**(-p+1)*x
     81 			one := new(Float).SetPrec(prec).SetInt64(1)
     82 			maxErr := new(Float).Mul(new(Float).SetMantExp(one, -int(prec)+1), got)
     83 
     84 			if err.Cmp(maxErr) >= 0 {
     85 				t.Errorf("prec = %d, Sqrt(%v) =\ngot err  %g;\nwant maxErr %g",
     86 					prec, test.x, err, maxErr)
     87 			}
     88 		}
     89 	}
     90 }
     91 
     92 func TestFloatSqrtSpecial(t *testing.T) {
     93 	for _, test := range []struct {
     94 		x    *Float
     95 		want *Float
     96 	}{
     97 		{NewFloat(+0), NewFloat(+0)},
     98 		{NewFloat(-0), NewFloat(-0)},
     99 		{NewFloat(math.Inf(+1)), NewFloat(math.Inf(+1))},
    100 	} {
    101 		got := new(Float).Sqrt(test.x)
    102 		if got.neg != test.want.neg || got.form != test.want.form {
    103 			t.Errorf("Sqrt(%v) = %v (neg: %v); want %v (neg: %v)",
    104 				test.x, got, got.neg, test.want, test.want.neg)
    105 		}
    106 	}
    107 
    108 }
    109 
    110 // Benchmarks
    111 
    112 func BenchmarkFloatSqrt(b *testing.B) {
    113 	for _, prec := range []uint{64, 128, 256, 1e3, 1e4, 1e5, 1e6} {
    114 		x := NewFloat(2)
    115 		z := new(Float).SetPrec(prec)
    116 		b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
    117 			b.ReportAllocs()
    118 			for n := 0; n < b.N; n++ {
    119 				z.Sqrt(x)
    120 			}
    121 		})
    122 	}
    123 }
    124