Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go for Decimals (Josh Carpeggiani 2019)

Go for Decimals (Josh Carpeggiani 2019)

Avatar for GopherConAU

GopherConAU

November 01, 2019
Tweet

More Decks by GopherConAU

Other Decks in Programming

Transcript

  1. Go for Decimals Go for Decimals Joshua Carpeggiani Joshua Carpeggiani

    Software Engineer, ANZ Software Engineer, ANZ
  2. Full Slides Full Slides Full slides available at Full slides

    available at gophercon.joshcarp.com/gophercon.slide gophercon.joshcarp.com/gophercon.slide (http://gophercon.joshcarp.com/gophercon.slide) (http://gophercon.joshcarp.com/gophercon.slide) 2 2
  3. 3 3

  4. Binary Binary Bin -> Dec Bin -> Dec 0 =

    0 0 = 0 1 = 1 1 = 1 10 = 2 10 = 2 100 = 4 100 = 4 12 12
  5. Binary Binary Bin -> Dec Bin -> Dec 1 =

    1 1 = 1 0.1 = 0.5 0.1 = 0.5 0.01 = 0.25 0.01 = 0.25 0.001 = 0.125 0.001 = 0.125 16 16
  6. Binary floating point numbers Binary floating point numbers 0 0

    2 2 . . 1 1 2 2 ×2 ×2 = 0.5 = 0.5 0 0 −1 −1 1.0 1.0 significand significand -1 -1 exponent exponent 17 17
  7. Binary Binary Bin -> Dec Bin -> Dec 0.0001 =

    0.0625 0.0001 = 0.0625 18 18
  8. Binary Binary Bin -> Dec Bin -> Dec 0.00011 =

    0.09375 0.00011 = 0.09375 19 19
  9. Binary Binary Bin -> Dec Bin -> Dec 0.000110011 =

    0.099609375 0.000110011 = 0.099609375 20 20
  10. Binary Binary Bin -> Dec Bin -> Dec 0.00011001101 =

    0.10009765625 0.00011001101 = 0.10009765625 21 21
  11. Another Decimal library Another Decimal library www.github.com/anz-bank/decimal www.github.com/anz-bank/decimal (http://www.github.com/anz-bank/decimal) (http://www.github.com/anz-bank/decimal)

    Project Goal: Implement a 64 & 128 bit decimal floating point library Project Goal: Implement a 64 & 128 bit decimal floating point library Conform as closely to IEEE 754-2008 standard as practical Conform as closely to IEEE 754-2008 standard as practical 23 23
  12. Scientific notation Scientific notation 0 0 10 10 . .

    1 1 10 10 1 1 -1 -1 1.0 1.0 25 25
  13. Scientific notation Scientific notation 0 0 10 10 . .

    1 1 10 10 ×10 ×10 1 1 -1 -1 1.0 1.0 -1 -1 26 26
  14. Scientific notation Scientific notation ×10 ×10 + + ×10 ×10

    = = ×10 ×10 1.0 1.0 -1 -1 2.0 2.0 -1 -1 3.0 3.0 -1 -1 29 29
  15. The End? The End? Use decimals for everything? Use decimals

    for everything? Why doesn't everything use this? Why doesn't everything use this? 40 40
  16. The End? The End? Use decimals for everything? Use decimals

    for everything? Why doesn't everything use this? Why doesn't everything use this? Are decimals even that efficient? Are decimals even that efficient? 41 41
  17. Decimal decoding Decimal decoding 9,999 + 1 9,999 + 1

    = 10,000 = 10,000 = = x x 10,000 10,000 1,000 1,000 10 10 ? ? 44 44
  18. Decimal decoding Decimal decoding 9,999 + 1 9,999 + 1

    = 10,000 = 10,000 = = x x x x 10,000 10,000 1,000 1,000 10 10 ? ? 1,000 1,000 10 10 1 1 45 45
  19. Decimal decoding Decimal decoding ÷ ÷ ÷ ÷ 10,000 dec

    10,000 dec 10 dec 10 dec 10011100010000 10011100010000 1010 1010 47 47
  20. Decimal decoding Decimal decoding ÷ ÷ ÷ ÷ some really

    long integer division here some really long integer division here 10,000 dec 10,000 dec 10 dec 10 dec 10011100010000 10011100010000 1010 1010 48 48
  21. Decimal decoding Decimal decoding ÷ ÷ ÷ ÷ some really

    long integer division here some really long integer division here = = = 1,000 (dec) = 1,000 (dec) 10,000 dec 10,000 dec 10 dec 10 dec 10011100010000 10011100010000 1010 1010 1111101000 1111101000 49 49
  22. Binary decoding Binary decoding >> 4 >> 4 x x

    10011100010000 10011100010000 1001110001 1001110001 2 2 4 4 52 52
  23. Conclusion Conclusion Use floats if you really need performance and

    not precision Use floats if you really need performance and not precision Use ints as your smallest unit if you need precision and performance Use ints as your smallest unit if you need precision and performance Use decimals for everything else Use decimals for everything else Come contribute Come contribute github.com/anz-bank/decimal github.com/anz-bank/decimal 67 67
  24. Looking forward Looking forward More optimisation More optimisation Implement 128

    bit Implement 128 bit Assembler optimisations Assembler optimisations 68 68