Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. * when creating a big float, there are two different constructors. The biggest difference I could see what the default precision for each:
  2. * `new(big.Float)` has a default precision of 0
  3. * `big.NewFloat` has a default precision of 53
  4. * this gets interesting when setting a either of these to a large number:
  5. * `new(big.Float)` seems to dynamically change the default precision to whatever is needed
  6. ```
  7. b := new(big.Float)
  8. b.SetUint64(16629421200200000000)
  9. //yields
  10. <*big.Float | 0xc0003ece10>: {
  11. prec: 64,
  12. mode: 0,
  13. acc: 0,
  14. form: 1,
  15. neg: false,
  16. mant: [16629421200200000000],
  17. exp: 64,
  18. }
  19.  
  20. * `big.NewFloat` doesn’t seem to change the precision (unless done so explicitly)
  21. ```
  22. b := big.NewFloat(0.0)
  23. b.SetUint64(16629421200200000000)
  24. // yields
  25. <*big.Float | 0xc0003ece40>: {
  26. prec: 53,
  27. mode: 0,
  28. acc: -1,
  29. form: 1,
  30. neg: false,
  31. mant: [16629421200199999488],
  32. exp: 64,
  33. }
  34. ```
  35. * the `acc` (accuracy) attribute is -1, which represents `Below`
  36. * the `mant` attribute (the mantissa) is the value of the this float, which is less than the expected value
  37. * if we set the precision to 64 before calling `SetUint64` then we get the correct value
  38. ```
  39. b := big.NewFloat(0.0)
  40. b.SetPrec(64)
  41. b.SetUint64(16629421200200000000)
  42. // yields
  43. <*big.Float | 0xc000354090>: {
  44. prec: 64,
  45. mode: 0,
  46. acc: 0,
  47. form: 1,
  48. neg: false,
  49. mant: [16629421200200000000],
  50. exp: 64,
  51. }
  52. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement