SlideShare uma empresa Scribd logo
1 de 10
(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?

Mais conteúdo relacionado

Mais procurados

Capabilities for Resources and Effects
Capabilities for Resources and EffectsCapabilities for Resources and Effects
Capabilities for Resources and EffectsMartin Odersky
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)paramisoft
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events WebStackAcademy
 
Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : BasicsMitul Desai
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic ProgrammingSahil Kumar
 
Domain Modeling in a Functional World
Domain Modeling in a Functional WorldDomain Modeling in a Functional World
Domain Modeling in a Functional WorldDebasish Ghosh
 
Cs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papersCs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papersappasami
 
Sentiment analysis on Bangla conversation using machine learning approach
Sentiment analysis on Bangla conversation using machine  learning approachSentiment analysis on Bangla conversation using machine  learning approach
Sentiment analysis on Bangla conversation using machine learning approachIJECEIAES
 
Lecture 05 problem solving through ai
Lecture 05 problem solving through aiLecture 05 problem solving through ai
Lecture 05 problem solving through aiHema Kashyap
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaJorge Vásquez
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 

Mais procurados (20)

Capabilities for Resources and Effects
Capabilities for Resources and EffectsCapabilities for Resources and Effects
Capabilities for Resources and Effects
 
Refactoring
RefactoringRefactoring
Refactoring
 
Java Micro-Benchmarking
Java Micro-BenchmarkingJava Micro-Benchmarking
Java Micro-Benchmarking
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Clean Code
Clean CodeClean Code
Clean Code
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events
 
JDBC
JDBCJDBC
JDBC
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : Basics
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Domain Modeling in a Functional World
Domain Modeling in a Functional WorldDomain Modeling in a Functional World
Domain Modeling in a Functional World
 
Cs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papersCs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papers
 
Sentiment analysis on Bangla conversation using machine learning approach
Sentiment analysis on Bangla conversation using machine  learning approachSentiment analysis on Bangla conversation using machine  learning approach
Sentiment analysis on Bangla conversation using machine learning approach
 
Lecture 05 problem solving through ai
Lecture 05 problem solving through aiLecture 05 problem solving through ai
Lecture 05 problem solving through ai
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
 
C#
C#C#
C#
 
Variables and Data Types
Variables and Data TypesVariables and Data Types
Variables and Data Types
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 

Destaque

Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016Peter Lawrey
 
Open HFT libraries in @Java
Open HFT libraries in @JavaOpen HFT libraries in @Java
Open HFT libraries in @JavaPeter Lawrey
 
GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconPeter Lawrey
 
Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)Peter Lawrey
 
Advanced off heap ipc
Advanced off heap ipcAdvanced off heap ipc
Advanced off heap ipcPeter Lawrey
 
High Frequency Trading and NoSQL database
High Frequency Trading and NoSQL databaseHigh Frequency Trading and NoSQL database
High Frequency Trading and NoSQL databasePeter Lawrey
 
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint PetersburgUnsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint PetersburgChristoph Engelbert
 
Writing and testing high frequency trading engines in java
Writing and testing high frequency trading engines in javaWriting and testing high frequency trading engines in java
Writing and testing high frequency trading engines in javaPeter Lawrey
 
Introduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users GroupIntroduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users GroupPeter Lawrey
 
Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe Interprocess Shared Memory in Java (in 7 mins)Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe Interprocess Shared Memory in Java (in 7 mins)Peter Lawrey
 
7.[54 59]the determinants of leverage of the listed-textile companies in india
7.[54 59]the determinants of leverage of the listed-textile companies in india7.[54 59]the determinants of leverage of the listed-textile companies in india
7.[54 59]the determinants of leverage of the listed-textile companies in indiaAlexander Decker
 
Ruben Licera's Social Media Marketing via Facebook Success Secrets
Ruben Licera's Social Media Marketing via Facebook Success SecretsRuben Licera's Social Media Marketing via Facebook Success Secrets
Ruben Licera's Social Media Marketing via Facebook Success SecretsRUBEN LICERA
 
Final_DF_deck
Final_DF_deckFinal_DF_deck
Final_DF_deckJon Cline
 
པོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབ
པོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབཔོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབ
པོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབQuickoffice Test
 

Destaque (20)

Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016Microservices for performance - GOTO Chicago 2016
Microservices for performance - GOTO Chicago 2016
 
Open HFT libraries in @Java
Open HFT libraries in @JavaOpen HFT libraries in @Java
Open HFT libraries in @Java
 
GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @Geecon
 
Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)Introduction to chronicle (low latency persistence)
Introduction to chronicle (low latency persistence)
 
Advanced off heap ipc
Advanced off heap ipcAdvanced off heap ipc
Advanced off heap ipc
 
High Frequency Trading and NoSQL database
High Frequency Trading and NoSQL databaseHigh Frequency Trading and NoSQL database
High Frequency Trading and NoSQL database
 
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint PetersburgUnsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
Unsafe Java World - Crossing the Borderline - JokerConf 2014 Saint Petersburg
 
Writing and testing high frequency trading engines in java
Writing and testing high frequency trading engines in javaWriting and testing high frequency trading engines in java
Writing and testing high frequency trading engines in java
 
Introduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users GroupIntroduction to OpenHFT for Melbourne Java Users Group
Introduction to OpenHFT for Melbourne Java Users Group
 
Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe Interprocess Shared Memory in Java (in 7 mins)Thread Safe Interprocess Shared Memory in Java (in 7 mins)
Thread Safe Interprocess Shared Memory in Java (in 7 mins)
 
7.[54 59]the determinants of leverage of the listed-textile companies in india
7.[54 59]the determinants of leverage of the listed-textile companies in india7.[54 59]the determinants of leverage of the listed-textile companies in india
7.[54 59]the determinants of leverage of the listed-textile companies in india
 
Aplicaciones Básicas de Ubuntu
Aplicaciones Básicas de UbuntuAplicaciones Básicas de Ubuntu
Aplicaciones Básicas de Ubuntu
 
Evolucion De La Comunicaion
Evolucion De La ComunicaionEvolucion De La Comunicaion
Evolucion De La Comunicaion
 
Unhealthy Developing World Food Markets
Unhealthy Developing World Food MarketsUnhealthy Developing World Food Markets
Unhealthy Developing World Food Markets
 
Aplicaciones basicas de unbuntu 14.02 LTE
Aplicaciones basicas de unbuntu 14.02 LTEAplicaciones basicas de unbuntu 14.02 LTE
Aplicaciones basicas de unbuntu 14.02 LTE
 
Ruben Licera's Social Media Marketing via Facebook Success Secrets
Ruben Licera's Social Media Marketing via Facebook Success SecretsRuben Licera's Social Media Marketing via Facebook Success Secrets
Ruben Licera's Social Media Marketing via Facebook Success Secrets
 
Risky Business
Risky BusinessRisky Business
Risky Business
 
Aboriginal Relations, Perspectives from both sides of the fence with Gordon M...
Aboriginal Relations, Perspectives from both sides of the fence with Gordon M...Aboriginal Relations, Perspectives from both sides of the fence with Gordon M...
Aboriginal Relations, Perspectives from both sides of the fence with Gordon M...
 
Final_DF_deck
Final_DF_deckFinal_DF_deck
Final_DF_deck
 
པོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབ
པོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབཔོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབ
པོི ུ ཧགཡད ིཇོོོིུནགཧཡཧཏ ཧཙགངགདཧཇ༄༄།ན ཙཅཛཟ ཅཅཅདརཛེ ཏཏེེཇིཇབ
 

Semelhante a Using BigDecimal and double

Decimal arithmetic in Processors
Decimal arithmetic in ProcessorsDecimal arithmetic in Processors
Decimal arithmetic in ProcessorsPeeyush Pashine
 
C Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptC Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptGAURAVNAUTIYAL19
 
Data Representation - Floating Point
Data Representation - Floating PointData Representation - Floating Point
Data Representation - Floating PointFahim Fana
 
Lesson plan on data representation
Lesson plan on data representationLesson plan on data representation
Lesson plan on data representationPooja Tripathi
 
PMHMathematicaSample
PMHMathematicaSamplePMHMathematicaSample
PMHMathematicaSamplePeter Hammel
 
Lec 02 data representation part 1
Lec 02 data representation part 1Lec 02 data representation part 1
Lec 02 data representation part 1Abdul Khan
 
data representation
 data representation data representation
data representationHaroon_007
 
Pertemuan 2 - Sistem Bilangan
Pertemuan 2 - Sistem BilanganPertemuan 2 - Sistem Bilangan
Pertemuan 2 - Sistem Bilanganahmad haidaroh
 
Practice Exercise Set 1
Practice Exercise Set 1Practice Exercise Set 1
Practice Exercise Set 1rampan
 
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014Cyrille Martraire
 
OverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxOverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxgerardkortney
 
Module 1 number systems and code1
Module 1  number systems and code1Module 1  number systems and code1
Module 1 number systems and code1Deepak John
 
Aplicaciones de la derivada en la Carrera de Contabilidad y Auditoría
Aplicaciones de la derivada en la Carrera de Contabilidad y AuditoríaAplicaciones de la derivada en la Carrera de Contabilidad y Auditoría
Aplicaciones de la derivada en la Carrera de Contabilidad y AuditoríaLeslieMoncayo1
 
CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com  CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com williamwordsworth10
 

Semelhante a Using BigDecimal and double (20)

Decimal arithmetic in Processors
Decimal arithmetic in ProcessorsDecimal arithmetic in Processors
Decimal arithmetic in Processors
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptC Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.ppt
 
Pseudocode By ZAK
Pseudocode By ZAKPseudocode By ZAK
Pseudocode By ZAK
 
Data Representation - Floating Point
Data Representation - Floating PointData Representation - Floating Point
Data Representation - Floating Point
 
Chapter 1
Chapter   1Chapter   1
Chapter 1
 
Lesson plan on data representation
Lesson plan on data representationLesson plan on data representation
Lesson plan on data representation
 
PMHMathematicaSample
PMHMathematicaSamplePMHMathematicaSample
PMHMathematicaSample
 
Lec 02 data representation part 1
Lec 02 data representation part 1Lec 02 data representation part 1
Lec 02 data representation part 1
 
Python Programming
Python Programming Python Programming
Python Programming
 
Es272 ch2
Es272 ch2Es272 ch2
Es272 ch2
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
 
data representation
 data representation data representation
data representation
 
Pertemuan 2 - Sistem Bilangan
Pertemuan 2 - Sistem BilanganPertemuan 2 - Sistem Bilangan
Pertemuan 2 - Sistem Bilangan
 
Practice Exercise Set 1
Practice Exercise Set 1Practice Exercise Set 1
Practice Exercise Set 1
 
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
 
OverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxOverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docx
 
Module 1 number systems and code1
Module 1  number systems and code1Module 1  number systems and code1
Module 1 number systems and code1
 
Aplicaciones de la derivada en la Carrera de Contabilidad y Auditoría
Aplicaciones de la derivada en la Carrera de Contabilidad y AuditoríaAplicaciones de la derivada en la Carrera de Contabilidad y Auditoría
Aplicaciones de la derivada en la Carrera de Contabilidad y Auditoría
 
CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com  CIS 115 Education for Service--cis115.com
CIS 115 Education for Service--cis115.com
 

Mais de Peter Lawrey

Chronicle accelerate building a digital currency
Chronicle accelerate   building a digital currencyChronicle accelerate   building a digital currency
Chronicle accelerate building a digital currencyPeter Lawrey
 
Chronicle Accelerate Crypto Investor conference
Chronicle Accelerate Crypto Investor conferenceChronicle Accelerate Crypto Investor conference
Chronicle Accelerate Crypto Investor conferencePeter Lawrey
 
Low latency microservices in java QCon New York 2016
Low latency microservices in java   QCon New York 2016Low latency microservices in java   QCon New York 2016
Low latency microservices in java QCon New York 2016Peter Lawrey
 
Low latency in java 8 v5
Low latency in java 8 v5Low latency in java 8 v5
Low latency in java 8 v5Peter Lawrey
 
Deterministic behaviour and performance in trading systems
Deterministic behaviour and performance in trading systemsDeterministic behaviour and performance in trading systems
Deterministic behaviour and performance in trading systemsPeter Lawrey
 
Determinism in finance
Determinism in financeDeterminism in finance
Determinism in financePeter Lawrey
 
Low latency for high throughput
Low latency for high throughputLow latency for high throughput
Low latency for high throughputPeter Lawrey
 
Legacy lambda code
Legacy lambda codeLegacy lambda code
Legacy lambda codePeter Lawrey
 
Responding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in JavaResponding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in JavaPeter Lawrey
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examplesPeter Lawrey
 
Streams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the uglyStreams and lambdas the good, the bad and the ugly
Streams and lambdas the good, the bad and the uglyPeter Lawrey
 
Low level java programming
Low level java programmingLow level java programming
Low level java programmingPeter Lawrey
 

Mais de Peter Lawrey (12)

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

Último

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Último (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

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?