float_test.go 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  1. // Copyright 2014 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. "flag"
  7. "fmt"
  8. "math"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. )
  13. // Verify that ErrNaN implements the error interface.
  14. var _ error = ErrNaN{}
  15. func (x *Float) uint64() uint64 {
  16. u, acc := x.Uint64()
  17. if acc != Exact {
  18. panic(fmt.Sprintf("%s is not a uint64", x.Text('g', 10)))
  19. }
  20. return u
  21. }
  22. func (x *Float) int64() int64 {
  23. i, acc := x.Int64()
  24. if acc != Exact {
  25. panic(fmt.Sprintf("%s is not an int64", x.Text('g', 10)))
  26. }
  27. return i
  28. }
  29. func TestFloatZeroValue(t *testing.T) {
  30. // zero (uninitialized) value is a ready-to-use 0.0
  31. var x Float
  32. if s := x.Text('f', 1); s != "0.0" {
  33. t.Errorf("zero value = %s; want 0.0", s)
  34. }
  35. // zero value has precision 0
  36. if prec := x.Prec(); prec != 0 {
  37. t.Errorf("prec = %d; want 0", prec)
  38. }
  39. // zero value can be used in any and all positions of binary operations
  40. make := func(x int) *Float {
  41. var f Float
  42. if x != 0 {
  43. f.SetInt64(int64(x))
  44. }
  45. // x == 0 translates into the zero value
  46. return &f
  47. }
  48. for _, test := range []struct {
  49. z, x, y, want int
  50. opname rune
  51. op func(z, x, y *Float) *Float
  52. }{
  53. {0, 0, 0, 0, '+', (*Float).Add},
  54. {0, 1, 2, 3, '+', (*Float).Add},
  55. {1, 2, 0, 2, '+', (*Float).Add},
  56. {2, 0, 1, 1, '+', (*Float).Add},
  57. {0, 0, 0, 0, '-', (*Float).Sub},
  58. {0, 1, 2, -1, '-', (*Float).Sub},
  59. {1, 2, 0, 2, '-', (*Float).Sub},
  60. {2, 0, 1, -1, '-', (*Float).Sub},
  61. {0, 0, 0, 0, '*', (*Float).Mul},
  62. {0, 1, 2, 2, '*', (*Float).Mul},
  63. {1, 2, 0, 0, '*', (*Float).Mul},
  64. {2, 0, 1, 0, '*', (*Float).Mul},
  65. // {0, 0, 0, 0, '/', (*Float).Quo}, // panics
  66. {0, 2, 1, 2, '/', (*Float).Quo},
  67. {1, 2, 0, 0, '/', (*Float).Quo}, // = +Inf
  68. {2, 0, 1, 0, '/', (*Float).Quo},
  69. } {
  70. z := make(test.z)
  71. test.op(z, make(test.x), make(test.y))
  72. got := 0
  73. if !z.IsInf() {
  74. got = int(z.int64())
  75. }
  76. if got != test.want {
  77. t.Errorf("%d %c %d = %d; want %d", test.x, test.opname, test.y, got, test.want)
  78. }
  79. }
  80. // TODO(gri) test how precision is set for zero value results
  81. }
  82. func makeFloat(s string) *Float {
  83. x, _, err := ParseFloat(s, 0, 1000, ToNearestEven)
  84. if err != nil {
  85. panic(err)
  86. }
  87. return x
  88. }
  89. func TestFloatSetPrec(t *testing.T) {
  90. for _, test := range []struct {
  91. x string
  92. prec uint
  93. want string
  94. acc Accuracy
  95. }{
  96. // prec 0
  97. {"0", 0, "0", Exact},
  98. {"-0", 0, "-0", Exact},
  99. {"-Inf", 0, "-Inf", Exact},
  100. {"+Inf", 0, "+Inf", Exact},
  101. {"123", 0, "0", Below},
  102. {"-123", 0, "-0", Above},
  103. // prec at upper limit
  104. {"0", MaxPrec, "0", Exact},
  105. {"-0", MaxPrec, "-0", Exact},
  106. {"-Inf", MaxPrec, "-Inf", Exact},
  107. {"+Inf", MaxPrec, "+Inf", Exact},
  108. // just a few regular cases - general rounding is tested elsewhere
  109. {"1.5", 1, "2", Above},
  110. {"-1.5", 1, "-2", Below},
  111. {"123", 1e6, "123", Exact},
  112. {"-123", 1e6, "-123", Exact},
  113. } {
  114. x := makeFloat(test.x).SetPrec(test.prec)
  115. prec := test.prec
  116. if prec > MaxPrec {
  117. prec = MaxPrec
  118. }
  119. if got := x.Prec(); got != prec {
  120. t.Errorf("%s.SetPrec(%d).Prec() == %d; want %d", test.x, test.prec, got, prec)
  121. }
  122. if got, acc := x.String(), x.Acc(); got != test.want || acc != test.acc {
  123. t.Errorf("%s.SetPrec(%d) = %s (%s); want %s (%s)", test.x, test.prec, got, acc, test.want, test.acc)
  124. }
  125. }
  126. }
  127. func TestFloatMinPrec(t *testing.T) {
  128. const max = 100
  129. for _, test := range []struct {
  130. x string
  131. want uint
  132. }{
  133. {"0", 0},
  134. {"-0", 0},
  135. {"+Inf", 0},
  136. {"-Inf", 0},
  137. {"1", 1},
  138. {"2", 1},
  139. {"3", 2},
  140. {"0x8001", 16},
  141. {"0x8001p-1000", 16},
  142. {"0x8001p+1000", 16},
  143. {"0.1", max},
  144. } {
  145. x := makeFloat(test.x).SetPrec(max)
  146. if got := x.MinPrec(); got != test.want {
  147. t.Errorf("%s.MinPrec() = %d; want %d", test.x, got, test.want)
  148. }
  149. }
  150. }
  151. func TestFloatSign(t *testing.T) {
  152. for _, test := range []struct {
  153. x string
  154. s int
  155. }{
  156. {"-Inf", -1},
  157. {"-1", -1},
  158. {"-0", 0},
  159. {"+0", 0},
  160. {"+1", +1},
  161. {"+Inf", +1},
  162. } {
  163. x := makeFloat(test.x)
  164. s := x.Sign()
  165. if s != test.s {
  166. t.Errorf("%s.Sign() = %d; want %d", test.x, s, test.s)
  167. }
  168. }
  169. }
  170. // alike(x, y) is like x.Cmp(y) == 0 but also considers the sign of 0 (0 != -0).
  171. func alike(x, y *Float) bool {
  172. return x.Cmp(y) == 0 && x.Signbit() == y.Signbit()
  173. }
  174. func alike32(x, y float32) bool {
  175. // we can ignore NaNs
  176. return x == y && math.Signbit(float64(x)) == math.Signbit(float64(y))
  177. }
  178. func alike64(x, y float64) bool {
  179. // we can ignore NaNs
  180. return x == y && math.Signbit(x) == math.Signbit(y)
  181. }
  182. func TestFloatMantExp(t *testing.T) {
  183. for _, test := range []struct {
  184. x string
  185. mant string
  186. exp int
  187. }{
  188. {"0", "0", 0},
  189. {"+0", "0", 0},
  190. {"-0", "-0", 0},
  191. {"Inf", "+Inf", 0},
  192. {"+Inf", "+Inf", 0},
  193. {"-Inf", "-Inf", 0},
  194. {"1.5", "0.75", 1},
  195. {"1.024e3", "0.5", 11},
  196. {"-0.125", "-0.5", -2},
  197. } {
  198. x := makeFloat(test.x)
  199. mant := makeFloat(test.mant)
  200. m := new(Float)
  201. e := x.MantExp(m)
  202. if !alike(m, mant) || e != test.exp {
  203. t.Errorf("%s.MantExp() = %s, %d; want %s, %d", test.x, m.Text('g', 10), e, test.mant, test.exp)
  204. }
  205. }
  206. }
  207. func TestFloatMantExpAliasing(t *testing.T) {
  208. x := makeFloat("0.5p10")
  209. if e := x.MantExp(x); e != 10 {
  210. t.Fatalf("Float.MantExp aliasing error: got %d; want 10", e)
  211. }
  212. if want := makeFloat("0.5"); !alike(x, want) {
  213. t.Fatalf("Float.MantExp aliasing error: got %s; want %s", x.Text('g', 10), want.Text('g', 10))
  214. }
  215. }
  216. func TestFloatSetMantExp(t *testing.T) {
  217. for _, test := range []struct {
  218. frac string
  219. exp int
  220. z string
  221. }{
  222. {"0", 0, "0"},
  223. {"+0", 0, "0"},
  224. {"-0", 0, "-0"},
  225. {"Inf", 1234, "+Inf"},
  226. {"+Inf", -1234, "+Inf"},
  227. {"-Inf", -1234, "-Inf"},
  228. {"0", MinExp, "0"},
  229. {"0.25", MinExp, "+0"}, // exponent underflow
  230. {"-0.25", MinExp, "-0"}, // exponent underflow
  231. {"1", MaxExp, "+Inf"}, // exponent overflow
  232. {"2", MaxExp - 1, "+Inf"}, // exponent overflow
  233. {"0.75", 1, "1.5"},
  234. {"0.5", 11, "1024"},
  235. {"-0.5", -2, "-0.125"},
  236. {"32", 5, "1024"},
  237. {"1024", -10, "1"},
  238. } {
  239. frac := makeFloat(test.frac)
  240. want := makeFloat(test.z)
  241. var z Float
  242. z.SetMantExp(frac, test.exp)
  243. if !alike(&z, want) {
  244. t.Errorf("SetMantExp(%s, %d) = %s; want %s", test.frac, test.exp, z.Text('g', 10), test.z)
  245. }
  246. // test inverse property
  247. mant := new(Float)
  248. if z.SetMantExp(mant, want.MantExp(mant)).Cmp(want) != 0 {
  249. t.Errorf("Inverse property not satisfied: got %s; want %s", z.Text('g', 10), test.z)
  250. }
  251. }
  252. }
  253. func TestFloatPredicates(t *testing.T) {
  254. for _, test := range []struct {
  255. x string
  256. sign int
  257. signbit, inf bool
  258. }{
  259. {x: "-Inf", sign: -1, signbit: true, inf: true},
  260. {x: "-1", sign: -1, signbit: true},
  261. {x: "-0", signbit: true},
  262. {x: "0"},
  263. {x: "1", sign: 1},
  264. {x: "+Inf", sign: 1, inf: true},
  265. } {
  266. x := makeFloat(test.x)
  267. if got := x.Signbit(); got != test.signbit {
  268. t.Errorf("(%s).Signbit() = %v; want %v", test.x, got, test.signbit)
  269. }
  270. if got := x.Sign(); got != test.sign {
  271. t.Errorf("(%s).Sign() = %d; want %d", test.x, got, test.sign)
  272. }
  273. if got := x.IsInf(); got != test.inf {
  274. t.Errorf("(%s).IsInf() = %v; want %v", test.x, got, test.inf)
  275. }
  276. }
  277. }
  278. func TestFloatIsInt(t *testing.T) {
  279. for _, test := range []string{
  280. "0 int",
  281. "-0 int",
  282. "1 int",
  283. "-1 int",
  284. "0.5",
  285. "1.23",
  286. "1.23e1",
  287. "1.23e2 int",
  288. "0.000000001e+8",
  289. "0.000000001e+9 int",
  290. "1.2345e200 int",
  291. "Inf",
  292. "+Inf",
  293. "-Inf",
  294. } {
  295. s := strings.TrimSuffix(test, " int")
  296. want := s != test
  297. if got := makeFloat(s).IsInt(); got != want {
  298. t.Errorf("%s.IsInt() == %t", s, got)
  299. }
  300. }
  301. }
  302. func fromBinary(s string) int64 {
  303. x, err := strconv.ParseInt(s, 2, 64)
  304. if err != nil {
  305. panic(err)
  306. }
  307. return x
  308. }
  309. func toBinary(x int64) string {
  310. return strconv.FormatInt(x, 2)
  311. }
  312. func testFloatRound(t *testing.T, x, r int64, prec uint, mode RoundingMode) {
  313. // verify test data
  314. var ok bool
  315. switch mode {
  316. case ToNearestEven, ToNearestAway:
  317. ok = true // nothing to do for now
  318. case ToZero:
  319. if x < 0 {
  320. ok = r >= x
  321. } else {
  322. ok = r <= x
  323. }
  324. case AwayFromZero:
  325. if x < 0 {
  326. ok = r <= x
  327. } else {
  328. ok = r >= x
  329. }
  330. case ToNegativeInf:
  331. ok = r <= x
  332. case ToPositiveInf:
  333. ok = r >= x
  334. default:
  335. panic("unreachable")
  336. }
  337. if !ok {
  338. t.Fatalf("incorrect test data for prec = %d, %s: x = %s, r = %s", prec, mode, toBinary(x), toBinary(r))
  339. }
  340. // compute expected accuracy
  341. a := Exact
  342. switch {
  343. case r < x:
  344. a = Below
  345. case r > x:
  346. a = Above
  347. }
  348. // round
  349. f := new(Float).SetMode(mode).SetInt64(x).SetPrec(prec)
  350. // check result
  351. r1 := f.int64()
  352. p1 := f.Prec()
  353. a1 := f.Acc()
  354. if r1 != r || p1 != prec || a1 != a {
  355. t.Errorf("round %s (%d bits, %s) incorrect: got %s (%d bits, %s); want %s (%d bits, %s)",
  356. toBinary(x), prec, mode,
  357. toBinary(r1), p1, a1,
  358. toBinary(r), prec, a)
  359. return
  360. }
  361. // g and f should be the same
  362. // (rounding by SetPrec after SetInt64 using default precision
  363. // should be the same as rounding by SetInt64 after setting the
  364. // precision)
  365. g := new(Float).SetMode(mode).SetPrec(prec).SetInt64(x)
  366. if !alike(g, f) {
  367. t.Errorf("round %s (%d bits, %s) not symmetric: got %s and %s; want %s",
  368. toBinary(x), prec, mode,
  369. toBinary(g.int64()),
  370. toBinary(r1),
  371. toBinary(r),
  372. )
  373. return
  374. }
  375. // h and f should be the same
  376. // (repeated rounding should be idempotent)
  377. h := new(Float).SetMode(mode).SetPrec(prec).Set(f)
  378. if !alike(h, f) {
  379. t.Errorf("round %s (%d bits, %s) not idempotent: got %s and %s; want %s",
  380. toBinary(x), prec, mode,
  381. toBinary(h.int64()),
  382. toBinary(r1),
  383. toBinary(r),
  384. )
  385. return
  386. }
  387. }
  388. // TestFloatRound tests basic rounding.
  389. func TestFloatRound(t *testing.T) {
  390. for _, test := range []struct {
  391. prec uint
  392. x, zero, neven, naway, away string // input, results rounded to prec bits
  393. }{
  394. {5, "1000", "1000", "1000", "1000", "1000"},
  395. {5, "1001", "1001", "1001", "1001", "1001"},
  396. {5, "1010", "1010", "1010", "1010", "1010"},
  397. {5, "1011", "1011", "1011", "1011", "1011"},
  398. {5, "1100", "1100", "1100", "1100", "1100"},
  399. {5, "1101", "1101", "1101", "1101", "1101"},
  400. {5, "1110", "1110", "1110", "1110", "1110"},
  401. {5, "1111", "1111", "1111", "1111", "1111"},
  402. {4, "1000", "1000", "1000", "1000", "1000"},
  403. {4, "1001", "1001", "1001", "1001", "1001"},
  404. {4, "1010", "1010", "1010", "1010", "1010"},
  405. {4, "1011", "1011", "1011", "1011", "1011"},
  406. {4, "1100", "1100", "1100", "1100", "1100"},
  407. {4, "1101", "1101", "1101", "1101", "1101"},
  408. {4, "1110", "1110", "1110", "1110", "1110"},
  409. {4, "1111", "1111", "1111", "1111", "1111"},
  410. {3, "1000", "1000", "1000", "1000", "1000"},
  411. {3, "1001", "1000", "1000", "1010", "1010"},
  412. {3, "1010", "1010", "1010", "1010", "1010"},
  413. {3, "1011", "1010", "1100", "1100", "1100"},
  414. {3, "1100", "1100", "1100", "1100", "1100"},
  415. {3, "1101", "1100", "1100", "1110", "1110"},
  416. {3, "1110", "1110", "1110", "1110", "1110"},
  417. {3, "1111", "1110", "10000", "10000", "10000"},
  418. {3, "1000001", "1000000", "1000000", "1000000", "1010000"},
  419. {3, "1001001", "1000000", "1010000", "1010000", "1010000"},
  420. {3, "1010001", "1010000", "1010000", "1010000", "1100000"},
  421. {3, "1011001", "1010000", "1100000", "1100000", "1100000"},
  422. {3, "1100001", "1100000", "1100000", "1100000", "1110000"},
  423. {3, "1101001", "1100000", "1110000", "1110000", "1110000"},
  424. {3, "1110001", "1110000", "1110000", "1110000", "10000000"},
  425. {3, "1111001", "1110000", "10000000", "10000000", "10000000"},
  426. {2, "1000", "1000", "1000", "1000", "1000"},
  427. {2, "1001", "1000", "1000", "1000", "1100"},
  428. {2, "1010", "1000", "1000", "1100", "1100"},
  429. {2, "1011", "1000", "1100", "1100", "1100"},
  430. {2, "1100", "1100", "1100", "1100", "1100"},
  431. {2, "1101", "1100", "1100", "1100", "10000"},
  432. {2, "1110", "1100", "10000", "10000", "10000"},
  433. {2, "1111", "1100", "10000", "10000", "10000"},
  434. {2, "1000001", "1000000", "1000000", "1000000", "1100000"},
  435. {2, "1001001", "1000000", "1000000", "1000000", "1100000"},
  436. {2, "1010001", "1000000", "1100000", "1100000", "1100000"},
  437. {2, "1011001", "1000000", "1100000", "1100000", "1100000"},
  438. {2, "1100001", "1100000", "1100000", "1100000", "10000000"},
  439. {2, "1101001", "1100000", "1100000", "1100000", "10000000"},
  440. {2, "1110001", "1100000", "10000000", "10000000", "10000000"},
  441. {2, "1111001", "1100000", "10000000", "10000000", "10000000"},
  442. {1, "1000", "1000", "1000", "1000", "1000"},
  443. {1, "1001", "1000", "1000", "1000", "10000"},
  444. {1, "1010", "1000", "1000", "1000", "10000"},
  445. {1, "1011", "1000", "1000", "1000", "10000"},
  446. {1, "1100", "1000", "10000", "10000", "10000"},
  447. {1, "1101", "1000", "10000", "10000", "10000"},
  448. {1, "1110", "1000", "10000", "10000", "10000"},
  449. {1, "1111", "1000", "10000", "10000", "10000"},
  450. {1, "1000001", "1000000", "1000000", "1000000", "10000000"},
  451. {1, "1001001", "1000000", "1000000", "1000000", "10000000"},
  452. {1, "1010001", "1000000", "1000000", "1000000", "10000000"},
  453. {1, "1011001", "1000000", "1000000", "1000000", "10000000"},
  454. {1, "1100001", "1000000", "10000000", "10000000", "10000000"},
  455. {1, "1101001", "1000000", "10000000", "10000000", "10000000"},
  456. {1, "1110001", "1000000", "10000000", "10000000", "10000000"},
  457. {1, "1111001", "1000000", "10000000", "10000000", "10000000"},
  458. } {
  459. x := fromBinary(test.x)
  460. z := fromBinary(test.zero)
  461. e := fromBinary(test.neven)
  462. n := fromBinary(test.naway)
  463. a := fromBinary(test.away)
  464. prec := test.prec
  465. testFloatRound(t, x, z, prec, ToZero)
  466. testFloatRound(t, x, e, prec, ToNearestEven)
  467. testFloatRound(t, x, n, prec, ToNearestAway)
  468. testFloatRound(t, x, a, prec, AwayFromZero)
  469. testFloatRound(t, x, z, prec, ToNegativeInf)
  470. testFloatRound(t, x, a, prec, ToPositiveInf)
  471. testFloatRound(t, -x, -a, prec, ToNegativeInf)
  472. testFloatRound(t, -x, -z, prec, ToPositiveInf)
  473. }
  474. }
  475. // TestFloatRound24 tests that rounding a float64 to 24 bits
  476. // matches IEEE-754 rounding to nearest when converting a
  477. // float64 to a float32 (excluding denormal numbers).
  478. func TestFloatRound24(t *testing.T) {
  479. const x0 = 1<<26 - 0x10 // 11...110000 (26 bits)
  480. for d := 0; d <= 0x10; d++ {
  481. x := float64(x0 + d)
  482. f := new(Float).SetPrec(24).SetFloat64(x)
  483. got, _ := f.Float32()
  484. want := float32(x)
  485. if got != want {
  486. t.Errorf("Round(%g, 24) = %g; want %g", x, got, want)
  487. }
  488. }
  489. }
  490. func TestFloatSetUint64(t *testing.T) {
  491. for _, want := range []uint64{
  492. 0,
  493. 1,
  494. 2,
  495. 10,
  496. 100,
  497. 1<<32 - 1,
  498. 1 << 32,
  499. 1<<64 - 1,
  500. } {
  501. var f Float
  502. f.SetUint64(want)
  503. if got := f.uint64(); got != want {
  504. t.Errorf("got %#x (%s); want %#x", got, f.Text('p', 0), want)
  505. }
  506. }
  507. // test basic rounding behavior (exhaustive rounding testing is done elsewhere)
  508. const x uint64 = 0x8765432187654321 // 64 bits needed
  509. for prec := uint(1); prec <= 64; prec++ {
  510. f := new(Float).SetPrec(prec).SetMode(ToZero).SetUint64(x)
  511. got := f.uint64()
  512. want := x &^ (1<<(64-prec) - 1) // cut off (round to zero) low 64-prec bits
  513. if got != want {
  514. t.Errorf("got %#x (%s); want %#x", got, f.Text('p', 0), want)
  515. }
  516. }
  517. }
  518. func TestFloatSetInt64(t *testing.T) {
  519. for _, want := range []int64{
  520. 0,
  521. 1,
  522. 2,
  523. 10,
  524. 100,
  525. 1<<32 - 1,
  526. 1 << 32,
  527. 1<<63 - 1,
  528. } {
  529. for i := range [2]int{} {
  530. if i&1 != 0 {
  531. want = -want
  532. }
  533. var f Float
  534. f.SetInt64(want)
  535. if got := f.int64(); got != want {
  536. t.Errorf("got %#x (%s); want %#x", got, f.Text('p', 0), want)
  537. }
  538. }
  539. }
  540. // test basic rounding behavior (exhaustive rounding testing is done elsewhere)
  541. const x int64 = 0x7654321076543210 // 63 bits needed
  542. for prec := uint(1); prec <= 63; prec++ {
  543. f := new(Float).SetPrec(prec).SetMode(ToZero).SetInt64(x)
  544. got := f.int64()
  545. want := x &^ (1<<(63-prec) - 1) // cut off (round to zero) low 63-prec bits
  546. if got != want {
  547. t.Errorf("got %#x (%s); want %#x", got, f.Text('p', 0), want)
  548. }
  549. }
  550. }
  551. func TestFloatSetFloat64(t *testing.T) {
  552. for _, want := range []float64{
  553. 0,
  554. 1,
  555. 2,
  556. 12345,
  557. 1e10,
  558. 1e100,
  559. 3.14159265e10,
  560. 2.718281828e-123,
  561. 1.0 / 3,
  562. math.MaxFloat32,
  563. math.MaxFloat64,
  564. math.SmallestNonzeroFloat32,
  565. math.SmallestNonzeroFloat64,
  566. math.Inf(-1),
  567. math.Inf(0),
  568. -math.Inf(1),
  569. } {
  570. for i := range [2]int{} {
  571. if i&1 != 0 {
  572. want = -want
  573. }
  574. var f Float
  575. f.SetFloat64(want)
  576. if got, acc := f.Float64(); got != want || acc != Exact {
  577. t.Errorf("got %g (%s, %s); want %g (Exact)", got, f.Text('p', 0), acc, want)
  578. }
  579. }
  580. }
  581. // test basic rounding behavior (exhaustive rounding testing is done elsewhere)
  582. const x uint64 = 0x8765432143218 // 53 bits needed
  583. for prec := uint(1); prec <= 52; prec++ {
  584. f := new(Float).SetPrec(prec).SetMode(ToZero).SetFloat64(float64(x))
  585. got, _ := f.Float64()
  586. want := float64(x &^ (1<<(52-prec) - 1)) // cut off (round to zero) low 53-prec bits
  587. if got != want {
  588. t.Errorf("got %g (%s); want %g", got, f.Text('p', 0), want)
  589. }
  590. }
  591. // test NaN
  592. defer func() {
  593. if p, ok := recover().(ErrNaN); !ok {
  594. t.Errorf("got %v; want ErrNaN panic", p)
  595. }
  596. }()
  597. var f Float
  598. f.SetFloat64(math.NaN())
  599. // should not reach here
  600. t.Errorf("got %s; want ErrNaN panic", f.Text('p', 0))
  601. }
  602. func TestFloatSetInt(t *testing.T) {
  603. for _, want := range []string{
  604. "0",
  605. "1",
  606. "-1",
  607. "1234567890",
  608. "123456789012345678901234567890",
  609. "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
  610. } {
  611. var x Int
  612. _, ok := x.SetString(want, 0)
  613. if !ok {
  614. t.Errorf("invalid integer %s", want)
  615. continue
  616. }
  617. n := x.BitLen()
  618. var f Float
  619. f.SetInt(&x)
  620. // check precision
  621. if n < 64 {
  622. n = 64
  623. }
  624. if prec := f.Prec(); prec != uint(n) {
  625. t.Errorf("got prec = %d; want %d", prec, n)
  626. }
  627. // check value
  628. got := f.Text('g', 100)
  629. if got != want {
  630. t.Errorf("got %s (%s); want %s", got, f.Text('p', 0), want)
  631. }
  632. }
  633. // TODO(gri) test basic rounding behavior
  634. }
  635. func TestFloatSetRat(t *testing.T) {
  636. for _, want := range []string{
  637. "0",
  638. "1",
  639. "-1",
  640. "1234567890",
  641. "123456789012345678901234567890",
  642. "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
  643. "1.2",
  644. "3.14159265",
  645. // TODO(gri) expand
  646. } {
  647. var x Rat
  648. _, ok := x.SetString(want)
  649. if !ok {
  650. t.Errorf("invalid fraction %s", want)
  651. continue
  652. }
  653. n := max(x.Num().BitLen(), x.Denom().BitLen())
  654. var f1, f2 Float
  655. f2.SetPrec(1000)
  656. f1.SetRat(&x)
  657. f2.SetRat(&x)
  658. // check precision when set automatically
  659. if n < 64 {
  660. n = 64
  661. }
  662. if prec := f1.Prec(); prec != uint(n) {
  663. t.Errorf("got prec = %d; want %d", prec, n)
  664. }
  665. got := f2.Text('g', 100)
  666. if got != want {
  667. t.Errorf("got %s (%s); want %s", got, f2.Text('p', 0), want)
  668. }
  669. }
  670. }
  671. func TestFloatSetInf(t *testing.T) {
  672. var f Float
  673. for _, test := range []struct {
  674. signbit bool
  675. prec uint
  676. want string
  677. }{
  678. {false, 0, "+Inf"},
  679. {true, 0, "-Inf"},
  680. {false, 10, "+Inf"},
  681. {true, 30, "-Inf"},
  682. } {
  683. x := f.SetPrec(test.prec).SetInf(test.signbit)
  684. if got := x.String(); got != test.want || x.Prec() != test.prec {
  685. t.Errorf("SetInf(%v) = %s (prec = %d); want %s (prec = %d)", test.signbit, got, x.Prec(), test.want, test.prec)
  686. }
  687. }
  688. }
  689. func TestFloatUint64(t *testing.T) {
  690. for _, test := range []struct {
  691. x string
  692. out uint64
  693. acc Accuracy
  694. }{
  695. {"-Inf", 0, Above},
  696. {"-1", 0, Above},
  697. {"-1e-1000", 0, Above},
  698. {"-0", 0, Exact},
  699. {"0", 0, Exact},
  700. {"1e-1000", 0, Below},
  701. {"1", 1, Exact},
  702. {"1.000000000000000000001", 1, Below},
  703. {"12345.0", 12345, Exact},
  704. {"12345.000000000000000000001", 12345, Below},
  705. {"18446744073709551615", 18446744073709551615, Exact},
  706. {"18446744073709551615.000000000000000000001", math.MaxUint64, Below},
  707. {"18446744073709551616", math.MaxUint64, Below},
  708. {"1e10000", math.MaxUint64, Below},
  709. {"+Inf", math.MaxUint64, Below},
  710. } {
  711. x := makeFloat(test.x)
  712. out, acc := x.Uint64()
  713. if out != test.out || acc != test.acc {
  714. t.Errorf("%s: got %d (%s); want %d (%s)", test.x, out, acc, test.out, test.acc)
  715. }
  716. }
  717. }
  718. func TestFloatInt64(t *testing.T) {
  719. for _, test := range []struct {
  720. x string
  721. out int64
  722. acc Accuracy
  723. }{
  724. {"-Inf", math.MinInt64, Above},
  725. {"-1e10000", math.MinInt64, Above},
  726. {"-9223372036854775809", math.MinInt64, Above},
  727. {"-9223372036854775808.000000000000000000001", math.MinInt64, Above},
  728. {"-9223372036854775808", -9223372036854775808, Exact},
  729. {"-9223372036854775807.000000000000000000001", -9223372036854775807, Above},
  730. {"-9223372036854775807", -9223372036854775807, Exact},
  731. {"-12345.000000000000000000001", -12345, Above},
  732. {"-12345.0", -12345, Exact},
  733. {"-1.000000000000000000001", -1, Above},
  734. {"-1.5", -1, Above},
  735. {"-1", -1, Exact},
  736. {"-1e-1000", 0, Above},
  737. {"0", 0, Exact},
  738. {"1e-1000", 0, Below},
  739. {"1", 1, Exact},
  740. {"1.000000000000000000001", 1, Below},
  741. {"1.5", 1, Below},
  742. {"12345.0", 12345, Exact},
  743. {"12345.000000000000000000001", 12345, Below},
  744. {"9223372036854775807", 9223372036854775807, Exact},
  745. {"9223372036854775807.000000000000000000001", math.MaxInt64, Below},
  746. {"9223372036854775808", math.MaxInt64, Below},
  747. {"1e10000", math.MaxInt64, Below},
  748. {"+Inf", math.MaxInt64, Below},
  749. } {
  750. x := makeFloat(test.x)
  751. out, acc := x.Int64()
  752. if out != test.out || acc != test.acc {
  753. t.Errorf("%s: got %d (%s); want %d (%s)", test.x, out, acc, test.out, test.acc)
  754. }
  755. }
  756. }
  757. func TestFloatFloat32(t *testing.T) {
  758. for _, test := range []struct {
  759. x string
  760. out float32
  761. acc Accuracy
  762. }{
  763. {"0", 0, Exact},
  764. // underflow to zero
  765. {"1e-1000", 0, Below},
  766. {"0x0.000002p-127", 0, Below},
  767. {"0x.0000010p-126", 0, Below},
  768. // denormals
  769. {"1.401298464e-45", math.SmallestNonzeroFloat32, Above}, // rounded up to smallest denormal
  770. {"0x.ffffff8p-149", math.SmallestNonzeroFloat32, Above}, // rounded up to smallest denormal
  771. {"0x.0000018p-126", math.SmallestNonzeroFloat32, Above}, // rounded up to smallest denormal
  772. {"0x.0000020p-126", math.SmallestNonzeroFloat32, Exact},
  773. {"0x.8p-148", math.SmallestNonzeroFloat32, Exact},
  774. {"1p-149", math.SmallestNonzeroFloat32, Exact},
  775. {"0x.fffffep-126", math.Float32frombits(0x7fffff), Exact}, // largest denormal
  776. // special denormal cases (see issues 14553, 14651)
  777. {"0x0.0000001p-126", math.Float32frombits(0x00000000), Below}, // underflow to zero
  778. {"0x0.0000008p-126", math.Float32frombits(0x00000000), Below}, // underflow to zero
  779. {"0x0.0000010p-126", math.Float32frombits(0x00000000), Below}, // rounded down to even
  780. {"0x0.0000011p-126", math.Float32frombits(0x00000001), Above}, // rounded up to smallest denormal
  781. {"0x0.0000018p-126", math.Float32frombits(0x00000001), Above}, // rounded up to smallest denormal
  782. {"0x1.0000000p-149", math.Float32frombits(0x00000001), Exact}, // smallest denormal
  783. {"0x0.0000020p-126", math.Float32frombits(0x00000001), Exact}, // smallest denormal
  784. {"0x0.fffffe0p-126", math.Float32frombits(0x007fffff), Exact}, // largest denormal
  785. {"0x1.0000000p-126", math.Float32frombits(0x00800000), Exact}, // smallest normal
  786. {"0x0.8p-149", math.Float32frombits(0x000000000), Below}, // rounded down to even
  787. {"0x0.9p-149", math.Float32frombits(0x000000001), Above}, // rounded up to smallest denormal
  788. {"0x0.ap-149", math.Float32frombits(0x000000001), Above}, // rounded up to smallest denormal
  789. {"0x0.bp-149", math.Float32frombits(0x000000001), Above}, // rounded up to smallest denormal
  790. {"0x0.cp-149", math.Float32frombits(0x000000001), Above}, // rounded up to smallest denormal
  791. {"0x1.0p-149", math.Float32frombits(0x000000001), Exact}, // smallest denormal
  792. {"0x1.7p-149", math.Float32frombits(0x000000001), Below},
  793. {"0x1.8p-149", math.Float32frombits(0x000000002), Above},
  794. {"0x1.9p-149", math.Float32frombits(0x000000002), Above},
  795. {"0x2.0p-149", math.Float32frombits(0x000000002), Exact},
  796. {"0x2.8p-149", math.Float32frombits(0x000000002), Below}, // rounded down to even
  797. {"0x2.9p-149", math.Float32frombits(0x000000003), Above},
  798. {"0x3.0p-149", math.Float32frombits(0x000000003), Exact},
  799. {"0x3.7p-149", math.Float32frombits(0x000000003), Below},
  800. {"0x3.8p-149", math.Float32frombits(0x000000004), Above}, // rounded up to even
  801. {"0x4.0p-149", math.Float32frombits(0x000000004), Exact},
  802. {"0x4.8p-149", math.Float32frombits(0x000000004), Below}, // rounded down to even
  803. {"0x4.9p-149", math.Float32frombits(0x000000005), Above},
  804. // specific case from issue 14553
  805. {"0x7.7p-149", math.Float32frombits(0x000000007), Below},
  806. {"0x7.8p-149", math.Float32frombits(0x000000008), Above},
  807. {"0x7.9p-149", math.Float32frombits(0x000000008), Above},
  808. // normals
  809. {"0x.ffffffp-126", math.Float32frombits(0x00800000), Above}, // rounded up to smallest normal
  810. {"1p-126", math.Float32frombits(0x00800000), Exact}, // smallest normal
  811. {"0x1.fffffep-126", math.Float32frombits(0x00ffffff), Exact},
  812. {"0x1.ffffffp-126", math.Float32frombits(0x01000000), Above}, // rounded up
  813. {"1", 1, Exact},
  814. {"1.000000000000000000001", 1, Below},
  815. {"12345.0", 12345, Exact},
  816. {"12345.000000000000000000001", 12345, Below},
  817. {"0x1.fffffe0p127", math.MaxFloat32, Exact},
  818. {"0x1.fffffe8p127", math.MaxFloat32, Below},
  819. // overflow
  820. {"0x1.ffffff0p127", float32(math.Inf(+1)), Above},
  821. {"0x1p128", float32(math.Inf(+1)), Above},
  822. {"1e10000", float32(math.Inf(+1)), Above},
  823. {"0x1.ffffff0p2147483646", float32(math.Inf(+1)), Above}, // overflow in rounding
  824. // inf
  825. {"Inf", float32(math.Inf(+1)), Exact},
  826. } {
  827. for i := 0; i < 2; i++ {
  828. // test both signs
  829. tx, tout, tacc := test.x, test.out, test.acc
  830. if i != 0 {
  831. tx = "-" + tx
  832. tout = -tout
  833. tacc = -tacc
  834. }
  835. // conversion should match strconv where syntax is agreeable
  836. if f, err := strconv.ParseFloat(tx, 32); err == nil && !alike32(float32(f), tout) {
  837. t.Errorf("%s: got %g; want %g (incorrect test data)", tx, f, tout)
  838. }
  839. x := makeFloat(tx)
  840. out, acc := x.Float32()
  841. if !alike32(out, tout) || acc != tacc {
  842. t.Errorf("%s: got %g (%#08x, %s); want %g (%#08x, %s)", tx, out, math.Float32bits(out), acc, test.out, math.Float32bits(test.out), tacc)
  843. }
  844. // test that x.SetFloat64(float64(f)).Float32() == f
  845. var x2 Float
  846. out2, acc2 := x2.SetFloat64(float64(out)).Float32()
  847. if !alike32(out2, out) || acc2 != Exact {
  848. t.Errorf("idempotency test: got %g (%s); want %g (Exact)", out2, acc2, out)
  849. }
  850. }
  851. }
  852. }
  853. func TestFloatFloat64(t *testing.T) {
  854. const smallestNormalFloat64 = 2.2250738585072014e-308 // 1p-1022
  855. for _, test := range []struct {
  856. x string
  857. out float64
  858. acc Accuracy
  859. }{
  860. {"0", 0, Exact},
  861. // underflow to zero
  862. {"1e-1000", 0, Below},
  863. {"0x0.0000000000001p-1023", 0, Below},
  864. {"0x0.00000000000008p-1022", 0, Below},
  865. // denormals
  866. {"0x0.0000000000000cp-1022", math.SmallestNonzeroFloat64, Above}, // rounded up to smallest denormal
  867. {"0x0.00000000000010p-1022", math.SmallestNonzeroFloat64, Exact}, // smallest denormal
  868. {"0x.8p-1073", math.SmallestNonzeroFloat64, Exact},
  869. {"1p-1074", math.SmallestNonzeroFloat64, Exact},
  870. {"0x.fffffffffffffp-1022", math.Float64frombits(0x000fffffffffffff), Exact}, // largest denormal
  871. // special denormal cases (see issues 14553, 14651)
  872. {"0x0.00000000000001p-1022", math.Float64frombits(0x00000000000000000), Below}, // underflow to zero
  873. {"0x0.00000000000004p-1022", math.Float64frombits(0x00000000000000000), Below}, // underflow to zero
  874. {"0x0.00000000000008p-1022", math.Float64frombits(0x00000000000000000), Below}, // rounded down to even
  875. {"0x0.00000000000009p-1022", math.Float64frombits(0x00000000000000001), Above}, // rounded up to smallest denormal
  876. {"0x0.0000000000000ap-1022", math.Float64frombits(0x00000000000000001), Above}, // rounded up to smallest denormal
  877. {"0x0.8p-1074", math.Float64frombits(0x00000000000000000), Below}, // rounded down to even
  878. {"0x0.9p-1074", math.Float64frombits(0x00000000000000001), Above}, // rounded up to smallest denormal
  879. {"0x0.ap-1074", math.Float64frombits(0x00000000000000001), Above}, // rounded up to smallest denormal
  880. {"0x0.bp-1074", math.Float64frombits(0x00000000000000001), Above}, // rounded up to smallest denormal
  881. {"0x0.cp-1074", math.Float64frombits(0x00000000000000001), Above}, // rounded up to smallest denormal
  882. {"0x1.0p-1074", math.Float64frombits(0x00000000000000001), Exact},
  883. {"0x1.7p-1074", math.Float64frombits(0x00000000000000001), Below},
  884. {"0x1.8p-1074", math.Float64frombits(0x00000000000000002), Above},
  885. {"0x1.9p-1074", math.Float64frombits(0x00000000000000002), Above},
  886. {"0x2.0p-1074", math.Float64frombits(0x00000000000000002), Exact},
  887. {"0x2.8p-1074", math.Float64frombits(0x00000000000000002), Below}, // rounded down to even
  888. {"0x2.9p-1074", math.Float64frombits(0x00000000000000003), Above},
  889. {"0x3.0p-1074", math.Float64frombits(0x00000000000000003), Exact},
  890. {"0x3.7p-1074", math.Float64frombits(0x00000000000000003), Below},
  891. {"0x3.8p-1074", math.Float64frombits(0x00000000000000004), Above}, // rounded up to even
  892. {"0x4.0p-1074", math.Float64frombits(0x00000000000000004), Exact},
  893. {"0x4.8p-1074", math.Float64frombits(0x00000000000000004), Below}, // rounded down to even
  894. {"0x4.9p-1074", math.Float64frombits(0x00000000000000005), Above},
  895. // normals
  896. {"0x.fffffffffffff8p-1022", math.Float64frombits(0x0010000000000000), Above}, // rounded up to smallest normal
  897. {"1p-1022", math.Float64frombits(0x0010000000000000), Exact}, // smallest normal
  898. {"1", 1, Exact},
  899. {"1.000000000000000000001", 1, Below},
  900. {"12345.0", 12345, Exact},
  901. {"12345.000000000000000000001", 12345, Below},
  902. {"0x1.fffffffffffff0p1023", math.MaxFloat64, Exact},
  903. {"0x1.fffffffffffff4p1023", math.MaxFloat64, Below},
  904. // overflow
  905. {"0x1.fffffffffffff8p1023", math.Inf(+1), Above},
  906. {"0x1p1024", math.Inf(+1), Above},
  907. {"1e10000", math.Inf(+1), Above},
  908. {"0x1.fffffffffffff8p2147483646", math.Inf(+1), Above}, // overflow in rounding
  909. {"Inf", math.Inf(+1), Exact},
  910. // selected denormalized values that were handled incorrectly in the past
  911. {"0x.fffffffffffffp-1022", smallestNormalFloat64 - math.SmallestNonzeroFloat64, Exact},
  912. {"4503599627370495p-1074", smallestNormalFloat64 - math.SmallestNonzeroFloat64, Exact},
  913. // https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
  914. {"2.2250738585072011e-308", 2.225073858507201e-308, Below},
  915. // https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
  916. {"2.2250738585072012e-308", 2.2250738585072014e-308, Above},
  917. } {
  918. for i := 0; i < 2; i++ {
  919. // test both signs
  920. tx, tout, tacc := test.x, test.out, test.acc
  921. if i != 0 {
  922. tx = "-" + tx
  923. tout = -tout
  924. tacc = -tacc
  925. }
  926. // conversion should match strconv where syntax is agreeable
  927. if f, err := strconv.ParseFloat(tx, 64); err == nil && !alike64(f, tout) {
  928. t.Errorf("%s: got %g; want %g (incorrect test data)", tx, f, tout)
  929. }
  930. x := makeFloat(tx)
  931. out, acc := x.Float64()
  932. if !alike64(out, tout) || acc != tacc {
  933. t.Errorf("%s: got %g (%#016x, %s); want %g (%#016x, %s)", tx, out, math.Float64bits(out), acc, test.out, math.Float64bits(test.out), tacc)
  934. }
  935. // test that x.SetFloat64(f).Float64() == f
  936. var x2 Float
  937. out2, acc2 := x2.SetFloat64(out).Float64()
  938. if !alike64(out2, out) || acc2 != Exact {
  939. t.Errorf("idempotency test: got %g (%s); want %g (Exact)", out2, acc2, out)
  940. }
  941. }
  942. }
  943. }
  944. func TestFloatInt(t *testing.T) {
  945. for _, test := range []struct {
  946. x string
  947. want string
  948. acc Accuracy
  949. }{
  950. {"0", "0", Exact},
  951. {"+0", "0", Exact},
  952. {"-0", "0", Exact},
  953. {"Inf", "nil", Below},
  954. {"+Inf", "nil", Below},
  955. {"-Inf", "nil", Above},
  956. {"1", "1", Exact},
  957. {"-1", "-1", Exact},
  958. {"1.23", "1", Below},
  959. {"-1.23", "-1", Above},
  960. {"123e-2", "1", Below},
  961. {"123e-3", "0", Below},
  962. {"123e-4", "0", Below},
  963. {"1e-1000", "0", Below},
  964. {"-1e-1000", "0", Above},
  965. {"1e+10", "10000000000", Exact},
  966. {"1e+100", "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", Exact},
  967. } {
  968. x := makeFloat(test.x)
  969. res, acc := x.Int(nil)
  970. got := "nil"
  971. if res != nil {
  972. got = res.String()
  973. }
  974. if got != test.want || acc != test.acc {
  975. t.Errorf("%s: got %s (%s); want %s (%s)", test.x, got, acc, test.want, test.acc)
  976. }
  977. }
  978. // check that supplied *Int is used
  979. for _, f := range []string{"0", "1", "-1", "1234"} {
  980. x := makeFloat(f)
  981. i := new(Int)
  982. if res, _ := x.Int(i); res != i {
  983. t.Errorf("(%s).Int is not using supplied *Int", f)
  984. }
  985. }
  986. }
  987. func TestFloatRat(t *testing.T) {
  988. for _, test := range []struct {
  989. x, want string
  990. acc Accuracy
  991. }{
  992. {"0", "0/1", Exact},
  993. {"+0", "0/1", Exact},
  994. {"-0", "0/1", Exact},
  995. {"Inf", "nil", Below},
  996. {"+Inf", "nil", Below},
  997. {"-Inf", "nil", Above},
  998. {"1", "1/1", Exact},
  999. {"-1", "-1/1", Exact},
  1000. {"1.25", "5/4", Exact},
  1001. {"-1.25", "-5/4", Exact},
  1002. {"1e10", "10000000000/1", Exact},
  1003. {"1p10", "1024/1", Exact},
  1004. {"-1p-10", "-1/1024", Exact},
  1005. {"3.14159265", "7244019449799623199/2305843009213693952", Exact},
  1006. } {
  1007. x := makeFloat(test.x).SetPrec(64)
  1008. res, acc := x.Rat(nil)
  1009. got := "nil"
  1010. if res != nil {
  1011. got = res.String()
  1012. }
  1013. if got != test.want {
  1014. t.Errorf("%s: got %s; want %s", test.x, got, test.want)
  1015. continue
  1016. }
  1017. if acc != test.acc {
  1018. t.Errorf("%s: got %s; want %s", test.x, acc, test.acc)
  1019. continue
  1020. }
  1021. // inverse conversion
  1022. if res != nil {
  1023. got := new(Float).SetPrec(64).SetRat(res)
  1024. if got.Cmp(x) != 0 {
  1025. t.Errorf("%s: got %s; want %s", test.x, got, x)
  1026. }
  1027. }
  1028. }
  1029. // check that supplied *Rat is used
  1030. for _, f := range []string{"0", "1", "-1", "1234"} {
  1031. x := makeFloat(f)
  1032. r := new(Rat)
  1033. if res, _ := x.Rat(r); res != r {
  1034. t.Errorf("(%s).Rat is not using supplied *Rat", f)
  1035. }
  1036. }
  1037. }
  1038. func TestFloatAbs(t *testing.T) {
  1039. for _, test := range []string{
  1040. "0",
  1041. "1",
  1042. "1234",
  1043. "1.23e-2",
  1044. "1e-1000",
  1045. "1e1000",
  1046. "Inf",
  1047. } {
  1048. p := makeFloat(test)
  1049. a := new(Float).Abs(p)
  1050. if !alike(a, p) {
  1051. t.Errorf("%s: got %s; want %s", test, a.Text('g', 10), test)
  1052. }
  1053. n := makeFloat("-" + test)
  1054. a.Abs(n)
  1055. if !alike(a, p) {
  1056. t.Errorf("-%s: got %s; want %s", test, a.Text('g', 10), test)
  1057. }
  1058. }
  1059. }
  1060. func TestFloatNeg(t *testing.T) {
  1061. for _, test := range []string{
  1062. "0",
  1063. "1",
  1064. "1234",
  1065. "1.23e-2",
  1066. "1e-1000",
  1067. "1e1000",
  1068. "Inf",
  1069. } {
  1070. p1 := makeFloat(test)
  1071. n1 := makeFloat("-" + test)
  1072. n2 := new(Float).Neg(p1)
  1073. p2 := new(Float).Neg(n2)
  1074. if !alike(n2, n1) {
  1075. t.Errorf("%s: got %s; want %s", test, n2.Text('g', 10), n1.Text('g', 10))
  1076. }
  1077. if !alike(p2, p1) {
  1078. t.Errorf("%s: got %s; want %s", test, p2.Text('g', 10), p1.Text('g', 10))
  1079. }
  1080. }
  1081. }
  1082. func TestFloatInc(t *testing.T) {
  1083. const n = 10
  1084. for _, prec := range precList {
  1085. if 1<<prec < n {
  1086. continue // prec must be large enough to hold all numbers from 0 to n
  1087. }
  1088. var x, one Float
  1089. x.SetPrec(prec)
  1090. one.SetInt64(1)
  1091. for i := 0; i < n; i++ {
  1092. x.Add(&x, &one)
  1093. }
  1094. if x.Cmp(new(Float).SetInt64(n)) != 0 {
  1095. t.Errorf("prec = %d: got %s; want %d", prec, &x, n)
  1096. }
  1097. }
  1098. }
  1099. // Selected precisions with which to run various tests.
  1100. var precList = [...]uint{1, 2, 5, 8, 10, 16, 23, 24, 32, 50, 53, 64, 100, 128, 500, 511, 512, 513, 1000, 10000}
  1101. // Selected bits with which to run various tests.
  1102. // Each entry is a list of bits representing a floating-point number (see fromBits).
  1103. var bitsList = [...]Bits{
  1104. {}, // = 0
  1105. {0}, // = 1
  1106. {1}, // = 2
  1107. {-1}, // = 1/2
  1108. {10}, // = 2**10 == 1024
  1109. {-10}, // = 2**-10 == 1/1024
  1110. {100, 10, 1}, // = 2**100 + 2**10 + 2**1
  1111. {0, -1, -2, -10},
  1112. // TODO(gri) add more test cases
  1113. }
  1114. // TestFloatAdd tests Float.Add/Sub by comparing the result of a "manual"
  1115. // addition/subtraction of arguments represented by Bits values with the
  1116. // respective Float addition/subtraction for a variety of precisions
  1117. // and rounding modes.
  1118. func TestFloatAdd(t *testing.T) {
  1119. for _, xbits := range bitsList {
  1120. for _, ybits := range bitsList {
  1121. // exact values
  1122. x := xbits.Float()
  1123. y := ybits.Float()
  1124. zbits := xbits.add(ybits)
  1125. z := zbits.Float()
  1126. for i, mode := range [...]RoundingMode{ToZero, ToNearestEven, AwayFromZero} {
  1127. for _, prec := range precList {
  1128. got := new(Float).SetPrec(prec).SetMode(mode)
  1129. got.Add(x, y)
  1130. want := zbits.round(prec, mode)
  1131. if got.Cmp(want) != 0 {
  1132. t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t+ %s %v\n\t= %s\n\twant %s",
  1133. i, prec, mode, x, xbits, y, ybits, got, want)
  1134. }
  1135. got.Sub(z, x)
  1136. want = ybits.round(prec, mode)
  1137. if got.Cmp(want) != 0 {
  1138. t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t- %s %v\n\t= %s\n\twant %s",
  1139. i, prec, mode, z, zbits, x, xbits, got, want)
  1140. }
  1141. }
  1142. }
  1143. }
  1144. }
  1145. }
  1146. // TestFloatAddRoundZero tests Float.Add/Sub rounding when the result is exactly zero.
  1147. // x + (-x) or x - x for non-zero x should be +0 in all cases except when
  1148. // the rounding mode is ToNegativeInf in which case it should be -0.
  1149. func TestFloatAddRoundZero(t *testing.T) {
  1150. for _, mode := range [...]RoundingMode{ToNearestEven, ToNearestAway, ToZero, AwayFromZero, ToPositiveInf, ToNegativeInf} {
  1151. x := NewFloat(5.0)
  1152. y := new(Float).Neg(x)
  1153. want := NewFloat(0.0)
  1154. if mode == ToNegativeInf {
  1155. want.Neg(want)
  1156. }
  1157. got := new(Float).SetMode(mode)
  1158. got.Add(x, y)
  1159. if got.Cmp(want) != 0 || got.neg != (mode == ToNegativeInf) {
  1160. t.Errorf("%s:\n\t %v\n\t+ %v\n\t= %v\n\twant %v",
  1161. mode, x, y, got, want)
  1162. }
  1163. got.Sub(x, x)
  1164. if got.Cmp(want) != 0 || got.neg != (mode == ToNegativeInf) {
  1165. t.Errorf("%v:\n\t %v\n\t- %v\n\t= %v\n\twant %v",
  1166. mode, x, x, got, want)
  1167. }
  1168. }
  1169. }
  1170. // TestFloatAdd32 tests that Float.Add/Sub of numbers with
  1171. // 24bit mantissa behaves like float32 addition/subtraction
  1172. // (excluding denormal numbers).
  1173. func TestFloatAdd32(t *testing.T) {
  1174. // chose base such that we cross the mantissa precision limit
  1175. const base = 1<<26 - 0x10 // 11...110000 (26 bits)
  1176. for d := 0; d <= 0x10; d++ {
  1177. for i := range [2]int{} {
  1178. x0, y0 := float64(base), float64(d)
  1179. if i&1 != 0 {
  1180. x0, y0 = y0, x0
  1181. }
  1182. x := NewFloat(x0)
  1183. y := NewFloat(y0)
  1184. z := new(Float).SetPrec(24)
  1185. z.Add(x, y)
  1186. got, acc := z.Float32()
  1187. want := float32(y0) + float32(x0)
  1188. if got != want || acc != Exact {
  1189. t.Errorf("d = %d: %g + %g = %g (%s); want %g (Exact)", d, x0, y0, got, acc, want)
  1190. }
  1191. z.Sub(z, y)
  1192. got, acc = z.Float32()
  1193. want = float32(want) - float32(y0)
  1194. if got != want || acc != Exact {
  1195. t.Errorf("d = %d: %g - %g = %g (%s); want %g (Exact)", d, x0+y0, y0, got, acc, want)
  1196. }
  1197. }
  1198. }
  1199. }
  1200. // TestFloatAdd64 tests that Float.Add/Sub of numbers with
  1201. // 53bit mantissa behaves like float64 addition/subtraction.
  1202. func TestFloatAdd64(t *testing.T) {
  1203. // chose base such that we cross the mantissa precision limit
  1204. const base = 1<<55 - 0x10 // 11...110000 (55 bits)
  1205. for d := 0; d <= 0x10; d++ {
  1206. for i := range [2]int{} {
  1207. x0, y0 := float64(base), float64(d)
  1208. if i&1 != 0 {
  1209. x0, y0 = y0, x0
  1210. }
  1211. x := NewFloat(x0)
  1212. y := NewFloat(y0)
  1213. z := new(Float).SetPrec(53)
  1214. z.Add(x, y)
  1215. got, acc := z.Float64()
  1216. want := x0 + y0
  1217. if got != want || acc != Exact {
  1218. t.Errorf("d = %d: %g + %g = %g (%s); want %g (Exact)", d, x0, y0, got, acc, want)
  1219. }
  1220. z.Sub(z, y)
  1221. got, acc = z.Float64()
  1222. want -= y0
  1223. if got != want || acc != Exact {
  1224. t.Errorf("d = %d: %g - %g = %g (%s); want %g (Exact)", d, x0+y0, y0, got, acc, want)
  1225. }
  1226. }
  1227. }
  1228. }
  1229. func TestIssue20490(t *testing.T) {
  1230. var tests = []struct {
  1231. a, b float64
  1232. }{
  1233. {4, 1},
  1234. {-4, 1},
  1235. {4, -1},
  1236. {-4, -1},
  1237. }
  1238. for _, test := range tests {
  1239. a, b := NewFloat(test.a), NewFloat(test.b)
  1240. diff := new(Float).Sub(a, b)
  1241. b.Sub(a, b)
  1242. if b.Cmp(diff) != 0 {
  1243. t.Errorf("got %g - %g = %g; want %g\n", a, NewFloat(test.b), b, diff)
  1244. }
  1245. b = NewFloat(test.b)
  1246. sum := new(Float).Add(a, b)
  1247. b.Add(a, b)
  1248. if b.Cmp(sum) != 0 {
  1249. t.Errorf("got %g + %g = %g; want %g\n", a, NewFloat(test.b), b, sum)
  1250. }
  1251. }
  1252. }
  1253. // TestFloatMul tests Float.Mul/Quo by comparing the result of a "manual"
  1254. // multiplication/division of arguments represented by Bits values with the
  1255. // respective Float multiplication/division for a variety of precisions
  1256. // and rounding modes.
  1257. func TestFloatMul(t *testing.T) {
  1258. for _, xbits := range bitsList {
  1259. for _, ybits := range bitsList {
  1260. // exact values
  1261. x := xbits.Float()
  1262. y := ybits.Float()
  1263. zbits := xbits.mul(ybits)
  1264. z := zbits.Float()
  1265. for i, mode := range [...]RoundingMode{ToZero, ToNearestEven, AwayFromZero} {
  1266. for _, prec := range precList {
  1267. got := new(Float).SetPrec(prec).SetMode(mode)
  1268. got.Mul(x, y)
  1269. want := zbits.round(prec, mode)
  1270. if got.Cmp(want) != 0 {
  1271. t.Errorf("i = %d, prec = %d, %s:\n\t %v %v\n\t* %v %v\n\t= %v\n\twant %v",
  1272. i, prec, mode, x, xbits, y, ybits, got, want)
  1273. }
  1274. if x.Sign() == 0 {
  1275. continue // ignore div-0 case (not invertable)
  1276. }
  1277. got.Quo(z, x)
  1278. want = ybits.round(prec, mode)
  1279. if got.Cmp(want) != 0 {
  1280. t.Errorf("i = %d, prec = %d, %s:\n\t %v %v\n\t/ %v %v\n\t= %v\n\twant %v",
  1281. i, prec, mode, z, zbits, x, xbits, got, want)
  1282. }
  1283. }
  1284. }
  1285. }
  1286. }
  1287. }
  1288. // TestFloatMul64 tests that Float.Mul/Quo of numbers with
  1289. // 53bit mantissa behaves like float64 multiplication/division.
  1290. func TestFloatMul64(t *testing.T) {
  1291. for _, test := range []struct {
  1292. x, y float64
  1293. }{
  1294. {0, 0},
  1295. {0, 1},
  1296. {1, 1},
  1297. {1, 1.5},
  1298. {1.234, 0.5678},
  1299. {2.718281828, 3.14159265358979},
  1300. {2.718281828e10, 3.14159265358979e-32},
  1301. {1.0 / 3, 1e200},
  1302. } {
  1303. for i := range [8]int{} {
  1304. x0, y0 := test.x, test.y
  1305. if i&1 != 0 {
  1306. x0 = -x0
  1307. }
  1308. if i&2 != 0 {
  1309. y0 = -y0
  1310. }
  1311. if i&4 != 0 {
  1312. x0, y0 = y0, x0
  1313. }
  1314. x := NewFloat(x0)
  1315. y := NewFloat(y0)
  1316. z := new(Float).SetPrec(53)
  1317. z.Mul(x, y)
  1318. got, _ := z.Float64()
  1319. want := x0 * y0
  1320. if got != want {
  1321. t.Errorf("%g * %g = %g; want %g", x0, y0, got, want)
  1322. }
  1323. if y0 == 0 {
  1324. continue // avoid division-by-zero
  1325. }
  1326. z.Quo(z, y)
  1327. got, _ = z.Float64()
  1328. want /= y0
  1329. if got != want {
  1330. t.Errorf("%g / %g = %g; want %g", x0*y0, y0, got, want)
  1331. }
  1332. }
  1333. }
  1334. }
  1335. func TestIssue6866(t *testing.T) {
  1336. for _, prec := range precList {
  1337. two := new(Float).SetPrec(prec).SetInt64(2)
  1338. one := new(Float).SetPrec(prec).SetInt64(1)
  1339. three := new(Float).SetPrec(prec).SetInt64(3)
  1340. msix := new(Float).SetPrec(prec).SetInt64(-6)
  1341. psix := new(Float).SetPrec(prec).SetInt64(+6)
  1342. p := new(Float).SetPrec(prec)
  1343. z1 := new(Float).SetPrec(prec)
  1344. z2 := new(Float).SetPrec(prec)
  1345. // z1 = 2 + 1.0/3*-6
  1346. p.Quo(one, three)
  1347. p.Mul(p, msix)
  1348. z1.Add(two, p)
  1349. // z2 = 2 - 1.0/3*+6
  1350. p.Quo(one, three)
  1351. p.Mul(p, psix)
  1352. z2.Sub(two, p)
  1353. if z1.Cmp(z2) != 0 {
  1354. t.Fatalf("prec %d: got z1 = %v != z2 = %v; want z1 == z2\n", prec, z1, z2)
  1355. }
  1356. if z1.Sign() != 0 {
  1357. t.Errorf("prec %d: got z1 = %v; want 0", prec, z1)
  1358. }
  1359. if z2.Sign() != 0 {
  1360. t.Errorf("prec %d: got z2 = %v; want 0", prec, z2)
  1361. }
  1362. }
  1363. }
  1364. func TestFloatQuo(t *testing.T) {
  1365. // TODO(gri) make the test vary these precisions
  1366. preci := 200 // precision of integer part
  1367. precf := 20 // precision of fractional part
  1368. for i := 0; i < 8; i++ {
  1369. // compute accurate (not rounded) result z
  1370. bits := Bits{preci - 1}
  1371. if i&3 != 0 {
  1372. bits = append(bits, 0)
  1373. }
  1374. if i&2 != 0 {
  1375. bits = append(bits, -1)
  1376. }
  1377. if i&1 != 0 {
  1378. bits = append(bits, -precf)
  1379. }
  1380. z := bits.Float()
  1381. // compute accurate x as z*y
  1382. y := NewFloat(3.14159265358979323e123)
  1383. x := new(Float).SetPrec(z.Prec() + y.Prec()).SetMode(ToZero)
  1384. x.Mul(z, y)
  1385. // leave for debugging
  1386. // fmt.Printf("x = %s\ny = %s\nz = %s\n", x, y, z)
  1387. if got := x.Acc(); got != Exact {
  1388. t.Errorf("got acc = %s; want exact", got)
  1389. }
  1390. // round accurate z for a variety of precisions and
  1391. // modes and compare against result of x / y.
  1392. for _, mode := range [...]RoundingMode{ToZero, ToNearestEven, AwayFromZero} {
  1393. for d := -5; d < 5; d++ {
  1394. prec := uint(preci + d)
  1395. got := new(Float).SetPrec(prec).SetMode(mode).Quo(x, y)
  1396. want := bits.round(prec, mode)
  1397. if got.Cmp(want) != 0 {
  1398. t.Errorf("i = %d, prec = %d, %s:\n\t %s\n\t/ %s\n\t= %s\n\twant %s",
  1399. i, prec, mode, x, y, got, want)
  1400. }
  1401. }
  1402. }
  1403. }
  1404. }
  1405. var long = flag.Bool("long", false, "run very long tests")
  1406. // TestFloatQuoSmoke tests all divisions x/y for values x, y in the range [-n, +n];
  1407. // it serves as a smoke test for basic correctness of division.
  1408. func TestFloatQuoSmoke(t *testing.T) {
  1409. n := 10
  1410. if *long {
  1411. n = 1000
  1412. }
  1413. const dprec = 3 // max. precision variation
  1414. const prec = 10 + dprec // enough bits to hold n precisely
  1415. for x := -n; x <= n; x++ {
  1416. for y := -n; y < n; y++ {
  1417. if y == 0 {
  1418. continue
  1419. }
  1420. a := float64(x)
  1421. b := float64(y)
  1422. c := a / b
  1423. // vary operand precision (only ok as long as a, b can be represented correctly)
  1424. for ad := -dprec; ad <= dprec; ad++ {
  1425. for bd := -dprec; bd <= dprec; bd++ {
  1426. A := new(Float).SetPrec(uint(prec + ad)).SetFloat64(a)
  1427. B := new(Float).SetPrec(uint(prec + bd)).SetFloat64(b)
  1428. C := new(Float).SetPrec(53).Quo(A, B) // C has float64 mantissa width
  1429. cc, acc := C.Float64()
  1430. if cc != c {
  1431. t.Errorf("%g/%g = %s; want %.5g\n", a, b, C.Text('g', 5), c)
  1432. continue
  1433. }
  1434. if acc != Exact {
  1435. t.Errorf("%g/%g got %s result; want exact result", a, b, acc)
  1436. }
  1437. }
  1438. }
  1439. }
  1440. }
  1441. }
  1442. // TestFloatArithmeticSpecialValues tests that Float operations produce the
  1443. // correct results for combinations of zero (±0), finite (±1 and ±2.71828),
  1444. // and infinite (±Inf) operands.
  1445. func TestFloatArithmeticSpecialValues(t *testing.T) {
  1446. zero := 0.0
  1447. args := []float64{math.Inf(-1), -2.71828, -1, -zero, zero, 1, 2.71828, math.Inf(1)}
  1448. xx := new(Float)
  1449. yy := new(Float)
  1450. got := new(Float)
  1451. want := new(Float)
  1452. for i := 0; i < 4; i++ {
  1453. for _, x := range args {
  1454. xx.SetFloat64(x)
  1455. // check conversion is correct
  1456. // (no need to do this for y, since we see exactly the
  1457. // same values there)
  1458. if got, acc := xx.Float64(); got != x || acc != Exact {
  1459. t.Errorf("Float(%g) == %g (%s)", x, got, acc)
  1460. }
  1461. for _, y := range args {
  1462. yy.SetFloat64(y)
  1463. var (
  1464. op string
  1465. z float64
  1466. f func(z, x, y *Float) *Float
  1467. )
  1468. switch i {
  1469. case 0:
  1470. op = "+"
  1471. z = x + y
  1472. f = (*Float).Add
  1473. case 1:
  1474. op = "-"
  1475. z = x - y
  1476. f = (*Float).Sub
  1477. case 2:
  1478. op = "*"
  1479. z = x * y
  1480. f = (*Float).Mul
  1481. case 3:
  1482. op = "/"
  1483. z = x / y
  1484. f = (*Float).Quo
  1485. default:
  1486. panic("unreachable")
  1487. }
  1488. var errnan bool // set if execution of f panicked with ErrNaN
  1489. // protect execution of f
  1490. func() {
  1491. defer func() {
  1492. if p := recover(); p != nil {
  1493. _ = p.(ErrNaN) // re-panic if not ErrNaN
  1494. errnan = true
  1495. }
  1496. }()
  1497. f(got, xx, yy)
  1498. }()
  1499. if math.IsNaN(z) {
  1500. if !errnan {
  1501. t.Errorf("%5g %s %5g = %5s; want ErrNaN panic", x, op, y, got)
  1502. }
  1503. continue
  1504. }
  1505. if errnan {
  1506. t.Errorf("%5g %s %5g panicked with ErrNan; want %5s", x, op, y, want)
  1507. continue
  1508. }
  1509. want.SetFloat64(z)
  1510. if !alike(got, want) {
  1511. t.Errorf("%5g %s %5g = %5s; want %5s", x, op, y, got, want)
  1512. }
  1513. }
  1514. }
  1515. }
  1516. }
  1517. func TestFloatArithmeticOverflow(t *testing.T) {
  1518. for _, test := range []struct {
  1519. prec uint
  1520. mode RoundingMode
  1521. op byte
  1522. x, y, want string
  1523. acc Accuracy
  1524. }{
  1525. {4, ToNearestEven, '+', "0", "0", "0", Exact}, // smoke test
  1526. {4, ToNearestEven, '+', "0x.8p+0", "0x.8p+0", "0x.8p+1", Exact}, // smoke test
  1527. {4, ToNearestEven, '+', "0", "0x.8p2147483647", "0x.8p+2147483647", Exact},
  1528. {4, ToNearestEven, '+', "0x.8p2147483500", "0x.8p2147483647", "0x.8p+2147483647", Below}, // rounded to zero
  1529. {4, ToNearestEven, '+', "0x.8p2147483647", "0x.8p2147483647", "+Inf", Above}, // exponent overflow in +
  1530. {4, ToNearestEven, '+', "-0x.8p2147483647", "-0x.8p2147483647", "-Inf", Below}, // exponent overflow in +
  1531. {4, ToNearestEven, '-', "-0x.8p2147483647", "0x.8p2147483647", "-Inf", Below}, // exponent overflow in -
  1532. {4, ToZero, '+', "0x.fp2147483647", "0x.8p2147483643", "0x.fp+2147483647", Below}, // rounded to zero
  1533. {4, ToNearestEven, '+', "0x.fp2147483647", "0x.8p2147483643", "+Inf", Above}, // exponent overflow in rounding
  1534. {4, AwayFromZero, '+', "0x.fp2147483647", "0x.8p2147483643", "+Inf", Above}, // exponent overflow in rounding
  1535. {4, AwayFromZero, '-', "-0x.fp2147483647", "0x.8p2147483644", "-Inf", Below}, // exponent overflow in rounding
  1536. {4, ToNearestEven, '-', "-0x.fp2147483647", "0x.8p2147483643", "-Inf", Below}, // exponent overflow in rounding
  1537. {4, ToZero, '-', "-0x.fp2147483647", "0x.8p2147483643", "-0x.fp+2147483647", Above}, // rounded to zero
  1538. {4, ToNearestEven, '+', "0", "0x.8p-2147483648", "0x.8p-2147483648", Exact},
  1539. {4, ToNearestEven, '+', "0x.8p-2147483648", "0x.8p-2147483648", "0x.8p-2147483647", Exact},
  1540. {4, ToNearestEven, '*', "1", "0x.8p2147483647", "0x.8p+2147483647", Exact},
  1541. {4, ToNearestEven, '*', "2", "0x.8p2147483647", "+Inf", Above}, // exponent overflow in *
  1542. {4, ToNearestEven, '*', "-2", "0x.8p2147483647", "-Inf", Below}, // exponent overflow in *
  1543. {4, ToNearestEven, '/', "0.5", "0x.8p2147483647", "0x.8p-2147483646", Exact},
  1544. {4, ToNearestEven, '/', "0x.8p+0", "0x.8p2147483647", "0x.8p-2147483646", Exact},
  1545. {4, ToNearestEven, '/', "0x.8p-1", "0x.8p2147483647", "0x.8p-2147483647", Exact},
  1546. {4, ToNearestEven, '/', "0x.8p-2", "0x.8p2147483647", "0x.8p-2147483648", Exact},
  1547. {4, ToNearestEven, '/', "0x.8p-3", "0x.8p2147483647", "0", Below}, // exponent underflow in /
  1548. } {
  1549. x := makeFloat(test.x)
  1550. y := makeFloat(test.y)
  1551. z := new(Float).SetPrec(test.prec).SetMode(test.mode)
  1552. switch test.op {
  1553. case '+':
  1554. z.Add(x, y)
  1555. case '-':
  1556. z.Sub(x, y)
  1557. case '*':
  1558. z.Mul(x, y)
  1559. case '/':
  1560. z.Quo(x, y)
  1561. default:
  1562. panic("unreachable")
  1563. }
  1564. if got := z.Text('p', 0); got != test.want || z.Acc() != test.acc {
  1565. t.Errorf(
  1566. "prec = %d (%s): %s %c %s = %s (%s); want %s (%s)",
  1567. test.prec, test.mode, x.Text('p', 0), test.op, y.Text('p', 0), got, z.Acc(), test.want, test.acc,
  1568. )
  1569. }
  1570. }
  1571. }
  1572. // TODO(gri) Add tests that check correctness in the presence of aliasing.
  1573. // For rounding modes ToNegativeInf and ToPositiveInf, rounding is affected
  1574. // by the sign of the value to be rounded. Test that rounding happens after
  1575. // the sign of a result has been set.
  1576. // This test uses specific values that are known to fail if rounding is
  1577. // "factored" out before setting the result sign.
  1578. func TestFloatArithmeticRounding(t *testing.T) {
  1579. for _, test := range []struct {
  1580. mode RoundingMode
  1581. prec uint
  1582. x, y, want int64
  1583. op byte
  1584. }{
  1585. {ToZero, 3, -0x8, -0x1, -0x8, '+'},
  1586. {AwayFromZero, 3, -0x8, -0x1, -0xa, '+'},
  1587. {ToNegativeInf, 3, -0x8, -0x1, -0xa, '+'},
  1588. {ToZero, 3, -0x8, 0x1, -0x8, '-'},
  1589. {AwayFromZero, 3, -0x8, 0x1, -0xa, '-'},
  1590. {ToNegativeInf, 3, -0x8, 0x1, -0xa, '-'},
  1591. {ToZero, 3, -0x9, 0x1, -0x8, '*'},
  1592. {AwayFromZero, 3, -0x9, 0x1, -0xa, '*'},
  1593. {ToNegativeInf, 3, -0x9, 0x1, -0xa, '*'},
  1594. {ToZero, 3, -0x9, 0x1, -0x8, '/'},
  1595. {AwayFromZero, 3, -0x9, 0x1, -0xa, '/'},
  1596. {ToNegativeInf, 3, -0x9, 0x1, -0xa, '/'},
  1597. } {
  1598. var x, y, z Float
  1599. x.SetInt64(test.x)
  1600. y.SetInt64(test.y)
  1601. z.SetPrec(test.prec).SetMode(test.mode)
  1602. switch test.op {
  1603. case '+':
  1604. z.Add(&x, &y)
  1605. case '-':
  1606. z.Sub(&x, &y)
  1607. case '*':
  1608. z.Mul(&x, &y)
  1609. case '/':
  1610. z.Quo(&x, &y)
  1611. default:
  1612. panic("unreachable")
  1613. }
  1614. if got, acc := z.Int64(); got != test.want || acc != Exact {
  1615. t.Errorf("%s, %d bits: %d %c %d = %d (%s); want %d (Exact)",
  1616. test.mode, test.prec, test.x, test.op, test.y, got, acc, test.want,
  1617. )
  1618. }
  1619. }
  1620. }
  1621. // TestFloatCmpSpecialValues tests that Cmp produces the correct results for
  1622. // combinations of zero (±0), finite (±1 and ±2.71828), and infinite (±Inf)
  1623. // operands.
  1624. func TestFloatCmpSpecialValues(t *testing.T) {
  1625. zero := 0.0
  1626. args := []float64{math.Inf(-1), -2.71828, -1, -zero, zero, 1, 2.71828, math.Inf(1)}
  1627. xx := new(Float)
  1628. yy := new(Float)
  1629. for i := 0; i < 4; i++ {
  1630. for _, x := range args {
  1631. xx.SetFloat64(x)
  1632. // check conversion is correct
  1633. // (no need to do this for y, since we see exactly the
  1634. // same values there)
  1635. if got, acc := xx.Float64(); got != x || acc != Exact {
  1636. t.Errorf("Float(%g) == %g (%s)", x, got, acc)
  1637. }
  1638. for _, y := range args {
  1639. yy.SetFloat64(y)
  1640. got := xx.Cmp(yy)
  1641. want := 0
  1642. switch {
  1643. case x < y:
  1644. want = -1
  1645. case x > y:
  1646. want = +1
  1647. }
  1648. if got != want {
  1649. t.Errorf("(%g).Cmp(%g) = %v; want %v", x, y, got, want)
  1650. }
  1651. }
  1652. }
  1653. }
  1654. }
  1655. func BenchmarkFloatAdd(b *testing.B) {
  1656. x := new(Float)
  1657. y := new(Float)
  1658. z := new(Float)
  1659. for _, prec := range []uint{10, 1e2, 1e3, 1e4, 1e5} {
  1660. x.SetPrec(prec).SetRat(NewRat(1, 3))
  1661. y.SetPrec(prec).SetRat(NewRat(1, 6))
  1662. z.SetPrec(prec)
  1663. b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
  1664. b.ReportAllocs()
  1665. for i := 0; i < b.N; i++ {
  1666. z.Add(x, y)
  1667. }
  1668. })
  1669. }
  1670. }
  1671. func BenchmarkFloatSub(b *testing.B) {
  1672. x := new(Float)
  1673. y := new(Float)
  1674. z := new(Float)
  1675. for _, prec := range []uint{10, 1e2, 1e3, 1e4, 1e5} {
  1676. x.SetPrec(prec).SetRat(NewRat(1, 3))
  1677. y.SetPrec(prec).SetRat(NewRat(1, 6))
  1678. z.SetPrec(prec)
  1679. b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
  1680. b.ReportAllocs()
  1681. for i := 0; i < b.N; i++ {
  1682. z.Sub(x, y)
  1683. }
  1684. })
  1685. }
  1686. }