rat_test.go 19 KB

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