Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Microservices for performance - GOT... by Peter Lawrey 211 views
- Open HFT libraries in @Java by Peter Lawrey 3015 views
- GC free coding in @Java presented @... by Peter Lawrey 6218 views
- Introduction to chronicle (low late... by Peter Lawrey 5226 views
- Chronicle software - DEC - 20170131 by Peter Lawrey 127 views
- Advanced off heap ipc by Peter Lawrey 2871 views

1,935 views

Published on

No Downloads

Total views

1,935

On SlideShare

0

From Embeds

0

Number of Embeds

1

Shares

0

Downloads

44

Comments

0

Likes

3

No embeds

No notes for slide

- 1. (c) Higher Frequency Trading Using BigDecimal and double Using BigDecimal and double (Or why you can use double for money) Higher Frequency Trading (c) Peter Lawrey
- 2. (c) Higher Frequency Trading Nearest representable value Floating point numbers are represented using a sum of powers of two e.g. 9.75 = 8 + 1 + 0.5 + 0.25 Values like 0.1 cannot be exactly represented using floating point but when you print it, it “knows” to print the simplest number which would be represented at this value.
- 3. (c) Higher Frequency Trading Rounding a double Why do we get errors with double – Representation errors, • 0.1 is not exactly 0.1 • new BigDecimal(0.1) – Using BigDecimal.valueOf(0.1) instead. – Rounding errors, • each calculation gives the closest match
- 4. (c) Higher Frequency Trading Rounding a double Rounding errors are not random errors. You can predict that the error will be less than 1 ulp. If you display a number with such as error it will have a sane toString() However, if you perform a calculation on a value which cannot be exactly represented, you will see the error.
- 5. (c) Higher Frequency Trading Rounding a double Ways to round to significant digits public static double round2(double d) { return Math.round(d * 1e2) / 1e2; }
- 6. (c) Higher Frequency Trading OpenHFT Java-Lang /** * Performs a round which is accurate to within 1 ulp. * i.e. for values very close to 0.5 it might be rounded up or down. * This is a pragmatic choice for performance reasons as it is assumed you are not working on the edge of the precision of double. */ public static double round2(double d) { final double factor = 1e2; return d > WHOLE_NUMBER || d < -WHOLE_NUMBER ? (long) (d < 0 ? d * factor - 0.5 : d * factor + 0.5) / factor : d; }
- 7. (c) Higher Frequency Trading Errors and double Exercise 10 mins Use BigDecimal to print the actual value of 0.1 Print the values of x for for(double d = 0.0; d != 1.0; d += 0.1) Print the values of x for for(int x = 0; x != 10; x++) { double d = x * 0.1; Print the values of x for for(int x = 0; x != 10; x++) { double d = x / 10.0;
- 8. (c) Higher Frequency Trading BigDecimal or double Exercise 20 mins. Write a loop to take the mid price of 1000 bid and ask values as BigDecimal and double. Use randomly generated prices. – What sort of distribution should your use? Test the mid values are the same. How long does each take? Why?
- 9. (c) Higher Frequency Trading rounding double Exercise 20 mins. Write a function for rounding a double – Using Math.round – Using / of whole numbers Compare the result with BigDecimal. Can this be written without Math.round? Is this any faster?
- 10. (c) Higher Frequency Trading Caching BigDecimal Exercise 30 mins. Write a function to cache BigDecimals created from a double. public BigDecimal intern(double d) Compare the performance with BigDecimal.valueOf() for values 0.01 to 1.00 Is this any faster?

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment