02157 functional programming - property-based testing ... · property-based testing was used to...
TRANSCRIPT
02157FunctionalProgram-
ming
Michael R. Hansen02157 Functional ProgrammingProperty-based testing: Floating-point properties
Michael R. Hansen
1 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
Overview
Testing properties of ”programs” involving floating-point values (oftype float)
Many obstacles, for example:• Approximations• Functions may be undefined for some arguments• .....
An example based presentation guided by complication spotted byFsCheck.
The technicalities concerning floats are not special to F#.
Floating-point values must always be treated with care!!
2 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
Testing that addition is associative
#r ".......FsCheck.dll"open FsCheck
let assoc(x:float,y,z) = x+(y+z) = (x+y) + z;;
Check.Verbose assoc;;
(*...3:(0.0, nan, 0.0)Falsifiable, after 4 tests (0 shrinks) (StdGen (1487969817,296669693)):Original:(0.0, nan, 0.0)
*)
What is nan?
3 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
Not A Number: nan:float
The domain of log is the positive floating-point values:
log -1.0;;val it : float = nan
nan = nan;;val it : bool = false
Ups — nan is indeed a special float value
It appears in randomly generated floats:
Gen.sample 10 10 Arb.generate<float>;;val it : float list =
[-6.556494257; 4.940656458e-324; ... ; nan;5.534954001; ... ; -6.136783418; 9.039187115]
4 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
Taking care of nan:float in tests
The law of associativity is only checked for floats that are not NaNs.
let assocV1(x,y,z) =(System.Double.IsNaN x|| System.Double.IsNaN y || System.Double.IsNaN z)|| x+(y+z) = (x+y) + z;;
But
Check.Verbose assocV1;;(* Falsifiable, after 17 tests (4 shrinks) (StdGen (830896496,296669696)):Original:(-1.355241514, 4.249447687, -3.425315758)Shrunk:(1.355241514, 4.0, 3.0) *)
What is the problem now?
5 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
Representation of float and float32 values
1.0 + 0.1 + 0.1 = 1.2;;val it : bool = false
NOT NICE!!!! WHY?
A floating point value (type float or float32) is represented bybinary numbers mant and exp, that represent
mant · 2exp
Problem:110 cannot be written in the form m · 2n with integers m and n.
The float representation of 110 is just an approximation.
6 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
Understanding the counterexample
The property being checked:
x + (y + z) = (x + y) + z
Check.Verbose assocV1;;(* Falsifiable, after 17 tests (4 shrinks) (StdGen (830896496,296669696)):Original:(-1.355241514, 4.249447687, -3.425315758)
Have a look at
let res1 = -1.355241514 + (4.249447687 + -3.425315758);;let res2 = (-1.355241514 + 4.249447687) + -3.425315758;;let diff = res1 - res2;;val diff : float = 2.220446049e-16
7 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
Equality within a tolerance
let assocV3 tol (x:float,y,z) =(System.Double.IsNaN x || System.Double.IsNaN y || .... )|| abs((x+(y+z)) - ((x+y) + z)) < tol;;
Check.Verbose (assocV3 1.0e-6);;
Falsifiable, after 2 tests (1 shrink) (StdGen (376632198,296669700)):Original:(1.797693135e+308, -infinity, 0.0)Shrunk:(0.0, -infinity, 0.0)
When does it stop?
8 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
A test Considering: -infinity, infinity and MaxValue
Are all special values:
> 1.0 / 0.0;;val it : float = infinity
> -1.0 / 0.0;;val it : float = -infinity
> System.Double.MaxValue;;val it : float = 1.797693135e+308
But there is no float for the textual representation of MaxValue
1.797693135e+308;;
1.797693135e+308;;ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ
stdin(6,1): error FS1153: Invalid floating point number
a problem for the testing
9 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
A test considering -infinity, infinity and MaxValue
let assocV5 tol (x,y,z) =(System.Double.IsNaN x || . ...)||(System.Double.IsInfinity x || .... )|| x = System.Double.MaxValue || y = .....|| abs((x+(y+z)) - ((x+y) + z)) < tol;;
Check.Verbose (assocV5 1.0e-14);;
Ok, passed 100 tests.
Can I be more confident?
let _ = Check.One ({ Config.Quick with MaxTest = 400; },assocV5 1.0e-14);;
Ok, passed 400 tests.
10 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019
02157FunctionalProgram-
ming
Michael R. Hansen
Summary
Property-based testing was used to reveal aspect involve concerningfloating-point programs
Floating-point values should be used with care!
The care needed is not specific to F#
A possible exercise:(Re)consider Exercise 6.8. Restrict the type Fexpr to theconstructors: X, Add, Sub and Log and restrict you solution to 6.8accordingly.
• Declare a function Eval fe x, for fe: Fexpr and x:float. Eval fe xcomputes the floating-point value of fe when X has value x
• Use FsCheck to test the correctness of the translation toinstructions in the sense that: for all fe, for all x
Eval fe x = intpProg(trans(fe,x))
11 DTU Compute, Technical University of Denmark Property-based testing: Floating-point properties MRH 14/11/2019