Revision 33ce4d2d test/hs/Test/Ganeti/TestCommon.hs
b/test/hs/Test/Ganeti/TestCommon.hs | ||
---|---|---|
65 | 65 |
, testParser |
66 | 66 |
, genPropParser |
67 | 67 |
, genNonNegative |
68 |
, relativeError |
|
68 | 69 |
) where |
69 | 70 |
|
70 | 71 |
import Control.Applicative |
... | ... | |
404 | 405 |
genNonNegative :: Gen Int |
405 | 406 |
genNonNegative = |
406 | 407 |
fmap fromIntegral (arbitrary::Gen (Test.QuickCheck.NonNegative Int)) |
408 |
|
|
409 |
-- | Computes the relative error of two 'Double' numbers. |
|
410 |
-- |
|
411 |
-- This is the \"relative error\" algorithm in |
|
412 |
-- http:\/\/randomascii.wordpress.com\/2012\/02\/25\/ |
|
413 |
-- comparing-floating-point-numbers-2012-edition (URL split due to too |
|
414 |
-- long line). |
|
415 |
relativeError :: Double -> Double -> Double |
|
416 |
relativeError d1 d2 = |
|
417 |
let delta = abs $ d1 - d2 |
|
418 |
a1 = abs d1 |
|
419 |
a2 = abs d2 |
|
420 |
greatest = max a1 a2 |
|
421 |
in if delta == 0 |
|
422 |
then 0 |
|
423 |
else delta / greatest |
Also available in: Unified diff