github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/internal/gc/big/rat_test.go (about) 1 // Do not edit. Bootstrap copy of /Users/rsc/g/go/src/cmd/internal/gc/big/rat_test.go 2 3 // Copyright 2010 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package big 8 9 import ( 10 "bytes" 11 "encoding/gob" 12 "encoding/json" 13 "encoding/xml" 14 "math" 15 "testing" 16 ) 17 18 func TestZeroRat(t *testing.T) { 19 var x, y, z Rat 20 y.SetFrac64(0, 42) 21 22 if x.Cmp(&y) != 0 { 23 t.Errorf("x and y should be both equal and zero") 24 } 25 26 if s := x.String(); s != "0/1" { 27 t.Errorf("got x = %s, want 0/1", s) 28 } 29 30 if s := x.RatString(); s != "0" { 31 t.Errorf("got x = %s, want 0", s) 32 } 33 34 z.Add(&x, &y) 35 if s := z.RatString(); s != "0" { 36 t.Errorf("got x+y = %s, want 0", s) 37 } 38 39 z.Sub(&x, &y) 40 if s := z.RatString(); s != "0" { 41 t.Errorf("got x-y = %s, want 0", s) 42 } 43 44 z.Mul(&x, &y) 45 if s := z.RatString(); s != "0" { 46 t.Errorf("got x*y = %s, want 0", s) 47 } 48 49 // check for division by zero 50 defer func() { 51 if s := recover(); s == nil || s.(string) != "division by zero" { 52 panic(s) 53 } 54 }() 55 z.Quo(&x, &y) 56 } 57 58 func TestRatSign(t *testing.T) { 59 zero := NewRat(0, 1) 60 for _, a := range setStringTests { 61 x, ok := new(Rat).SetString(a.in) 62 if !ok { 63 continue 64 } 65 s := x.Sign() 66 e := x.Cmp(zero) 67 if s != e { 68 t.Errorf("got %d; want %d for z = %v", s, e, &x) 69 } 70 } 71 } 72 73 var ratCmpTests = []struct { 74 rat1, rat2 string 75 out int 76 }{ 77 {"0", "0/1", 0}, 78 {"1/1", "1", 0}, 79 {"-1", "-2/2", 0}, 80 {"1", "0", 1}, 81 {"0/1", "1/1", -1}, 82 {"-5/1434770811533343057144", "-5/1434770811533343057145", -1}, 83 {"49832350382626108453/8964749413", "49832350382626108454/8964749413", -1}, 84 {"-37414950961700930/7204075375675961", "37414950961700930/7204075375675961", -1}, 85 {"37414950961700930/7204075375675961", "74829901923401860/14408150751351922", 0}, 86 } 87 88 func TestRatCmp(t *testing.T) { 89 for i, test := range ratCmpTests { 90 x, _ := new(Rat).SetString(test.rat1) 91 y, _ := new(Rat).SetString(test.rat2) 92 93 out := x.Cmp(y) 94 if out != test.out { 95 t.Errorf("#%d got out = %v; want %v", i, out, test.out) 96 } 97 } 98 } 99 100 func TestIsInt(t *testing.T) { 101 one := NewInt(1) 102 for _, a := range setStringTests { 103 x, ok := new(Rat).SetString(a.in) 104 if !ok { 105 continue 106 } 107 i := x.IsInt() 108 e := x.Denom().Cmp(one) == 0 109 if i != e { 110 t.Errorf("got IsInt(%v) == %v; want %v", x, i, e) 111 } 112 } 113 } 114 115 func TestRatAbs(t *testing.T) { 116 zero := new(Rat) 117 for _, a := range setStringTests { 118 x, ok := new(Rat).SetString(a.in) 119 if !ok { 120 continue 121 } 122 e := new(Rat).Set(x) 123 if e.Cmp(zero) < 0 { 124 e.Sub(zero, e) 125 } 126 z := new(Rat).Abs(x) 127 if z.Cmp(e) != 0 { 128 t.Errorf("got Abs(%v) = %v; want %v", x, z, e) 129 } 130 } 131 } 132 133 func TestRatNeg(t *testing.T) { 134 zero := new(Rat) 135 for _, a := range setStringTests { 136 x, ok := new(Rat).SetString(a.in) 137 if !ok { 138 continue 139 } 140 e := new(Rat).Sub(zero, x) 141 z := new(Rat).Neg(x) 142 if z.Cmp(e) != 0 { 143 t.Errorf("got Neg(%v) = %v; want %v", x, z, e) 144 } 145 } 146 } 147 148 func TestRatInv(t *testing.T) { 149 zero := new(Rat) 150 for _, a := range setStringTests { 151 x, ok := new(Rat).SetString(a.in) 152 if !ok { 153 continue 154 } 155 if x.Cmp(zero) == 0 { 156 continue // avoid division by zero 157 } 158 e := new(Rat).SetFrac(x.Denom(), x.Num()) 159 z := new(Rat).Inv(x) 160 if z.Cmp(e) != 0 { 161 t.Errorf("got Inv(%v) = %v; want %v", x, z, e) 162 } 163 } 164 } 165 166 type ratBinFun func(z, x, y *Rat) *Rat 167 type ratBinArg struct { 168 x, y, z string 169 } 170 171 func testRatBin(t *testing.T, i int, name string, f ratBinFun, a ratBinArg) { 172 x, _ := new(Rat).SetString(a.x) 173 y, _ := new(Rat).SetString(a.y) 174 z, _ := new(Rat).SetString(a.z) 175 out := f(new(Rat), x, y) 176 177 if out.Cmp(z) != 0 { 178 t.Errorf("%s #%d got %s want %s", name, i, out, z) 179 } 180 } 181 182 var ratBinTests = []struct { 183 x, y string 184 sum, prod string 185 }{ 186 {"0", "0", "0", "0"}, 187 {"0", "1", "1", "0"}, 188 {"-1", "0", "-1", "0"}, 189 {"-1", "1", "0", "-1"}, 190 {"1", "1", "2", "1"}, 191 {"1/2", "1/2", "1", "1/4"}, 192 {"1/4", "1/3", "7/12", "1/12"}, 193 {"2/5", "-14/3", "-64/15", "-28/15"}, 194 {"4707/49292519774798173060", "-3367/70976135186689855734", "84058377121001851123459/1749296273614329067191168098769082663020", "-1760941/388732505247628681598037355282018369560"}, 195 {"-61204110018146728334/3", "-31052192278051565633/2", "-215564796870448153567/6", "950260896245257153059642991192710872711/3"}, 196 {"-854857841473707320655/4237645934602118692642972629634714039", "-18/31750379913563777419", "-27/133467566250814981", "15387441146526731771790/134546868362786310073779084329032722548987800600710485341"}, 197 {"618575745270541348005638912139/19198433543745179392300736", "-19948846211000086/637313996471", "27674141753240653/30123979153216", "-6169936206128396568797607742807090270137721977/6117715203873571641674006593837351328"}, 198 {"-3/26206484091896184128", "5/2848423294177090248", "15310893822118706237/9330894968229805033368778458685147968", "-5/24882386581946146755650075889827061248"}, 199 {"26946729/330400702820", "41563965/225583428284", "1238218672302860271/4658307703098666660055", "224002580204097/14906584649915733312176"}, 200 {"-8259900599013409474/7", "-84829337473700364773/56707961321161574960", "-468402123685491748914621885145127724451/396955729248131024720", "350340947706464153265156004876107029701/198477864624065512360"}, 201 {"575775209696864/1320203974639986246357", "29/712593081308", "410331716733912717985762465/940768218243776489278275419794956", "808/45524274987585732633"}, 202 {"1786597389946320496771/2066653520653241", "6269770/1992362624741777", "3559549865190272133656109052308126637/4117523232840525481453983149257", "8967230/3296219033"}, 203 {"-36459180403360509753/32150500941194292113930", "9381566963714/9633539", "301622077145533298008420642898530153/309723104686531919656937098270", "-3784609207827/3426986245"}, 204 } 205 206 func TestRatBin(t *testing.T) { 207 for i, test := range ratBinTests { 208 arg := ratBinArg{test.x, test.y, test.sum} 209 testRatBin(t, i, "Add", (*Rat).Add, arg) 210 211 arg = ratBinArg{test.y, test.x, test.sum} 212 testRatBin(t, i, "Add symmetric", (*Rat).Add, arg) 213 214 arg = ratBinArg{test.sum, test.x, test.y} 215 testRatBin(t, i, "Sub", (*Rat).Sub, arg) 216 217 arg = ratBinArg{test.sum, test.y, test.x} 218 testRatBin(t, i, "Sub symmetric", (*Rat).Sub, arg) 219 220 arg = ratBinArg{test.x, test.y, test.prod} 221 testRatBin(t, i, "Mul", (*Rat).Mul, arg) 222 223 arg = ratBinArg{test.y, test.x, test.prod} 224 testRatBin(t, i, "Mul symmetric", (*Rat).Mul, arg) 225 226 if test.x != "0" { 227 arg = ratBinArg{test.prod, test.x, test.y} 228 testRatBin(t, i, "Quo", (*Rat).Quo, arg) 229 } 230 231 if test.y != "0" { 232 arg = ratBinArg{test.prod, test.y, test.x} 233 testRatBin(t, i, "Quo symmetric", (*Rat).Quo, arg) 234 } 235 } 236 } 237 238 func TestIssue820(t *testing.T) { 239 x := NewRat(3, 1) 240 y := NewRat(2, 1) 241 z := y.Quo(x, y) 242 q := NewRat(3, 2) 243 if z.Cmp(q) != 0 { 244 t.Errorf("got %s want %s", z, q) 245 } 246 247 y = NewRat(3, 1) 248 x = NewRat(2, 1) 249 z = y.Quo(x, y) 250 q = NewRat(2, 3) 251 if z.Cmp(q) != 0 { 252 t.Errorf("got %s want %s", z, q) 253 } 254 255 x = NewRat(3, 1) 256 z = x.Quo(x, x) 257 q = NewRat(3, 3) 258 if z.Cmp(q) != 0 { 259 t.Errorf("got %s want %s", z, q) 260 } 261 } 262 263 var setFrac64Tests = []struct { 264 a, b int64 265 out string 266 }{ 267 {0, 1, "0"}, 268 {0, -1, "0"}, 269 {1, 1, "1"}, 270 {-1, 1, "-1"}, 271 {1, -1, "-1"}, 272 {-1, -1, "1"}, 273 {-9223372036854775808, -9223372036854775808, "1"}, 274 } 275 276 func TestRatSetFrac64Rat(t *testing.T) { 277 for i, test := range setFrac64Tests { 278 x := new(Rat).SetFrac64(test.a, test.b) 279 if x.RatString() != test.out { 280 t.Errorf("#%d got %s want %s", i, x.RatString(), test.out) 281 } 282 } 283 } 284 285 func TestRatGobEncoding(t *testing.T) { 286 var medium bytes.Buffer 287 enc := gob.NewEncoder(&medium) 288 dec := gob.NewDecoder(&medium) 289 for _, test := range encodingTests { 290 medium.Reset() // empty buffer for each test case (in case of failures) 291 var tx Rat 292 tx.SetString(test + ".14159265") 293 if err := enc.Encode(&tx); err != nil { 294 t.Errorf("encoding of %s failed: %s", &tx, err) 295 } 296 var rx Rat 297 if err := dec.Decode(&rx); err != nil { 298 t.Errorf("decoding of %s failed: %s", &tx, err) 299 } 300 if rx.Cmp(&tx) != 0 { 301 t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx) 302 } 303 } 304 } 305 306 // Sending a nil Rat pointer (inside a slice) on a round trip through gob should yield a zero. 307 // TODO: top-level nils. 308 func TestGobEncodingNilRatInSlice(t *testing.T) { 309 buf := new(bytes.Buffer) 310 enc := gob.NewEncoder(buf) 311 dec := gob.NewDecoder(buf) 312 313 var in = make([]*Rat, 1) 314 err := enc.Encode(&in) 315 if err != nil { 316 t.Errorf("gob encode failed: %q", err) 317 } 318 var out []*Rat 319 err = dec.Decode(&out) 320 if err != nil { 321 t.Fatalf("gob decode failed: %q", err) 322 } 323 if len(out) != 1 { 324 t.Fatalf("wrong len; want 1 got %d", len(out)) 325 } 326 var zero Rat 327 if out[0].Cmp(&zero) != 0 { 328 t.Errorf("transmission of (*Int)(nill) failed: got %s want 0", out) 329 } 330 } 331 332 var ratNums = []string{ 333 "-141592653589793238462643383279502884197169399375105820974944592307816406286", 334 "-1415926535897932384626433832795028841971", 335 "-141592653589793", 336 "-1", 337 "0", 338 "1", 339 "141592653589793", 340 "1415926535897932384626433832795028841971", 341 "141592653589793238462643383279502884197169399375105820974944592307816406286", 342 } 343 344 var ratDenoms = []string{ 345 "1", 346 "718281828459045", 347 "7182818284590452353602874713526624977572", 348 "718281828459045235360287471352662497757247093699959574966967627724076630353", 349 } 350 351 func TestRatJSONEncoding(t *testing.T) { 352 for _, num := range ratNums { 353 for _, denom := range ratDenoms { 354 var tx Rat 355 tx.SetString(num + "/" + denom) 356 b, err := json.Marshal(&tx) 357 if err != nil { 358 t.Errorf("marshaling of %s failed: %s", &tx, err) 359 continue 360 } 361 var rx Rat 362 if err := json.Unmarshal(b, &rx); err != nil { 363 t.Errorf("unmarshaling of %s failed: %s", &tx, err) 364 continue 365 } 366 if rx.Cmp(&tx) != 0 { 367 t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx) 368 } 369 } 370 } 371 } 372 373 func TestRatXMLEncoding(t *testing.T) { 374 for _, num := range ratNums { 375 for _, denom := range ratDenoms { 376 var tx Rat 377 tx.SetString(num + "/" + denom) 378 b, err := xml.Marshal(&tx) 379 if err != nil { 380 t.Errorf("marshaling of %s failed: %s", &tx, err) 381 continue 382 } 383 var rx Rat 384 if err := xml.Unmarshal(b, &rx); err != nil { 385 t.Errorf("unmarshaling of %s failed: %s", &tx, err) 386 continue 387 } 388 if rx.Cmp(&tx) != 0 { 389 t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx) 390 } 391 } 392 } 393 } 394 395 func TestIssue2379(t *testing.T) { 396 // 1) no aliasing 397 q := NewRat(3, 2) 398 x := new(Rat) 399 x.SetFrac(NewInt(3), NewInt(2)) 400 if x.Cmp(q) != 0 { 401 t.Errorf("1) got %s want %s", x, q) 402 } 403 404 // 2) aliasing of numerator 405 x = NewRat(2, 3) 406 x.SetFrac(NewInt(3), x.Num()) 407 if x.Cmp(q) != 0 { 408 t.Errorf("2) got %s want %s", x, q) 409 } 410 411 // 3) aliasing of denominator 412 x = NewRat(2, 3) 413 x.SetFrac(x.Denom(), NewInt(2)) 414 if x.Cmp(q) != 0 { 415 t.Errorf("3) got %s want %s", x, q) 416 } 417 418 // 4) aliasing of numerator and denominator 419 x = NewRat(2, 3) 420 x.SetFrac(x.Denom(), x.Num()) 421 if x.Cmp(q) != 0 { 422 t.Errorf("4) got %s want %s", x, q) 423 } 424 425 // 5) numerator and denominator are the same 426 q = NewRat(1, 1) 427 x = new(Rat) 428 n := NewInt(7) 429 x.SetFrac(n, n) 430 if x.Cmp(q) != 0 { 431 t.Errorf("5) got %s want %s", x, q) 432 } 433 } 434 435 func TestIssue3521(t *testing.T) { 436 a := new(Int) 437 b := new(Int) 438 a.SetString("64375784358435883458348587", 0) 439 b.SetString("4789759874531", 0) 440 441 // 0) a raw zero value has 1 as denominator 442 zero := new(Rat) 443 one := NewInt(1) 444 if zero.Denom().Cmp(one) != 0 { 445 t.Errorf("0) got %s want %s", zero.Denom(), one) 446 } 447 448 // 1a) a zero value remains zero independent of denominator 449 x := new(Rat) 450 x.Denom().Set(new(Int).Neg(b)) 451 if x.Cmp(zero) != 0 { 452 t.Errorf("1a) got %s want %s", x, zero) 453 } 454 455 // 1b) a zero value may have a denominator != 0 and != 1 456 x.Num().Set(a) 457 qab := new(Rat).SetFrac(a, b) 458 if x.Cmp(qab) != 0 { 459 t.Errorf("1b) got %s want %s", x, qab) 460 } 461 462 // 2a) an integral value becomes a fraction depending on denominator 463 x.SetFrac64(10, 2) 464 x.Denom().SetInt64(3) 465 q53 := NewRat(5, 3) 466 if x.Cmp(q53) != 0 { 467 t.Errorf("2a) got %s want %s", x, q53) 468 } 469 470 // 2b) an integral value becomes a fraction depending on denominator 471 x = NewRat(10, 2) 472 x.Denom().SetInt64(3) 473 if x.Cmp(q53) != 0 { 474 t.Errorf("2b) got %s want %s", x, q53) 475 } 476 477 // 3) changing the numerator/denominator of a Rat changes the Rat 478 x.SetFrac(a, b) 479 a = x.Num() 480 b = x.Denom() 481 a.SetInt64(5) 482 b.SetInt64(3) 483 if x.Cmp(q53) != 0 { 484 t.Errorf("3) got %s want %s", x, q53) 485 } 486 } 487 488 func TestFloat32Distribution(t *testing.T) { 489 // Generate a distribution of (sign, mantissa, exp) values 490 // broader than the float32 range, and check Rat.Float32() 491 // always picks the closest float32 approximation. 492 var add = []int64{ 493 0, 494 1, 495 3, 496 5, 497 7, 498 9, 499 11, 500 } 501 var winc, einc = uint64(1), 1 // soak test (~1.5s on x86-64) 502 if testing.Short() { 503 winc, einc = 5, 15 // quick test (~60ms on x86-64) 504 } 505 506 for _, sign := range "+-" { 507 for _, a := range add { 508 for wid := uint64(0); wid < 30; wid += winc { 509 b := 1<<wid + a 510 if sign == '-' { 511 b = -b 512 } 513 for exp := -150; exp < 150; exp += einc { 514 num, den := NewInt(b), NewInt(1) 515 if exp > 0 { 516 num.Lsh(num, uint(exp)) 517 } else { 518 den.Lsh(den, uint(-exp)) 519 } 520 r := new(Rat).SetFrac(num, den) 521 f, _ := r.Float32() 522 523 if !checkIsBestApprox32(t, f, r) { 524 // Append context information. 525 t.Errorf("(input was mantissa %#x, exp %d; f = %g (%b); f ~ %g; r = %v)", 526 b, exp, f, f, math.Ldexp(float64(b), exp), r) 527 } 528 529 checkNonLossyRoundtrip32(t, f) 530 } 531 } 532 } 533 } 534 } 535 536 func TestFloat64Distribution(t *testing.T) { 537 // Generate a distribution of (sign, mantissa, exp) values 538 // broader than the float64 range, and check Rat.Float64() 539 // always picks the closest float64 approximation. 540 var add = []int64{ 541 0, 542 1, 543 3, 544 5, 545 7, 546 9, 547 11, 548 } 549 var winc, einc = uint64(1), 1 // soak test (~75s on x86-64) 550 if testing.Short() { 551 winc, einc = 10, 500 // quick test (~12ms on x86-64) 552 } 553 554 for _, sign := range "+-" { 555 for _, a := range add { 556 for wid := uint64(0); wid < 60; wid += winc { 557 b := 1<<wid + a 558 if sign == '-' { 559 b = -b 560 } 561 for exp := -1100; exp < 1100; exp += einc { 562 num, den := NewInt(b), NewInt(1) 563 if exp > 0 { 564 num.Lsh(num, uint(exp)) 565 } else { 566 den.Lsh(den, uint(-exp)) 567 } 568 r := new(Rat).SetFrac(num, den) 569 f, _ := r.Float64() 570 571 if !checkIsBestApprox64(t, f, r) { 572 // Append context information. 573 t.Errorf("(input was mantissa %#x, exp %d; f = %g (%b); f ~ %g; r = %v)", 574 b, exp, f, f, math.Ldexp(float64(b), exp), r) 575 } 576 577 checkNonLossyRoundtrip64(t, f) 578 } 579 } 580 } 581 } 582 } 583 584 // TestSetFloat64NonFinite checks that SetFloat64 of a non-finite value 585 // returns nil. 586 func TestSetFloat64NonFinite(t *testing.T) { 587 for _, f := range []float64{math.NaN(), math.Inf(+1), math.Inf(-1)} { 588 var r Rat 589 if r2 := r.SetFloat64(f); r2 != nil { 590 t.Errorf("SetFloat64(%g) was %v, want nil", f, r2) 591 } 592 } 593 } 594 595 // checkNonLossyRoundtrip32 checks that a float->Rat->float roundtrip is 596 // non-lossy for finite f. 597 func checkNonLossyRoundtrip32(t *testing.T, f float32) { 598 if !isFinite(float64(f)) { 599 return 600 } 601 r := new(Rat).SetFloat64(float64(f)) 602 if r == nil { 603 t.Errorf("Rat.SetFloat64(float64(%g) (%b)) == nil", f, f) 604 return 605 } 606 f2, exact := r.Float32() 607 if f != f2 || !exact { 608 t.Errorf("Rat.SetFloat64(float64(%g)).Float32() = %g (%b), %v, want %g (%b), %v; delta = %b", 609 f, f2, f2, exact, f, f, true, f2-f) 610 } 611 } 612 613 // checkNonLossyRoundtrip64 checks that a float->Rat->float roundtrip is 614 // non-lossy for finite f. 615 func checkNonLossyRoundtrip64(t *testing.T, f float64) { 616 if !isFinite(f) { 617 return 618 } 619 r := new(Rat).SetFloat64(f) 620 if r == nil { 621 t.Errorf("Rat.SetFloat64(%g (%b)) == nil", f, f) 622 return 623 } 624 f2, exact := r.Float64() 625 if f != f2 || !exact { 626 t.Errorf("Rat.SetFloat64(%g).Float64() = %g (%b), %v, want %g (%b), %v; delta = %b", 627 f, f2, f2, exact, f, f, true, f2-f) 628 } 629 } 630 631 // delta returns the absolute difference between r and f. 632 func delta(r *Rat, f float64) *Rat { 633 d := new(Rat).Sub(r, new(Rat).SetFloat64(f)) 634 return d.Abs(d) 635 } 636 637 // checkIsBestApprox32 checks that f is the best possible float32 638 // approximation of r. 639 // Returns true on success. 640 func checkIsBestApprox32(t *testing.T, f float32, r *Rat) bool { 641 if math.Abs(float64(f)) >= math.MaxFloat32 { 642 // Cannot check +Inf, -Inf, nor the float next to them (MaxFloat32). 643 // But we have tests for these special cases. 644 return true 645 } 646 647 // r must be strictly between f0 and f1, the floats bracketing f. 648 f0 := math.Nextafter32(f, float32(math.Inf(-1))) 649 f1 := math.Nextafter32(f, float32(math.Inf(+1))) 650 651 // For f to be correct, r must be closer to f than to f0 or f1. 652 df := delta(r, float64(f)) 653 df0 := delta(r, float64(f0)) 654 df1 := delta(r, float64(f1)) 655 if df.Cmp(df0) > 0 { 656 t.Errorf("Rat(%v).Float32() = %g (%b), but previous float32 %g (%b) is closer", r, f, f, f0, f0) 657 return false 658 } 659 if df.Cmp(df1) > 0 { 660 t.Errorf("Rat(%v).Float32() = %g (%b), but next float32 %g (%b) is closer", r, f, f, f1, f1) 661 return false 662 } 663 if df.Cmp(df0) == 0 && !isEven32(f) { 664 t.Errorf("Rat(%v).Float32() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f0, f0) 665 return false 666 } 667 if df.Cmp(df1) == 0 && !isEven32(f) { 668 t.Errorf("Rat(%v).Float32() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f1, f1) 669 return false 670 } 671 return true 672 } 673 674 // checkIsBestApprox64 checks that f is the best possible float64 675 // approximation of r. 676 // Returns true on success. 677 func checkIsBestApprox64(t *testing.T, f float64, r *Rat) bool { 678 if math.Abs(f) >= math.MaxFloat64 { 679 // Cannot check +Inf, -Inf, nor the float next to them (MaxFloat64). 680 // But we have tests for these special cases. 681 return true 682 } 683 684 // r must be strictly between f0 and f1, the floats bracketing f. 685 f0 := math.Nextafter(f, math.Inf(-1)) 686 f1 := math.Nextafter(f, math.Inf(+1)) 687 688 // For f to be correct, r must be closer to f than to f0 or f1. 689 df := delta(r, f) 690 df0 := delta(r, f0) 691 df1 := delta(r, f1) 692 if df.Cmp(df0) > 0 { 693 t.Errorf("Rat(%v).Float64() = %g (%b), but previous float64 %g (%b) is closer", r, f, f, f0, f0) 694 return false 695 } 696 if df.Cmp(df1) > 0 { 697 t.Errorf("Rat(%v).Float64() = %g (%b), but next float64 %g (%b) is closer", r, f, f, f1, f1) 698 return false 699 } 700 if df.Cmp(df0) == 0 && !isEven64(f) { 701 t.Errorf("Rat(%v).Float64() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f0, f0) 702 return false 703 } 704 if df.Cmp(df1) == 0 && !isEven64(f) { 705 t.Errorf("Rat(%v).Float64() = %g (%b); halfway should have rounded to %g (%b) instead", r, f, f, f1, f1) 706 return false 707 } 708 return true 709 } 710 711 func isEven32(f float32) bool { return math.Float32bits(f)&1 == 0 } 712 func isEven64(f float64) bool { return math.Float64bits(f)&1 == 0 } 713 714 func TestIsFinite(t *testing.T) { 715 finites := []float64{ 716 1.0 / 3, 717 4891559871276714924261e+222, 718 math.MaxFloat64, 719 math.SmallestNonzeroFloat64, 720 -math.MaxFloat64, 721 -math.SmallestNonzeroFloat64, 722 } 723 for _, f := range finites { 724 if !isFinite(f) { 725 t.Errorf("!IsFinite(%g (%b))", f, f) 726 } 727 } 728 nonfinites := []float64{ 729 math.NaN(), 730 math.Inf(-1), 731 math.Inf(+1), 732 } 733 for _, f := range nonfinites { 734 if isFinite(f) { 735 t.Errorf("IsFinite(%g, (%b))", f, f) 736 } 737 } 738 }