SlideShare a Scribd company logo
(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
(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.
(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
(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.
(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;
}
(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;
}
(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;
(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?
(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?
(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?

More Related Content

PPTX
Decision Tree.pptx
PDF
Decision Tree Algorithm | Decision Tree in Python | Machine Learning Algorith...
PDF
KNN Algorithm using Python | How KNN Algorithm works | Python Data Science Tr...
PPT
Topic Models - LDA and Correlated Topic Models
PDF
Data Exploration and Visualization with R
PPT
3.7 outlier analysis
PPTX
Microservices for performance - GOTO Chicago 2016
PPT
Open HFT libraries in @Java
Decision Tree.pptx
Decision Tree Algorithm | Decision Tree in Python | Machine Learning Algorith...
KNN Algorithm using Python | How KNN Algorithm works | Python Data Science Tr...
Topic Models - LDA and Correlated Topic Models
Data Exploration and Visualization with R
3.7 outlier analysis
Microservices for performance - GOTO Chicago 2016
Open HFT libraries in @Java

Viewers also liked (20)

PPT
GC free coding in @Java presented @Geecon
PPT
Introduction to chronicle (low latency persistence)
PPT
Advanced off heap ipc
PPT
High Frequency Trading and NoSQL database
PDF
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
ODP
Writing and testing high frequency trading engines in java
PPT
Introduction to OpenHFT for Melbourne Java Users Group
PPT
Thread Safe Interprocess Shared Memory in Java (in 7 mins)
PDF
7.[54 59]the determinants of leverage of the listed-textile companies in india
PPTX
Aplicaciones Básicas de Ubuntu
PPT
Evolucion De La Comunicaion
PDF
Unhealthy Developing World Food Markets
PPTX
Aplicaciones basicas de unbuntu 14.02 LTE
PPT
Ruben Licera's Social Media Marketing via Facebook Success Secrets
PPTX
Risky Business
PPT
Aboriginal Relations, Perspectives from both sides of the fence with Gordon M...
PDF
Final_DF_deck
DOC
པོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབ
PDF
Advancing learning and transforming scholarship in higher education
PPTX
니나노경과
GC free coding in @Java presented @Geecon
Introduction to chronicle (low latency persistence)
Advanced off heap ipc
High Frequency Trading and NoSQL database
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
Writing and testing high frequency trading engines in java
Introduction to OpenHFT for Melbourne Java Users Group
Thread Safe Interprocess Shared Memory in Java (in 7 mins)
7.[54 59]the determinants of leverage of the listed-textile companies in india
Aplicaciones Básicas de Ubuntu
Evolucion De La Comunicaion
Unhealthy Developing World Food Markets
Aplicaciones basicas de unbuntu 14.02 LTE
Ruben Licera's Social Media Marketing via Facebook Success Secrets
Risky Business
Aboriginal Relations, Perspectives from both sides of the fence with Gordon M...
Final_DF_deck
པོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབ
Advancing learning and transforming scholarship in higher education
니나노경과
Ad

Similar to Using BigDecimal and double (7)

PDF
Payments nick
PPT
Finite word lenth effects
PDF
Chpater 6
PPTX
02. Data Types and variables
PPTX
Icom4015 lecture3-f17
PPT
0453545452552155255665-Math-Functions.ppt
PDF
Rubykaigi2010mrkn bigdecimal
Payments nick
Finite word lenth effects
Chpater 6
02. Data Types and variables
Icom4015 lecture3-f17
0453545452552155255665-Math-Functions.ppt
Rubykaigi2010mrkn bigdecimal
Ad

More from Peter Lawrey (12)

PPTX
Chronicle accelerate building a digital currency
PPTX
Chronicle Accelerate Crypto Investor conference
PPTX
Low latency microservices in java QCon New York 2016
PPTX
Low latency in java 8 v5
PPTX
Deterministic behaviour and performance in trading systems
PPTX
Determinism in finance
PPTX
Low latency for high throughput
PPTX
Legacy lambda code
PPTX
Responding rapidly when you have 100+ GB data sets in Java
PPT
Reactive programming with examples
PPT
Streams and lambdas the good, the bad and the ugly
ODP
Low level java programming
Chronicle accelerate building a digital currency
Chronicle Accelerate Crypto Investor conference
Low latency microservices in java QCon New York 2016
Low latency in java 8 v5
Deterministic behaviour and performance in trading systems
Determinism in finance
Low latency for high throughput
Legacy lambda code
Responding rapidly when you have 100+ GB data sets in Java
Reactive programming with examples
Streams and lambdas the good, the bad and the ugly
Low level java programming

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
sap open course for s4hana steps from ECC to s4
PDF
KodekX | Application Modernization Development
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.
Programs and apps: productivity, graphics, security and other tools
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
sap open course for s4hana steps from ECC to s4
KodekX | Application Modernization Development
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Dropbox Q2 2025 Financial Results & Investor Presentation
Understanding_Digital_Forensics_Presentation.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Digital-Transformation-Roadmap-for-Companies.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

Using BigDecimal and double

  • 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?