floatexample_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2015 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_test
  5. import (
  6. "fmt"
  7. "math"
  8. "math/big"
  9. )
  10. func ExampleFloat_Add() {
  11. // Operate on numbers of different precision.
  12. var x, y, z big.Float
  13. x.SetInt64(1000) // x is automatically set to 64bit precision
  14. y.SetFloat64(2.718281828) // y is automatically set to 53bit precision
  15. z.SetPrec(32)
  16. z.Add(&x, &y)
  17. fmt.Printf("x = %.10g (%s, prec = %d, acc = %s)\n", &x, x.Text('p', 0), x.Prec(), x.Acc())
  18. fmt.Printf("y = %.10g (%s, prec = %d, acc = %s)\n", &y, y.Text('p', 0), y.Prec(), y.Acc())
  19. fmt.Printf("z = %.10g (%s, prec = %d, acc = %s)\n", &z, z.Text('p', 0), z.Prec(), z.Acc())
  20. // Output:
  21. // x = 1000 (0x.fap+10, prec = 64, acc = Exact)
  22. // y = 2.718281828 (0x.adf85458248cd8p+2, prec = 53, acc = Exact)
  23. // z = 1002.718282 (0x.faadf854p+10, prec = 32, acc = Below)
  24. }
  25. func ExampleFloat_shift() {
  26. // Implement Float "shift" by modifying the (binary) exponents directly.
  27. for s := -5; s <= 5; s++ {
  28. x := big.NewFloat(0.5)
  29. x.SetMantExp(x, x.MantExp(nil)+s) // shift x by s
  30. fmt.Println(x)
  31. }
  32. // Output:
  33. // 0.015625
  34. // 0.03125
  35. // 0.0625
  36. // 0.125
  37. // 0.25
  38. // 0.5
  39. // 1
  40. // 2
  41. // 4
  42. // 8
  43. // 16
  44. }
  45. func ExampleFloat_Cmp() {
  46. inf := math.Inf(1)
  47. zero := 0.0
  48. operands := []float64{-inf, -1.2, -zero, 0, +1.2, +inf}
  49. fmt.Println(" x y cmp")
  50. fmt.Println("---------------")
  51. for _, x64 := range operands {
  52. x := big.NewFloat(x64)
  53. for _, y64 := range operands {
  54. y := big.NewFloat(y64)
  55. fmt.Printf("%4g %4g %3d\n", x, y, x.Cmp(y))
  56. }
  57. fmt.Println()
  58. }
  59. // Output:
  60. // x y cmp
  61. // ---------------
  62. // -Inf -Inf 0
  63. // -Inf -1.2 -1
  64. // -Inf -0 -1
  65. // -Inf 0 -1
  66. // -Inf 1.2 -1
  67. // -Inf +Inf -1
  68. //
  69. // -1.2 -Inf 1
  70. // -1.2 -1.2 0
  71. // -1.2 -0 -1
  72. // -1.2 0 -1
  73. // -1.2 1.2 -1
  74. // -1.2 +Inf -1
  75. //
  76. // -0 -Inf 1
  77. // -0 -1.2 1
  78. // -0 -0 0
  79. // -0 0 0
  80. // -0 1.2 -1
  81. // -0 +Inf -1
  82. //
  83. // 0 -Inf 1
  84. // 0 -1.2 1
  85. // 0 -0 0
  86. // 0 0 0
  87. // 0 1.2 -1
  88. // 0 +Inf -1
  89. //
  90. // 1.2 -Inf 1
  91. // 1.2 -1.2 1
  92. // 1.2 -0 1
  93. // 1.2 0 1
  94. // 1.2 1.2 0
  95. // 1.2 +Inf -1
  96. //
  97. // +Inf -Inf 1
  98. // +Inf -1.2 1
  99. // +Inf -0 1
  100. // +Inf 0 1
  101. // +Inf 1.2 1
  102. // +Inf +Inf 0
  103. }
  104. func ExampleRoundingMode() {
  105. operands := []float64{2.6, 2.5, 2.1, -2.1, -2.5, -2.6}
  106. fmt.Print(" x")
  107. for mode := big.ToNearestEven; mode <= big.ToPositiveInf; mode++ {
  108. fmt.Printf(" %s", mode)
  109. }
  110. fmt.Println()
  111. for _, f64 := range operands {
  112. fmt.Printf("%4g", f64)
  113. for mode := big.ToNearestEven; mode <= big.ToPositiveInf; mode++ {
  114. // sample operands above require 2 bits to represent mantissa
  115. // set binary precision to 2 to round them to integer values
  116. f := new(big.Float).SetPrec(2).SetMode(mode).SetFloat64(f64)
  117. fmt.Printf(" %*g", len(mode.String()), f)
  118. }
  119. fmt.Println()
  120. }
  121. // Output:
  122. // x ToNearestEven ToNearestAway ToZero AwayFromZero ToNegativeInf ToPositiveInf
  123. // 2.6 3 3 2 3 2 3
  124. // 2.5 2 3 2 3 2 3
  125. // 2.1 2 2 2 3 2 3
  126. // -2.1 -2 -2 -2 -3 -3 -2
  127. // -2.5 -2 -3 -2 -3 -3 -2
  128. // -2.6 -3 -3 -2 -3 -3 -2
  129. }