174
SCAN 2014 urzburg, Germany Formal verification of tricky numerical computations Sylvie Boldo Inria September 25th, 2014 (joint work with Cl´ ement, Filliˆ atre, Mayero, Melquiond, Weis)

Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

SCAN 2014Wurzburg, Germany

Formal verification of tricky numerical computations

Sylvie Boldo

Inria

September 25th, 2014

(joint work with Clement, Filliatre, Mayero, Melquiond, Weis)

Page 2: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Motivations

Scientific Computing, Computer Arithmetic and Validated Numerics

My personal challenge:

CORRECTNESS

consider small critical programs, where complex properties aboutfloating-point arithmetic are involved

How can we get a high guarantee?

↪→ formal verification

Convince people of what is formally verified!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 52

Page 3: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Motivations

Scientific Computing, Computer Arithmetic and Validated Numerics

My personal challenge:

CORRECTNESS

consider small critical programs, where complex properties aboutfloating-point arithmetic are involved

How can we get a high guarantee?

↪→ formal verification

Convince people of what is formally verified!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 52

Page 4: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Motivations

Scientific Computing, Computer Arithmetic and Validated Numerics

My personal challenge:

CORRECTNESS

consider small critical programs, where complex properties aboutfloating-point arithmetic are involved

How can we get a high guarantee?

↪→ formal verification

Convince people of what is formally verified!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 52

Page 5: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Motivations

Scientific Computing, Computer Arithmetic and Validated Numerics

My personal challenge:

CORRECTNESS

consider small critical programs, where complex properties aboutfloating-point arithmetic are involved

How can we get a high guarantee?

↪→ formal verification

Convince people of what is formally verified!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 52

Page 6: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Motivations

Scientific Computing, Computer Arithmetic and Validated Numerics

My personal challenge:

CORRECTNESS

consider small critical programs, where complex properties aboutfloating-point arithmetic are involved

How can we get a high guarantee?

↪→ formal verification

Convince people of what is formally verified!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 52

Page 7: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Motivations

Scientific Computing, Computer Arithmetic and Validated Numerics

My personal challenge:

CORRECTNESS

consider small critical programs, where complex properties aboutfloating-point arithmetic are involved

How can we get a high guarantee?

↪→ formal verification

Convince people of what is formally verified!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 52

Page 8: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Motivations

Scientific Computing, Computer Arithmetic and Validated Numerics

My personal challenge:

CORRECTNESS

consider small critical programs, where complex properties aboutfloating-point arithmetic are involved

How can we get a high guarantee?

↪→ formal verification

Convince people of what is formally verified!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 52

Page 9: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 3 / 52

Page 10: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

The used toolchain: Frama-C/Jessie/Why

Annotated C program

Frama-C/Jessie plug-in

WHY verification condition generator

Verification conditions

Automatic provers(Alt-Ergo,Gappa,CVC3,etc.)

Interactive provers(Coq,PVS,etc.)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 4 / 52

Page 11: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

The used toolchain: Frama-C/Jessie/Why

Annotated C program

Frama-C/Jessie plug-in

WHY verification condition generator

Verification conditions

Automatic provers(Alt-Ergo,Gappa,CVC3,etc.)

Interactive provers(Coq,PVS,etc.)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 4 / 52

Page 12: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

The used toolchain: Frama-C/Jessie/Why

Annotated C program

Frama-C/Jessie plug-in

WHY verification condition generator

Verification conditions

Automatic provers(Alt-Ergo,Gappa,CVC3,etc.)

Interactive provers(Coq,PVS,etc.)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 4 / 52

Page 13: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 5 / 52

Page 14: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Annotation language: ACSL

(how a bug differs from a rounding error)

ANSI/ISO C Specification Language

behavioral specification language for C programs

pre-conditions and post-conditions to functions(and which variables are modified).

variants and invariants of the loops.

assertions

In annotations, all computations are exact.

⇒ For the programmer, the specification is easy to understand.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 52

Page 15: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Annotation language: ACSL

(how a bug differs from a rounding error)

ANSI/ISO C Specification Language

behavioral specification language for C programs

pre-conditions and post-conditions to functions(and which variables are modified).

variants and invariants of the loops.

assertions

In annotations, all computations are exact.

⇒ For the programmer, the specification is easy to understand.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 52

Page 16: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Annotation language: ACSL

(how a bug differs from a rounding error)

ANSI/ISO C Specification Language

behavioral specification language for C programs

pre-conditions and post-conditions to functions(and which variables are modified).

variants and invariants of the loops.

assertions

In annotations, all computations are exact.

⇒ For the programmer, the specification is easy to understand.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 52

Page 17: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Annotation language: ACSL

(how a bug differs from a rounding error)

ANSI/ISO C Specification Language

behavioral specification language for C programs

pre-conditions and post-conditions to functions(and which variables are modified).

variants and invariants of the loops.

assertions

In annotations, all computations are exact.

⇒ For the programmer, the specification is easy to understand.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 52

Page 18: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Annotation language: ACSL

(how a bug differs from a rounding error)

ANSI/ISO C Specification Language

behavioral specification language for C programs

pre-conditions and post-conditions to functions(and which variables are modified).

variants and invariants of the loops.

assertions

In annotations, all computations are exact.

⇒ For the programmer, the specification is easy to understand.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 52

Page 19: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Annotation language: ACSL

(how a bug differs from a rounding error)

ANSI/ISO C Specification Language

behavioral specification language for C programs

pre-conditions and post-conditions to functions(and which variables are modified).

variants and invariants of the loops.

assertions

In annotations, all computations are exact.

⇒ For the programmer, the specification is easy to understand.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 52

Page 20: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Annotation language: ACSL

(how a bug differs from a rounding error)

ANSI/ISO C Specification Language

behavioral specification language for C programs

pre-conditions and post-conditions to functions(and which variables are modified).

variants and invariants of the loops.

assertions

In annotations, all computations are exact.

⇒ For the programmer, the specification is easy to understand.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 52

Page 21: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Annotation language: ACSL

(how a bug differs from a rounding error)

ANSI/ISO C Specification Language

behavioral specification language for C programs

pre-conditions and post-conditions to functions(and which variables are modified).

variants and invariants of the loops.

assertions

In annotations, all computations are exact.

⇒ For the programmer, the specification is easy to understand.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 52

Page 22: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

ACSL and floating-point numbers

A floating-point number is a triple:

the floating-point number, really computed by the program,x → xf floating-point part

the value that would have been obtained with exact computations,x → xe exact part

the value that we ideally wanted to computex → xm model part

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 7 / 52

Page 23: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

ACSL and floating-point numbers

A floating-point number is a triple:

the floating-point number, really computed by the program,x → xf floating-point part

the value that would have been obtained with exact computations,x → xe exact part

the value that we ideally wanted to computex → xm model part

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 7 / 52

Page 24: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

ACSL and floating-point numbers

A floating-point number is a triple:

the floating-point number, really computed by the program,x → xf floating-point part

the value that would have been obtained with exact computations,x → xe exact part

the value that we ideally wanted to computex → xm model part

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 7 / 52

Page 25: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

ACSL and floating-point numbers

A floating-point number is a triple:

the floating-point number, really computed by the program,x → xf floating-point part 1+x+x*x/2

the value that would have been obtained with exact computations,x → xe exact part 1 + x + x2

2

the value that we ideally wanted to computex → xm model part exp(x)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 7 / 52

Page 26: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

ACSL and floating-point numbers

A floating-point number is a triple:

the floating-point number, really computed by the program,x → xf floating-point part 1+x+x*x/2

the value that would have been obtained with exact computations,x → xe exact part 1 + x + x2

2

the value that we ideally wanted to computex → xm model part exp(x)

⇒ easy to split into method error and rounding error

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 7 / 52

Page 27: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 8 / 52

Page 28: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Formal proof

The proof is checked in its deep details until the computer agrees with it.

We often use formal proof checkers, meaning programs that only check aproof (they may also generate easy demonstrations).

Therefore the checker is a very short program (de Bruijn criteria: thecorrectness of the system as a whole depends on the correctness of a verysmall ”kernel”).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 9 / 52

Page 29: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

The Coq proof assistant (http://coq.inria.fr)

Based on the Curry-Howard isomorphism.(equivalence between proofs and λ-terms)

Few automations.

Comprehensive libraries, including on Z and R.

Coq kernel mechanically checks each step of each proof.

The method is to apply successively tactics (theorem application,rewriting, simplifications. . . ) to transform or reduce the goal down tothe hypotheses.

The proof is handled starting from the conclusion.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 10 / 52

Page 30: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

A Coq formalization of FP arithmetic : Flocq

A FP format is only characterized by a function ϕ : Z→ Z.

For x ∈ R, we compute e such that βe−1 ≤ |x | < βe .Then x is in the format iff

x =⌊

xβ−ϕ(e)⌋βϕ(e)

In other words: if it can be written with exponent ϕ(e).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 11 / 52

Page 31: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

A Coq formalization of FP arithmetic : Flocq

A FP format is only characterized by a function ϕ : Z→ Z.

For x ∈ R, we compute e such that βe−1 ≤ |x | < βe .Then x is in the format iff

x =⌊

xβ−ϕ(e)⌋βϕ(e)

In other words: if it can be written with exponent ϕ(e).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 11 / 52

Page 32: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Usual Formats

Definition (FIX)

Fixed-point format with exponent emin: ϕ(e) = emin.

Definition (FL*)

Floating-point format with precision p:

unbounded (FLX): ϕ(e) = e − p,

bounded with subnormal numbers (FLT): ϕ(e) = max(e − p, emin),

bounded without subnormal numbers (FTZ).

A random ϕ may not allow to define a rounding: we have a valid

predicate for being a reasonable ϕ.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 12 / 52

Page 33: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Usual Formats

Definition (FIX)

Fixed-point format with exponent emin: ϕ(e) = emin.

Definition (FL*)

Floating-point format with precision p:

unbounded (FLX): ϕ(e) = e − p,

bounded with subnormal numbers (FLT): ϕ(e) = max(e − p, emin),

bounded without subnormal numbers (FTZ).

A random ϕ may not allow to define a rounding: we have a valid

predicate for being a reasonable ϕ.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 12 / 52

Page 34: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Usual Formats

Definition (FIX)

Fixed-point format with exponent emin: ϕ(e) = emin.

Definition (FL*)

Floating-point format with precision p:

unbounded (FLX): ϕ(e) = e − p,

bounded with subnormal numbers (FLT): ϕ(e) = max(e − p, emin),

bounded without subnormal numbers (FTZ).

A random ϕ may not allow to define a rounding: we have a valid

predicate for being a reasonable ϕ.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 12 / 52

Page 35: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Usual Formats

Definition (FIX)

Fixed-point format with exponent emin: ϕ(e) = emin.

Definition (FL*)

Floating-point format with precision p:

unbounded (FLX): ϕ(e) = e − p,

bounded with subnormal numbers (FLT): ϕ(e) = max(e − p, emin),

bounded without subnormal numbers (FTZ).

A random ϕ may not allow to define a rounding: we have a valid

predicate for being a reasonable ϕ.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 12 / 52

Page 36: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Usual Floating-Point Formats

FLXFLT

FTZFTZ

FLT

FLX

ϕ(e)

emin emin + p − 1 e

emin

p − 1emin +

p

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 13 / 52

Page 37: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Example of Coq theorem

Theorem (round NE abs)

Let ϕ be a format, such that the rounding to nearest, ties to even (◦) canbe defined. For all x ∈ R, ◦(|x |) = |◦(x)|.

Lemma round NE abs: forall x : R,

round beta fexp ZnearestE (Rabs x) = Rabs (round beta fexp ZnearestE x).

Proof with auto with typeclass instances.

intros x; apply sym eq.

unfold Rabs at 2.

destruct (Rcase abs x) as [Hx|Hx].

rewrite round NE opp.

apply Rabs left1.

rewrite <- (round 0 beta fexp ZnearestE).

apply round le...

now apply Rlt le.

apply Rabs pos eq.

rewrite <- (round 0 beta fexp ZnearestE).

apply round le...

now apply Rge le.

Qed.

With the stating of the theorem, the tactics, and the name of theorems.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 14 / 52

Page 38: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Example of Coq theorem

Theorem (round NE abs)

Let ϕ be a format, such that the rounding to nearest, ties to even (◦) canbe defined. For all x ∈ R, ◦(|x |) = |◦(x)|.

Lemma round NE abs: forall x : R,

round beta fexp ZnearestE (Rabs x) = Rabs (round beta fexp ZnearestE x).

Proof with auto with typeclass instances.

intros x; apply sym eq.

unfold Rabs at 2.

destruct (Rcase abs x) as [Hx|Hx].

rewrite round NE opp.

apply Rabs left1.

rewrite <- (round 0 beta fexp ZnearestE).

apply round le...

now apply Rlt le.

apply Rabs pos eq.

rewrite <- (round 0 beta fexp ZnearestE).

apply round le...

now apply Rge le.

Qed.

With the stating of the theorem, the tactics, and the name of theorems.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 14 / 52

Page 39: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Example of Coq theorem

Theorem (round NE abs)

Let ϕ be a format, such that the rounding to nearest, ties to even (◦) canbe defined. For all x ∈ R, ◦(|x |) = |◦(x)|.

Lemma round NE abs: forall x : R,

round beta fexp ZnearestE (Rabs x) = Rabs (round beta fexp ZnearestE x).

Proof with auto with typeclass instances.

intros x; apply sym eq.

unfold Rabs at 2.

destruct (Rcase abs x) as [Hx|Hx].

rewrite round NE opp.

apply Rabs left1.

rewrite <- (round 0 beta fexp ZnearestE).

apply round le...

now apply Rlt le.

apply Rabs pos eq.

rewrite <- (round 0 beta fexp ZnearestE).

apply round le...

now apply Rge le.

Qed.

With the stating of the theorem, the tactics, and the name of theorems.Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 14 / 52

Page 40: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

More about Flocq

Flocq: 16 000 lines of Coq, 700 theorems,

any radix, any format,

both axiomatic and computable definitions of rounding,

effective arithmetic operators,

numerous theorems.

Applications:

CompCert certified C compiler

http://flocq.gforge.inria.fr/

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 15 / 52

Page 41: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

More about Flocq

Flocq: 16 000 lines of Coq, 700 theorems,

any radix, any format,

both axiomatic and computable definitions of rounding,

effective arithmetic operators,

numerous theorems.

Applications:

Frama-C/Jessie C code certifier

CompCert certified C compiler

http://flocq.gforge.inria.fr/

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 15 / 52

Page 42: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

More about Flocq

Flocq: 16 000 lines of Coq, 700 theorems,

any radix, any format,

both axiomatic and computable definitions of rounding,

effective arithmetic operators,

numerous theorems.

Applications:

Frama-C/Jessie C code certifier

CompCert certified C compiler

http://flocq.gforge.inria.fr/

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 15 / 52

Page 43: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 44: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 45: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 46: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← Human

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 47: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← Human

Coq ← Human

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 48: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← Human

Coq ← Human

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 49: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← Human

Coq ← Human

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 50: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Coq ← Human

Coq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 51: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Coq ← Human

Coq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 52: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Coq ← Human

Coq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 53: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Coq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 54: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 55: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Methodology for the verification of C programs

C Program

Annotated C Program(specification, invariant)

Human

Theorem statementsFrama-C

Jessie

Automaticprovers(Alt-Ergo,Gappa, Z3)

Coq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← HumanCoq ← Human

Proved Theorems

Coq ← Human

The program is correct withrespect to its specifications

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 16 / 52

Page 56: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 17 / 52

Page 57: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Examples

All examples use Frama-C Neon, Why 2.34 and Why3 0.83.

Non-automatic proof obligations are proved using Coq 8.4pl4.

Overflow is considered a runtime error.

Code & proofs available onhttp://www.lri.fr/~sboldo/research.html.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 18 / 52

Page 58: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Examples

All examples use Frama-C Neon, Why 2.34 and Why3 0.83.

Non-automatic proof obligations are proved using Coq 8.4pl4.

Overflow is considered a runtime error.

Code & proofs available onhttp://www.lri.fr/~sboldo/research.html.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 18 / 52

Page 59: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Examples

All examples use Frama-C Neon, Why 2.34 and Why3 0.83.

Non-automatic proof obligations are proved using Coq 8.4pl4.

Overflow is considered a runtime error.

Code & proofs available onhttp://www.lri.fr/~sboldo/research.html.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 18 / 52

Page 60: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Examples

All examples use Frama-C Neon, Why 2.34 and Why3 0.83.

Non-automatic proof obligations are proved using Coq 8.4pl4.

Overflow is considered a runtime error.

Code & proofs available onhttp://www.lri.fr/~sboldo/research.html.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 18 / 52

Page 61: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Sterbenz

Theorem (Sterbenz)

If x and y are FP numbers in a given precision such that

y

2≤ x ≤ 2y ,

then x − y fits in a FP number in the same precision and is thereforecomputed without error.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 19 / 52

Page 62: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Sterbenz – program

/*@ requires y/2. <= x <= 2.*y;

@ ensures \result == x-y;

@*/

f l o a t S t e r b e n z ( f l o a t x , f l o a t y ) {return x−y ;

}

Exact subtraction

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 20 / 52

Page 63: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Sterbenz – program

/*@ requires y/2. <= x <= 2.*y;

@ ensures \result == x-y;

@*/

f l o a t S t e r b e n z ( f l o a t x , f l o a t y ) {return x−y ;

}

Exact subtraction

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 20 / 52

Page 64: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Sterbenz – proofs

Proof obligations CVC3 CoqNb lines

VC for behavior 2.34 6

VC for safety 0.23

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 21 / 52

Page 65: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 22 / 52

Page 66: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker

Also known as Error-Free-Transformation for the multiplication.

Theorem (Veltkamp/Dekker)

Provided no Overflow and no Underflow occur, there is an algorithmcomputing the exact error of the multiplication using only FP operations.

Idea:split your floats in 2, multiply all the parts, add them in the correct order.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 23 / 52

Page 67: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker

Also known as Error-Free-Transformation for the multiplication.

Theorem (Veltkamp/Dekker)

Provided no Overflow and no Underflow occur, there is an algorithmcomputing the exact error of the multiplication using only FP operations.

Idea:split your floats in 2, multiply all the parts, add them in the correct order.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 23 / 52

Page 68: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker – program

xy = ◦(xy)

Overflow

If no UnderflowExact error of ⊗

Split x and y

Multiply all halves and

add all the results

/*@ requires xy == \round_double (\ NearestEven ,x*y) &&

@ \abs(x) <= 0x1.p995 &&

@ \abs(y) <= 0x1.p995 &&

@ \abs(x*y) <= 0x1.p1021;

@ ensures ((x*y == 0 || 0x1.p-969 <= \abs(x*y))

@ ==> x*y == xy+\ result );

@*/

double Dekker ( double x , double y , double xy ) {

double C , px , qx , hx , py , qy , hy , tx , ty , r2 ;C=0x8000001p0 ;/*@ assert C == 0x1p27 +1; */

px=x∗C ; qx=x−px ; hx=px+qx ; t x=x−hx ;

py=y∗C ; qy=y−py ; hy=py+qy ; t y=y−hy ;

r 2=−xy+hx∗hy ;r 2+=hx∗ t y ;r 2+=hy∗ t x ;r 2+=t x ∗ t y ;r e t u r n r 2 ;

}

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 24 / 52

Page 69: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker – program

xy = ◦(xy)

Overflow

If no UnderflowExact error of ⊗

Split x and y

Multiply all halves and

add all the results

/*@ requires xy == \round_double (\ NearestEven ,x*y) &&

@ \abs(x) <= 0x1.p995 &&

@ \abs(y) <= 0x1.p995 &&

@ \abs(x*y) <= 0x1.p1021;

@ ensures ((x*y == 0 || 0x1.p-969 <= \abs(x*y))

@ ==> x*y == xy+\ result );

@*/

double Dekker ( double x , double y , double xy ) {

double C , px , qx , hx , py , qy , hy , tx , ty , r2 ;C=0x8000001p0 ;/*@ assert C == 0x1p27 +1; */

px=x∗C ; qx=x−px ; hx=px+qx ; t x=x−hx ;

py=y∗C ; qy=y−py ; hy=py+qy ; t y=y−hy ;

r 2=−xy+hx∗hy ;r 2+=hx∗ t y ;r 2+=hy∗ t x ;r 2+=t x ∗ t y ;r e t u r n r 2 ;

}

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 24 / 52

Page 70: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker – program

xy = ◦(xy)

Overflow

If no UnderflowExact error of ⊗

Split x and y

Multiply all halves and

add all the results

/*@ requires xy == \round_double (\ NearestEven ,x*y) &&

@ \abs(x) <= 0x1.p995 &&

@ \abs(y) <= 0x1.p995 &&

@ \abs(x*y) <= 0x1.p1021;

@ ensures ((x*y == 0 || 0x1.p-969 <= \abs(x*y))

@ ==> x*y == xy+\ result );

@*/

double Dekker ( double x , double y , double xy ) {

double C , px , qx , hx , py , qy , hy , tx , ty , r2 ;C=0x8000001p0 ;/*@ assert C == 0x1p27 +1; */

px=x∗C ; qx=x−px ; hx=px+qx ; t x=x−hx ;

py=y∗C ; qy=y−py ; hy=py+qy ; t y=y−hy ;

r 2=−xy+hx∗hy ;r 2+=hx∗ t y ;r 2+=hy∗ t x ;r 2+=t x ∗ t y ;r e t u r n r 2 ;

}

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 24 / 52

Page 71: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker – program

xy = ◦(xy)

Overflow

If no Underflow

Exact error of ⊗

Split x and y

Multiply all halves and

add all the results

/*@ requires xy == \round_double (\ NearestEven ,x*y) &&

@ \abs(x) <= 0x1.p995 &&

@ \abs(y) <= 0x1.p995 &&

@ \abs(x*y) <= 0x1.p1021;

@ ensures ((x*y == 0 || 0x1.p-969 <= \abs(x*y))

@ ==> x*y == xy+\ result );

@*/

double Dekker ( double x , double y , double xy ) {

double C , px , qx , hx , py , qy , hy , tx , ty , r2 ;C=0x8000001p0 ;/*@ assert C == 0x1p27 +1; */

px=x∗C ; qx=x−px ; hx=px+qx ; t x=x−hx ;

py=y∗C ; qy=y−py ; hy=py+qy ; t y=y−hy ;

r 2=−xy+hx∗hy ;r 2+=hx∗ t y ;r 2+=hy∗ t x ;r 2+=t x ∗ t y ;r e t u r n r 2 ;

}

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 24 / 52

Page 72: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker – program

xy = ◦(xy)

Overflow

If no Underflow

Exact error of ⊗

Split x and y

Multiply all halves and

add all the results

/*@ requires xy == \round_double (\ NearestEven ,x*y) &&

@ \abs(x) <= 0x1.p995 &&

@ \abs(y) <= 0x1.p995 &&

@ \abs(x*y) <= 0x1.p1021;

@ ensures ((x*y == 0 || 0x1.p-969 <= \abs(x*y))

@ ==> x*y == xy+\ result );

@*/

double Dekker ( double x , double y , double xy ) {

double C , px , qx , hx , py , qy , hy , tx , ty , r2 ;C=0x8000001p0 ;/*@ assert C == 0x1p27 +1; */

px=x∗C ; qx=x−px ; hx=px+qx ; t x=x−hx ;

py=y∗C ; qy=y−py ; hy=py+qy ; t y=y−hy ;

r 2=−xy+hx∗hy ;r 2+=hx∗ t y ;r 2+=hy∗ t x ;r 2+=t x ∗ t y ;r e t u r n r 2 ;

}

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 24 / 52

Page 73: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker – program

xy = ◦(xy)

Overflow

If no UnderflowExact error of ⊗

Split x and y

Multiply all halves and

add all the results

/*@ requires xy == \round_double (\ NearestEven ,x*y) &&

@ \abs(x) <= 0x1.p995 &&

@ \abs(y) <= 0x1.p995 &&

@ \abs(x*y) <= 0x1.p1021;

@ ensures ((x*y == 0 || 0x1.p-969 <= \abs(x*y))

@ ==> x*y == xy+\ result );

@*/

double Dekker ( double x , double y , double xy ) {

double C , px , qx , hx , py , qy , hy , tx , ty , r2 ;C=0x8000001p0 ;/*@ assert C == 0x1p27 +1; */

px=x∗C ; qx=x−px ; hx=px+qx ; t x=x−hx ;

py=y∗C ; qy=y−py ; hy=py+qy ; t y=y−hy ;

r 2=−xy+hx∗hy ;r 2+=hx∗ t y ;r 2+=hy∗ t x ;r 2+=t x ∗ t y ;r e t u r n r 2 ;

}

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 24 / 52

Page 74: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker – program

xy = ◦(xy)

Overflow

If no UnderflowExact error of ⊗

Split x and y

Multiply all halves and

add all the results

/*@ requires xy == \round_double (\ NearestEven ,x*y) &&

@ \abs(x) <= 0x1.p995 &&

@ \abs(y) <= 0x1.p995 &&

@ \abs(x*y) <= 0x1.p1021;

@ ensures ((x*y == 0 || 0x1.p-969 <= \abs(x*y))

@ ==> x*y == xy+\ result );

@*/

double Dekker ( double x , double y , double xy ) {

double C , px , qx , hx , py , qy , hy , tx , ty , r2 ;C=0x8000001p0 ;/*@ assert C == 0x1p27 +1; */

px=x∗C ; qx=x−px ; hx=px+qx ; t x=x−hx ;

py=y∗C ; qy=y−py ; hy=py+qy ; t y=y−hy ;

r 2=−xy+hx∗hy ;r 2+=hx∗ t y ;r 2+=hy∗ t x ;r 2+=t x ∗ t y ;r e t u r n r 2 ;

}

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 24 / 52

Page 75: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Veltkamp/Dekker – proof

Proof obligations Coq TimeNb lines

Previous Coq proof (spec + proof) 2639

VC for behavior 1. assertion 32. postcondition 238

VC for safety 1-9. FP overflow 1 or 210. FP overflow 3711. FP overflow 4712. FP overflow 4313. FP overflow 6414. FP overflow 4315. FP overflow 8316. FP overflow 4917. FP overflow 94

Total (1,248 lines spec VC excluded) 3351 9 min 02

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 25 / 52

Page 76: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 26 / 52

Page 77: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant

It is pretty hard to compute b2 − ac accurately.

Theorem (Kahan)

Provided no Overflow and no Underflow occur, there is an algorithmcomputing the b2 − a ∗ c within 2 ulps.

Idea:Test whether there is cancellation. If not, then use the naive algorithm.Else, compute the errors of the multiplication, and add everything in thecorrect order.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 27 / 52

Page 78: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant

It is pretty hard to compute b2 − ac accurately.

Theorem (Kahan)

Provided no Overflow and no Underflow occur, there is an algorithmcomputing the b2 − a ∗ c within 2 ulps.

Idea:Test whether there is cancellation. If not, then use the naive algorithm.Else, compute the errors of the multiplication, and add everything in thecorrect order.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 27 / 52

Page 79: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant

It is pretty hard to compute b2 − ac accurately.

Theorem (Kahan)

Provided no Overflow and no Underflow occur, there is an algorithmcomputing the b2 − a ∗ c within 2 ulps.

Idea:Test whether there is cancellation. If not, then use the naive algorithm.Else, compute the errors of the multiplication, and add everything in thecorrect order.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 27 / 52

Page 80: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithmCompute themultiplication errorsAdd everything,

p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 81: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithmCompute themultiplication errorsAdd everything,

p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 82: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithm

Compute themultiplication errorsAdd everything,

p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 83: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithm

Compute themultiplication errors

Add everything,p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 84: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithmCompute themultiplication errors

Add everything,p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 85: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithmCompute themultiplication errorsAdd everything,

p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 86: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithmCompute themultiplication errorsAdd everything,

p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 87: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithmCompute themultiplication errorsAdd everything,

p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 88: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithmCompute themultiplication errorsAdd everything,

p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 89: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – program

Underflow

Overflow

2 ulps

/*@ requires

@ (b==0. || 0x1.p-916 <= \abs(b*b)) &&

@ (a*c==0. || 0x1.p-916 <= \abs(a*c)) &&

@ \abs(b) <= 0x1.p510 &&

@ \abs(a) <= 0x1.p995 && \abs(c) <= 0x1.p995 &&

@ \abs(a*c) <= 0x1.p1021;

@ ensures \result ==0.

@ || \abs(\result -(b*b-a*c)) <= 2.* ulp(\ result );

@ */

double d i s c r i m i n a n t ( double a , double b , double c ) {double p , q , d , dp , dq ;p=b∗b ;q=a∗c ;

i f ( p+q <= 3∗ f a b s ( p−q ) )d=p−q ;

e l s e {dp=Dekker ( b , b , p ) ;dq=Dekker ( a , c , q ) ;d=(p−q)+(dp−dq ) ;

}r e t u r n d ;

}

Test of cancellation

When p ≥ q, it roughlycorresponds to p ≥ 2 q

Naive algorithmCompute themultiplication errorsAdd everything,

p−q being correct.

As q2 . p . 2 q

Function calls

⇒ pre-conditions to prove

⇒ post-conditions guaranteed

In initial proof,test assumed correct

⇒ Additional proofwhen test is incorrect

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 28 / 52

Page 90: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Accurate discriminant – proofProof obligations Coq Time

Previous Coq proof (spec + proof) 3390

VC for theory realization 88

Behavior 1. postcondition 612. postcondition 90

Safety 1. floating-point overflow 22. floating-point overflow 23. floating-point overflow 34. floating-point overflow 45. floating-point overflow 46. precondition for call 27. precondition for call 98. precondition for call 19-13. precondition for call 214. floating-point overflow 4415. floating-point overflow 45

Total (1,146 lines spec VC excluded) 3655 5 min 47Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 29 / 52

Page 91: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 30 / 52

Page 92: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area

b c

a

Heron’s formula: ∆ =√

s (s − a) (s − b) (s − c) where s = a+b+c2 .

Kahan’s formula, for c ≤ b ≤ a:

∆ =1

4

√(a + (b + c)) (c − (a− b)) (c + (a− b)) (a + (b − c)).

[Kahan, Miscalculating Area and Angles of a Needle-like Triangle]

Area ∆ is accurate to within a few units in their last digits.

[Goldberg, 1991]

The rounding error of area ∆ is at most 11 ε, provided ε < 0.005 andsubtraction and square roots are accurate.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 31 / 52

Page 93: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area

b c

a

Heron’s formula: ∆ =√

s (s − a) (s − b) (s − c) where s = a+b+c2 .

Kahan’s formula, for c ≤ b ≤ a:

∆ =1

4

√(a + (b + c)) (c − (a− b)) (c + (a− b)) (a + (b − c)).

[Kahan, Miscalculating Area and Angles of a Needle-like Triangle]

Area ∆ is accurate to within a few units in their last digits.

[Goldberg, 1991]

The rounding error of area ∆ is at most 11 ε, provided ε < 0.005 andsubtraction and square roots are accurate.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 31 / 52

Page 94: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area

b c

a

Heron’s formula: ∆ =√

s (s − a) (s − b) (s − c) where s = a+b+c2 .

Kahan’s formula, for c ≤ b ≤ a:

∆ =1

4

√(a + (b + c)) (c − (a− b)) (c + (a− b)) (a + (b − c)).

[Kahan, Miscalculating Area and Angles of a Needle-like Triangle]

Area ∆ is accurate to within a few units in their last digits.

[Goldberg, 1991]

The rounding error of area ∆ is at most 11 ε, provided ε < 0.005 andsubtraction and square roots are accurate.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 31 / 52

Page 95: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area

b c

a

Heron’s formula: ∆ =√

s (s − a) (s − b) (s − c) where s = a+b+c2 .

Kahan’s formula, for c ≤ b ≤ a:

∆ =1

4

√(a + (b + c)) (c − (a− b)) (c + (a− b)) (a + (b − c)).

[Kahan, Miscalculating Area and Angles of a Needle-like Triangle]

Area ∆ is accurate to within a few units in their last digits.

[Goldberg, 1991]

The rounding error of area ∆ is at most 11 ε, provided ε < 0.005 andsubtraction and square roots are accurate.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 31 / 52

Page 96: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area

Theorem (err ∆ flx radix2)

With an unbounded exponent range, β = 2, and ε ≤ 1100 , The rounding

error of area ∆ is at most 4.75ε+ 33ε2.

For underflow:

detect afterwards if a subnormal appeared in the computation

order the intermediate variables, and multiply the biggest first:0 ≤ c (a b) ≤ c ⊕ (a b) ≤ a⊕ (b c) ≤ a⊕ (b ⊕ c)

Theorem (err ∆ flt radix2)

We assume that β = 2, that ε ≤ 1100 , and that 2

⌈Ei+p−1

2

⌉−2

< ∆. Therounding error of area ∆ (computed in the given order) is at most4.75ε+ 33ε2.

(and 5.75ε in radix 10 as multiplying by 14 is not exact).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 32 / 52

Page 97: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area

Theorem (err ∆ flx radix2)

With an unbounded exponent range, β = 2, and ε ≤ 1100 , The rounding

error of area ∆ is at most 4.75ε+ 33ε2.

For underflow:

detect afterwards if a subnormal appeared in the computation

order the intermediate variables, and multiply the biggest first:0 ≤ c (a b) ≤ c ⊕ (a b) ≤ a⊕ (b c) ≤ a⊕ (b ⊕ c)

Theorem (err ∆ flt radix2)

We assume that β = 2, that ε ≤ 1100 , and that 2

⌈Ei+p−1

2

⌉−2

< ∆. Therounding error of area ∆ (computed in the given order) is at most4.75ε+ 33ε2.

(and 5.75ε in radix 10 as multiplying by 14 is not exact).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 32 / 52

Page 98: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area

Theorem (err ∆ flx radix2)

With an unbounded exponent range, β = 2, and ε ≤ 1100 , The rounding

error of area ∆ is at most 4.75ε+ 33ε2.

For underflow:

detect afterwards if a subnormal appeared in the computation

order the intermediate variables, and multiply the biggest first:0 ≤ c (a b) ≤ c ⊕ (a b) ≤ a⊕ (b c) ≤ a⊕ (b ⊕ c)

Theorem (err ∆ flt radix2)

We assume that β = 2, that ε ≤ 1100 , and that 2

⌈Ei+p−1

2

⌉−2

< ∆. Therounding error of area ∆ (computed in the given order) is at most4.75ε+ 33ε2.

(and 5.75ε in radix 10 as multiplying by 14 is not exact).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 32 / 52

Page 99: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area

Theorem (err ∆ flx radix2)

With an unbounded exponent range, β = 2, and ε ≤ 1100 , The rounding

error of area ∆ is at most 4.75ε+ 33ε2.

For underflow:

detect afterwards if a subnormal appeared in the computation

order the intermediate variables, and multiply the biggest first:0 ≤ c (a b) ≤ c ⊕ (a b) ≤ a⊕ (b c) ≤ a⊕ (b ⊕ c)

Theorem (err ∆ flt radix2)

We assume that β = 2, that ε ≤ 1100 , and that 2

⌈Ei+p−1

2

⌉−2

< ∆. Therounding error of area ∆ (computed in the given order) is at most4.75ε+ 33ε2.

(and 5.75ε in radix 10 as multiplying by 14 is not exact).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 32 / 52

Page 100: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengthsoverflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 101: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengthsoverflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 102: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengthsoverflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 103: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengthsoverflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 104: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengths

overflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 105: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengths

overflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 106: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengthsoverflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 107: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengthsoverflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 108: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – program

Square root definition

Heron’s formula

(no rounding)

Kahan’s algorithmwith properly ordered values

ordered side lengthsoverflow condition

If no underflow

Error bound

/*@ requires 0 <= x;

@ ensures \result ==\ round_double (\ NearestEven ,\sqrt(x));

@*/

double s q r t ( double x ) ;

/*@ logic real S(real a, real b, real c) =

@ \let s = (a+b+c)/2;

@ \sqrt(s*(s-a)*(s-b)*(s-c));

@ */

/*@ requires 0 <= c <= b <= a && a <= b + c && a <= 0x1p255;

@ ensures 0x1p -513 < \result

@ ==> \abs(\result -S(a,b,c))

@ <= (4.75*0x1p -53 + 33*0x1p -106)*S(a,b,c);

@ */

double t r i a n g l e ( double a , double b , double c ) {r e t u r n (0 x1p−2∗ s q r t ( ( a+(b+c ) )∗ ( a+(b−c ) )∗ ( c+(a−b ) )∗ ( c−(a−b ) ) ) ) ;

}

Function call

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 33 / 52

Page 109: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – proof

Proof obligations Gappa CoqPrevious Coq proof 18.89 2091

Behavior 1. postcondition 16.00 82

Safety 1. FP overflow 0.022. FP overflow 0.033. FP overflow 0.034. FP overflow 0.035. FP overflow 0.036. FP overflow 0.007. FP overflow 0.028. FP overflow 0.019. FP overflow 0.0010. FP overflow 0.0211. FP overflow 0.0212. precondition for call 13.22 1313. FP overflow 0.0314. FP overflow 0.04

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 34 / 52

Page 110: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Triangle area – proof

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 35 / 52

Page 111: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 36 / 52

Page 112: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

The wave equation

Looking for u : R2 → R regular enough such that:

∂2u(x , t)

∂t2− c2∂

2u(x , t)

∂x2= s(x , t)

with given values for the initial position u0(x) and the initial velocity u1(x).

⇒ rope oscillation, sound, radar, oil prospection. . .

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 37 / 52

Page 113: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Scheme?

We want ukj ≈ u(j∆x , k∆t).

ukj − 2uk−1

j + uk−2j

∆t2− c2

uk−1j+1 − 2uk−1

j + uk−1j−1

∆x2= sk−1

j

And other horrible formulas to initialize u0j and u1

j .

t

k∆t

(k − 1)∆t

(k − 2)∆t

xj∆x(j − 1)∆x (j + 1)∆x

Three-point scheme: ukj depends on uk−1

j−1 , uk−1j , uk−1

j+1 and uk−2j .

Not really tricky computer arithmetic!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 38 / 52

Page 114: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Scheme?

We want ukj ≈ u(j∆x , k∆t).

ukj − 2uk−1

j + uk−2j

∆t2− c2

uk−1j+1 − 2uk−1

j + uk−1j−1

∆x2= sk−1

j

And other horrible formulas to initialize u0j and u1

j .

t

k∆t

(k − 1)∆t

(k − 2)∆t

xj∆x(j − 1)∆x (j + 1)∆x

Three-point scheme: ukj depends on uk−1

j−1 , uk−1j , uk−1

j+1 and uk−2j .

Not really tricky computer arithmetic!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 38 / 52

Page 115: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Scheme?

We want ukj ≈ u(j∆x , k∆t).

ukj − 2uk−1

j + uk−2j

∆t2− c2

uk−1j+1 − 2uk−1

j + uk−1j−1

∆x2= sk−1

j

And other horrible formulas to initialize u0j and u1

j .

t

k∆t

(k − 1)∆t

(k − 2)∆t

xj∆x(j − 1)∆x (j + 1)∆x

Three-point scheme: ukj depends on uk−1

j−1 , uk−1j , uk−1

j+1 and uk−2j .

Not really tricky computer arithmetic!

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 38 / 52

Page 116: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Program

// i n i t i a l i z a t i o n o f p [ i ] [ 0 ] and p [ i ] [ 1 ]f o r ( k=1; k<nk ; k++) {

p [ 0 ] [ k+1] = 0 . ;f o r ( i =1; i<n i ; i ++) {

dp = p [ i +1] [ k ] − 2 .∗ p [ i ] [ k ] + p [ i −1] [ k ] ;p [ i ] [ k+1] = 2 .∗ p [ i ] [ k ] − p [ i ] [ k−1] + a∗dp ;}

p [ n i ] [ k+1] = 0 . ;}

Two different errors:

round-off errorsdue to floating-point roundings

method errorsthe scheme only approximates the exact solution

Page 117: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Program

// i n i t i a l i z a t i o n o f p [ i ] [ 0 ] and p [ i ] [ 1 ]f o r ( k=1; k<nk ; k++) {

p [ 0 ] [ k+1] = 0 . ;f o r ( i =1; i<n i ; i ++) {

dp = p [ i +1] [ k ] − 2 .∗ p [ i ] [ k ] + p [ i −1] [ k ] ;p [ i ] [ k+1] = 2 .∗ p [ i ] [ k ] − p [ i ] [ k−1] + a∗dp ;}

p [ n i ] [ k+1] = 0 . ;}

Two different errors:

round-off errorsdue to floating-point roundings

method errorsthe scheme only approximates the exact solution

Page 118: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Rounding error

Remainder:

dp = p [ i +1] [ k ] − 2 .∗ p [ i ] [ k ] + p [ i −1] [ k ] ;p [ i ] [ k+1] = 2 .∗ p [ i ] [ k ] − p [ i ] [ k−1] + a∗dp ;

If we use a naive technique to bound the rounding errors, we get

|pki − exact(pk

i )| ≤ O(

2k2−53)

This is too much because the errors do compensate.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 40 / 52

Page 119: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Rounding error

Remainder:

dp = p [ i +1] [ k ] − 2 .∗ p [ i ] [ k ] + p [ i −1] [ k ] ;p [ i ] [ k+1] = 2 .∗ p [ i ] [ k ] − p [ i ] [ k−1] + a∗dp ;

If we use a naive technique to bound the rounding errors, we get

|pki − exact(pk

i )| ≤ O(

2k2−53)

This is too much because the errors do compensate.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 40 / 52

Page 120: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Rounding error

Remainder:

dp = p [ i +1] [ k ] − 2 .∗ p [ i ] [ k ] + p [ i −1] [ k ] ;p [ i ] [ k+1] = 2 .∗ p [ i ] [ k ] − p [ i ] [ k−1] + a∗dp ;

If we use a naive technique to bound the rounding errors, we get

|pki − exact(pk

i )| ≤ O(

2k2−53)

This is too much because the errors do compensate.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 40 / 52

Page 121: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Definition of εki

Remainder:

dp = p [ i +1] [ k ] − 2 .∗ p [ i ] [ k ] + p [ i −1] [ k ] ;p [ i ] [ k+1] = 2 .∗ p [ i ] [ k ] − p [ i ] [ k−1] + a∗dp ;

Let εk+1i be the rounding error made during these two lines of computations.

We assume a, pki−1, pk

i , pki+1 and pk−1

i are exact and we look into the

rounding error of these two lines. It is called εk+1i .

We know (from initializations) that the model values of the |pmn | are

bounded by 1. We assume that the floating-point values of the |pmn | are

bounded by 2.

|εmn | ≤ 78× 2−52

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 41 / 52

Page 122: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Definition of εki

Remainder:

dp = p [ i +1] [ k ] − 2 .∗ p [ i ] [ k ] + p [ i −1] [ k ] ;p [ i ] [ k+1] = 2 .∗ p [ i ] [ k ] − p [ i ] [ k−1] + a∗dp ;

Let εk+1i be the rounding error made during these two lines of computations.

We assume a, pki−1, pk

i , pki+1 and pk−1

i are exact and we look into the

rounding error of these two lines. It is called εk+1i .

We know (from initializations) that the model values of the |pmn | are

bounded by 1. We assume that the floating-point values of the |pmn | are

bounded by 2.

|εmn | ≤ 78× 2−52

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 41 / 52

Page 123: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Definition of εki

Remainder:

dp = p [ i +1] [ k ] − 2 .∗ p [ i ] [ k ] + p [ i −1] [ k ] ;p [ i ] [ k+1] = 2 .∗ p [ i ] [ k ] − p [ i ] [ k−1] + a∗dp ;

Let εk+1i be the rounding error made during these two lines of computations.

We assume a, pki−1, pk

i , pki+1 and pk−1

i are exact and we look into the

rounding error of these two lines. It is called εk+1i .

We know (from initializations) that the model values of the |pmn | are

bounded by 1. We assume that the floating-point values of the |pmn | are

bounded by 2.

|εmn | ≤ 78× 2−52

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 41 / 52

Page 124: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Rounding error

pki − exact(pki ) =k∑

l=0

l∑j=−l

αlj ε

k−li+j

1 We have an analytical expression of the rounding error with knownconstants αk

i .

2 It is not that complicated!(we cannot get rid of the pyramidal double summation)

3 The rounding error is bounded by ©(k2 2−53):∣∣∣pki − exact

(pk

i

)∣∣∣ ≤ 78× 2−53 × (k + 1)× (k + 2)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 42 / 52

Page 125: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Rounding error

pki − exact(pki ) =k∑

l=0

l∑j=−l

αlj ε

k−li+j

1 We have an analytical expression of the rounding error with knownconstants αk

i .

2 It is not that complicated!(we cannot get rid of the pyramidal double summation)

3 The rounding error is bounded by ©(k2 2−53):∣∣∣pki − exact

(pk

i

)∣∣∣ ≤ 78× 2−53 × (k + 1)× (k + 2)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 42 / 52

Page 126: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Rounding error

pki − exact(pki ) =k∑

l=0

l∑j=−l

αlj ε

k−li+j

1 We have an analytical expression of the rounding error with knownconstants αk

i .

2 It is not that complicated!(we cannot get rid of the pyramidal double summation)

3 The rounding error is bounded by ©(k2 2−53):∣∣∣pki − exact

(pk

i

)∣∣∣ ≤ 78× 2−53 × (k + 1)× (k + 2)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 42 / 52

Page 127: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Method error

We measure that u and ukj are close when (∆x ,∆t)→ 0.

We define ekjdef= uk

j − ukj : convergence error

where ukj is the value of u at the (j , k) point of the grid.

We want to bound∥∥∥e

k∆t(t)h

∥∥∥∆x

: the average of the convergence error on

all points of the grid at a given time k∆t(t) =⌊

t∆t

⌋∆t.

We want to prove: ∥∥∥ek∆t(t)h

∥∥∥∆x

= O[0,tmax](∆x2 + ∆t2)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 43 / 52

Page 128: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Method error

We measure that u and ukj are close when (∆x ,∆t)→ 0.

We define ekjdef= uk

j − ukj : convergence error

where ukj is the value of u at the (j , k) point of the grid.

We want to bound∥∥∥e

k∆t(t)h

∥∥∥∆x

: the average of the convergence error on

all points of the grid at a given time k∆t(t) =⌊

t∆t

⌋∆t.

We want to prove: ∥∥∥ek∆t(t)h

∥∥∥∆x

= O[0,tmax](∆x2 + ∆t2)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 43 / 52

Page 129: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Method error

We measure that u and ukj are close when (∆x ,∆t)→ 0.

We define ekjdef= uk

j − ukj : convergence error

where ukj is the value of u at the (j , k) point of the grid.

We want to bound∥∥∥e

k∆t(t)h

∥∥∥∆x

: the average of the convergence error on

all points of the grid at a given time k∆t(t) =⌊

t∆t

⌋∆t.

We want to prove: ∥∥∥ek∆t(t)h

∥∥∥∆x

= O[0,tmax](∆x2 + ∆t2)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 43 / 52

Page 130: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Convergence

We proved that:∥∥∥ek∆t(t)h

∥∥∥∆x

= O t ∈ [0, tmax]

(∆x ,∆t)→ 00 < ∆x ∧ 0 < ∆t ∧ζ ≤ c ∆t

∆x≤ 1− ξ

(∆x2 + ∆t2).

(This is out of the scope of this talk.)

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 44 / 52

Page 131: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Extraction of the big O constants

The preceding result is a uniform big O defined by:

∃α,C > 0, ∀x,∆x, ‖∆x‖ ≤ α⇒ |f (x,∆x)| ≤ C · |g(∆x)|.

Let (α3,C3) be the constants for the order-3 Taylor development of theexact solution and (α4,C4) for order-4. The initial support is [χ1;χ2].

α = min(α3, α4, 1, tmax)

s1 = max(1, 2 · C4 · (c2 + 1),C3 · (1 + c2/2) + 1)

s2 = s21

(bχ2c − bχ1c+ 2 · c · tmax ·

(1 +

1

ζ

)+ 3

)s3 =

1√

2

(C3 · (1 + c2/2) + 1

)· (χ2 − χ1 + 1 + (2 · c + 4))

+

√2

2√

2ξ − ξ2(2 · tmax · s2 + 2s2)

C =

√2√

2ξ − ξ2· 2 · tmax · s3

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 45 / 52

Page 132: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Extraction of the big O constants

The preceding result is a uniform big O defined by:

∃α,C > 0, ∀x,∆x, ‖∆x‖ ≤ α⇒ |f (x,∆x)| ≤ C · |g(∆x)|.

Let (α3,C3) be the constants for the order-3 Taylor development of theexact solution and (α4,C4) for order-4. The initial support is [χ1;χ2].

α = min(α3, α4, 1, tmax)

s1 = max(1, 2 · C4 · (c2 + 1),C3 · (1 + c2/2) + 1)

s2 = s21

(bχ2c − bχ1c+ 2 · c · tmax ·

(1 +

1

ζ

)+ 3

)s3 =

1√

2

(C3 · (1 + c2/2) + 1

)· (χ2 − χ1 + 1 + (2 · c + 4))

+

√2

2√

2ξ − ξ2(2 · tmax · s2 + 2s2)

C =

√2√

2ξ − ξ2· 2 · tmax · s3

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 45 / 52

Page 133: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Extraction of the big O constants

The preceding result is a uniform big O defined by:

∃α,C > 0, ∀x,∆x, ‖∆x‖ ≤ α⇒ |f (x,∆x)| ≤ C · |g(∆x)|.

Let (α3,C3) be the constants for the order-3 Taylor development of theexact solution and (α4,C4) for order-4. The initial support is [χ1;χ2].

α = min(α3, α4, 1, tmax)

s1 = max(1, 2 · C4 · (c2 + 1),C3 · (1 + c2/2) + 1)

s2 = s21

(bχ2c − bχ1c+ 2 · c · tmax ·

(1 +

1

ζ

)+ 3

)s3 =

1√

2

(C3 · (1 + c2/2) + 1

)· (χ2 − χ1 + 1 + (2 · c + 4))

+

√2

2√

2ξ − ξ2(2 · tmax · s2 + 2s2)

C =

√2√

2ξ − ξ2· 2 · tmax · s3

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 45 / 52

Page 134: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Program verification

154 lines of annotations for 32 lines of C

150 verification conditions:

44 about the behavior106 about the safety (runtime errors)

Prover Behavior VC Safety VC Total

Alt-Ergo 18 80 98

CVC3 18 89 107

Gappa 2 20 22

Z3 21 63 84

Automatically proved 23 94 117Coq 21 12 33

Total 44 106 150

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 46 / 52

Page 135: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Program verification

154 lines of annotations for 32 lines of C

150 verification conditions:

44 about the behavior106 about the safety (runtime errors)

Prover Behavior VC Safety VC Total

Alt-Ergo 18 80 98

CVC3 18 89 107

Gappa 2 20 22

Z3 21 63 84

Automatically proved 23 94 117Coq 21 12 33

Total 44 106 150

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 46 / 52

Page 136: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Program verification

About 90 % of the safety goals (matrix access, Overflow, and so on)are proved automatically.

33 theorems are interactively proved using Coq for a total of about15,000 lines of Coq and 30 minutes of compilation.

Type of proofs Nb spec lines Nb lines Compilation time

Convergence 991 5 275 42 s

Round-off + runtime errors 7 737 13 175 32 min

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 47 / 52

Page 137: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Outline

1 Introduction

2 ToolsFrama-C/Jessie/WhyACSLProof assistant: Coq

3 ExamplesSterbenzError of the multiplicationAccurate discriminantArea of a triangle1-D Wave equation discretization

4 Conclusion

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 48 / 52

Page 138: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

always a Coq proof, generic wrt precision and minimal exponent(and often radix)

but also an annotated C program that handles exceptional behavior(e. g. Overflow, division by zero)

formal proofs are required because algorithms are tricky

formal proofs are possible because algorithms are small

(Have you seen long tricky algorithms?)

not applicable on big (naive) industrial algorithms

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 49 / 52

Page 139: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

always a Coq proof, generic wrt precision and minimal exponent(and often radix)

but also an annotated C program that handles exceptional behavior(e. g. Overflow, division by zero)

formal proofs are required because algorithms are tricky

formal proofs are possible because algorithms are small

(Have you seen long tricky algorithms?)

not applicable on big (naive) industrial algorithms

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 49 / 52

Page 140: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

always a Coq proof, generic wrt precision and minimal exponent(and often radix)

but also an annotated C program that handles exceptional behavior(e. g. Overflow, division by zero)

formal proofs are required because algorithms are tricky

formal proofs are possible because algorithms are small

(Have you seen long tricky algorithms?)

not applicable on big (naive) industrial algorithms

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 49 / 52

Page 141: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

always a Coq proof, generic wrt precision and minimal exponent(and often radix)

but also an annotated C program that handles exceptional behavior(e. g. Overflow, division by zero)

formal proofs are required because algorithms are tricky

formal proofs are possible because algorithms are small

(Have you seen long tricky algorithms?)

not applicable on big (naive) industrial algorithms

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 49 / 52

Page 142: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

always a Coq proof, generic wrt precision and minimal exponent(and often radix)

but also an annotated C program that handles exceptional behavior(e. g. Overflow, division by zero)

formal proofs are required because algorithms are tricky

formal proofs are possible because algorithms are small

(Have you seen long tricky algorithms?)

not applicable on big (naive) industrial algorithms

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 49 / 52

Page 143: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

always a Coq proof, generic wrt precision and minimal exponent(and often radix)

but also an annotated C program that handles exceptional behavior(e. g. Overflow, division by zero)

formal proofs are required because algorithms are tricky

formal proofs are possible because algorithms are small

(Have you seen long tricky algorithms?)

not applicable on big (naive) industrial algorithms

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 49 / 52

Page 144: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

Very high guarantee

not only rounding errors:

all other errors such as pointer dereferencing or division by zerolink with mathematical propertiesany property can be checked

expressive annotation language (as expressive as Coq)⇒ exactly the specification you want

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 50 / 52

Page 145: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

Very high guarantee

not only rounding errors:

all other errors such as pointer dereferencing or division by zerolink with mathematical propertiesany property can be checked

expressive annotation language (as expressive as Coq)⇒ exactly the specification you want

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 50 / 52

Page 146: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

Very high guarantee

not only rounding errors:

all other errors such as pointer dereferencing or division by zero

link with mathematical propertiesany property can be checked

expressive annotation language (as expressive as Coq)⇒ exactly the specification you want

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 50 / 52

Page 147: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

Very high guarantee

not only rounding errors:

all other errors such as pointer dereferencing or division by zerolink with mathematical properties

any property can be checked

expressive annotation language (as expressive as Coq)⇒ exactly the specification you want

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 50 / 52

Page 148: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

Very high guarantee

not only rounding errors:

all other errors such as pointer dereferencing or division by zerolink with mathematical propertiesany property can be checked

expressive annotation language (as expressive as Coq)⇒ exactly the specification you want

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 50 / 52

Page 149: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Conclusion

Very high guarantee

not only rounding errors:

all other errors such as pointer dereferencing or division by zerolink with mathematical propertiesany property can be checked

expressive annotation language (as expressive as Coq)⇒ exactly the specification you want

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 50 / 52

Page 150: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Limits: compilation

We assume all double operations are direct 64-bits roundings.

On recent processors, we have x86 extended registers (80-bits long)and FMA (◦(ax + b) with one single rounding).

⇒ several possible results!

Solution 1: cover all cases.

only use forward analysis with a slightly larger bound(it covers, 64-bit, 80-bit, double roundings and all uses of FMA)

Solution 2: look into the assembly, and prove what is compiled.

Solution 3: use a certified compiler, then compilation is specified.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 51 / 52

Page 151: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Limits: compilation

We assume all double operations are direct 64-bits roundings.

On recent processors, we have x86 extended registers (80-bits long)and FMA (◦(ax + b) with one single rounding).

⇒ several possible results!

Solution 1: cover all cases.

only use forward analysis with a slightly larger bound(it covers, 64-bit, 80-bit, double roundings and all uses of FMA)

Solution 2: look into the assembly, and prove what is compiled.

Solution 3: use a certified compiler, then compilation is specified.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 51 / 52

Page 152: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Limits: compilation

We assume all double operations are direct 64-bits roundings.

On recent processors, we have x86 extended registers (80-bits long)and FMA (◦(ax + b) with one single rounding).

⇒ several possible results!

Solution 1: cover all cases.

only use forward analysis with a slightly larger bound(it covers, 64-bit, 80-bit, double roundings and all uses of FMA)

Solution 2: look into the assembly, and prove what is compiled.

Solution 3: use a certified compiler, then compilation is specified.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 51 / 52

Page 153: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Limits: compilation

We assume all double operations are direct 64-bits roundings.

On recent processors, we have x86 extended registers (80-bits long)and FMA (◦(ax + b) with one single rounding).

⇒ several possible results!

Solution 1: cover all cases.

only use forward analysis with a slightly larger bound(it covers, 64-bit, 80-bit, double roundings and all uses of FMA)

Solution 2: look into the assembly, and prove what is compiled.

Solution 3: use a certified compiler, then compilation is specified.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 51 / 52

Page 154: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Limits: compilation

We assume all double operations are direct 64-bits roundings.

On recent processors, we have x86 extended registers (80-bits long)and FMA (◦(ax + b) with one single rounding).

⇒ several possible results!

Solution 1: cover all cases.

only use forward analysis with a slightly larger bound(it covers, 64-bit, 80-bit, double roundings and all uses of FMA)

Solution 2: look into the assembly, and prove what is compiled.

Solution 3: use a certified compiler, then compilation is specified.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 51 / 52

Page 155: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Limits: compilation

We assume all double operations are direct 64-bits roundings.

On recent processors, we have x86 extended registers (80-bits long)and FMA (◦(ax + b) with one single rounding).

⇒ several possible results!

Solution 1: cover all cases.

only use forward analysis with a slightly larger bound(it covers, 64-bit, 80-bit, double roundings and all uses of FMA)

Solution 2: look into the assembly, and prove what is compiled.

Solution 3: use a certified compiler, then compilation is specified.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 51 / 52

Page 156: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Limits: compilation

We assume all double operations are direct 64-bits roundings.

On recent processors, we have x86 extended registers (80-bits long)and FMA (◦(ax + b) with one single rounding).

⇒ several possible results!

Solution 1: cover all cases.

only use forward analysis with a slightly larger bound(it covers, 64-bit, 80-bit, double roundings and all uses of FMA)

Solution 2: look into the assembly, and prove what is compiled.

Solution 3: use a certified compiler, then compilation is specified.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 51 / 52

Page 157: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Perspectives

a better handling of exceptional behaviors

prove and generalize well-known facts/algorithms/programs from thecomputer arithmetic community

⇒ basic blocks to build upon

prove libraries with computational contents(e.g. computational geometry)

go deeper into numerical analysis

⇒ e.g. finite elements

⇒ e.g. stability

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 52 / 52

Page 158: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Perspectives

a better handling of exceptional behaviors

prove and generalize well-known facts/algorithms/programs from thecomputer arithmetic community

⇒ basic blocks to build upon

prove libraries with computational contents(e.g. computational geometry)

go deeper into numerical analysis

⇒ e.g. finite elements

⇒ e.g. stability

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 52 / 52

Page 159: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Perspectives

a better handling of exceptional behaviors

prove and generalize well-known facts/algorithms/programs from thecomputer arithmetic community

⇒ basic blocks to build upon

prove libraries with computational contents(e.g. computational geometry)

go deeper into numerical analysis

⇒ e.g. finite elements

⇒ e.g. stability

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 52 / 52

Page 160: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Perspectives

a better handling of exceptional behaviors

prove and generalize well-known facts/algorithms/programs from thecomputer arithmetic community

⇒ basic blocks to build upon

prove libraries with computational contents(e.g. computational geometry)

go deeper into numerical analysis

⇒ e.g. finite elements

⇒ e.g. stability

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 52 / 52

Page 161: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Perspectives

a better handling of exceptional behaviors

prove and generalize well-known facts/algorithms/programs from thecomputer arithmetic community

⇒ basic blocks to build upon

prove libraries with computational contents(e.g. computational geometry)

go deeper into numerical analysis

⇒ e.g. finite elements

⇒ e.g. stability

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 52 / 52

Page 162: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Perspectives

a better handling of exceptional behaviors

prove and generalize well-known facts/algorithms/programs from thecomputer arithmetic community

⇒ basic blocks to build upon

prove libraries with computational contents(e.g. computational geometry)

go deeper into numerical analysis

⇒ e.g. finite elements

⇒ e.g. stability

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 52 / 52

Page 163: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Perspectives

a better handling of exceptional behaviors

prove and generalize well-known facts/algorithms/programs from thecomputer arithmetic community

⇒ basic blocks to build upon

prove libraries with computational contents(e.g. computational geometry)

go deeper into numerical analysis

⇒ e.g. finite elements

⇒ e.g. stability

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 52 / 52

Page 164: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

This is not a slide.

Page 165: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Big O = big pain

Usually, the big O uses one variable and f (x) = O‖x‖→0(g(x)) means

∃α,C > 0, ∀x ∈ Rn, ‖x‖ ≤ α⇒ |f (x)| ≤ C · |g(x)|.

Here 2 variables: ∆x (grid sizes, tends to 0), and x (time and space).(Think about Taylor expansions)

∀x, ∃α,C > 0, ∀∆x ∈ R2, ‖∆x‖ ≤ α⇒ |f (x,∆x)| ≤ C · |g(∆x)|

does not work.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 6

Page 166: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Big O = big pain

Usually, the big O uses one variable and f (x) = O‖x‖→0(g(x)) means

∃α,C > 0, ∀x ∈ Rn, ‖x‖ ≤ α⇒ |f (x)| ≤ C · |g(x)|.

Here 2 variables: ∆x (grid sizes, tends to 0), and x (time and space).(Think about Taylor expansions)

∀x, ∃α,C > 0, ∀∆x ∈ R2, ‖∆x‖ ≤ α⇒ |f (x,∆x)| ≤ C · |g(∆x)|

does not work.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 6

Page 167: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Big O = big pain

Usually, the big O uses one variable and f (x) = O‖x‖→0(g(x)) means

∃α,C > 0, ∀x ∈ Rn, ‖x‖ ≤ α⇒ |f (x)| ≤ C · |g(x)|.

Here 2 variables: ∆x (grid sizes, tends to 0), and x (time and space).(Think about Taylor expansions)

∀x, ∃α,C > 0, ∀∆x ∈ R2, ‖∆x‖ ≤ α⇒ |f (x,∆x)| ≤ C · |g(∆x)|

does not work.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 2 / 6

Page 168: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Uniform big O

We used a uniform big O:

∃α,C > 0, ∀x,∆x, ‖∆x‖ ≤ α⇒ |f (x,∆x)| ≤ C · |g(∆x)|.

where variables x and ∆x are restricted to subsets of R2.(for example such that ∆t > 0)⇒ Taylor expansions

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 3 / 6

Page 169: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Proof idea 1/3: consistency

The truncation error is defined as how much the exact solution solves thenumerical scheme:

εk−1j =

ukj − 2uk−1

j + uk−2j

∆t2− c2

uk−1j+1 − 2uk−1

j + uk−1j−1

∆x2− sk−1

j

The consistency is the boundedness of the truncation error:∥∥∥εk∆t(t)h

∥∥∥∆x

= O[0,tmax](∆x2 + ∆t2)

By Taylor series and many computations.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 4 / 6

Page 170: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Proof idea 1/3: consistency

The truncation error is defined as how much the exact solution solves thenumerical scheme:

εk−1j =

ukj − 2uk−1

j + uk−2j

∆t2− c2

uk−1j+1 − 2uk−1

j + uk−1j−1

∆x2− sk−1

j

The consistency is the boundedness of the truncation error:∥∥∥εk∆t(t)h

∥∥∥∆x

= O[0,tmax](∆x2 + ∆t2)

By Taylor series and many computations.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 4 / 6

Page 171: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Proof idea 2/3: stability

We define a discrete energy by

Eh(c)(uh)k+ 12

def=

1

2

∥∥∥∥∥uk+1h − uk

h

∆t

∥∥∥∥∥2

∆x

+1

2

⟨ukh , u

k+1h

⟩Ah(c)

kinetic energy potential energy

〈vh,wh〉Ah(c)def= 〈Ah(c) vh,wh〉∆x and (Ah(c) vh)j

def= − c2 vj+1−2vj+vj−1

∆x2 .

Note that this energy is constant if f = 0.We prove an overestimation and an underestimation of this energy.⇒ uh does not diverge.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 5 / 6

Page 172: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Proof idea 2/3: stability

We define a discrete energy by

Eh(c)(uh)k+ 12

def=

1

2

∥∥∥∥∥uk+1h − uk

h

∆t

∥∥∥∥∥2

∆x

+1

2

⟨ukh , u

k+1h

⟩Ah(c)

kinetic energy potential energy

〈vh,wh〉Ah(c)def= 〈Ah(c) vh,wh〉∆x and (Ah(c) vh)j

def= − c2 vj+1−2vj+vj−1

∆x2 .

Note that this energy is constant if f = 0.We prove an overestimation and an underestimation of this energy.⇒ uh does not diverge.

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 5 / 6

Page 173: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Proof idea 3/3: convergence

The convergence error is solution of the same discrete scheme with inputs

u0,j = 0, u1,j =e1j

∆t, and skj = εk+1

j .

+ proofs about the initializations.

All these proofs require the existence of ζ and ξ in ]0, 1[ with ζ ≤ 1− ξand we require that ζ ≤ c∆t

∆x ≤ 1− ξ (CFL conditions).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 6

Page 174: Formal verification of tricky numerical computations · 2014. 10. 14. · For x 2R, we compute e such that e 1 jxj< e. Then x is in the format i x = j x ’(e) k ’(e) In other words:if

Proof idea 3/3: convergence

The convergence error is solution of the same discrete scheme with inputs

u0,j = 0, u1,j =e1j

∆t, and skj = εk+1

j .

+ proofs about the initializations.

All these proofs require the existence of ζ and ξ in ]0, 1[ with ζ ≤ 1− ξand we require that ζ ≤ c∆t

∆x ≤ 1− ξ (CFL conditions).

Sylvie Boldo (Inria) Formal verification of numerical computations September 25th, 2014 6 / 6