module Network.Mux.DeltaQ.TraceStats
  ( step
  , OneWayDeltaQSample (..)
  , constructSample
  , StatsA
  , initialStatsA
  ) where

import Data.IntMap.Strict (IntMap)
import Data.IntMap.Strict qualified as IM
import Data.Word (Word32)

import Control.Monad.Class.MonadTime.SI
import Network.Mux.DeltaQ.TraceStatsSupport
import Network.Mux.DeltaQ.TraceTypes
import Network.Mux.Types

-- the per observation processing step
step :: RemoteClockModel        -- ^ Remote clock timestamp
     -> Time                    -- ^ Local clock timestamp
     -> Int                     -- ^ the number of octets in the
                                --   observed outcome
     -> StatsA                  -- ^ accumulation state
     -> (Maybe OneWayDeltaQSample, StatsA)
step :: RemoteClockModel
-> Time -> Key -> StatsA -> (Maybe OneWayDeltaQSample, StatsA)
step RemoteClockModel
remoteTS Time
localTS Key
obsSize StatsA
s =
  case StatsA -> Maybe (Word32, Time)
referenceTimePoint StatsA
s of
    Maybe (Word32, Time)
Nothing -> -- first observation this sample period
      RemoteClockModel
-> Time -> Key -> StatsA -> (Maybe OneWayDeltaQSample, StatsA)
step RemoteClockModel
remoteTS Time
localTS Key
obsSize
          (StatsA
s { referenceTimePoint = Just $! (unRemoteClockModel remoteTS, localTS)
             , nextSampleAt       = sampleInterval `addTime` localTS
             , timeLastObs        = localTS -- for single observation in sample case
             })
    Just (Word32, Time)
refTimePoint | Time
localTS Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
<= StatsA -> Time
nextSampleAt StatsA
s ->   -- still in a sample period
      let transitTime :: SISec
transitTime = (Word32, Time) -> RemoteClockModel -> Time -> SISec
calcTransitTime (Word32, Time)
refTimePoint RemoteClockModel
remoteTS Time
localTS
      in (Maybe OneWayDeltaQSample
forall a. Maybe a
Nothing, StatsA -> Time -> Key -> SISec -> StatsA
recordObservation StatsA
s Time
localTS Key
obsSize SISec
transitTime)
    Maybe (Word32, Time)
_ -> -- need to start next sample period
      let sample :: OneWayDeltaQSample
sample  = StatsA -> OneWayDeltaQSample
constructSample StatsA
s
          (Maybe OneWayDeltaQSample
_, StatsA
s') = RemoteClockModel
-> Time -> Key -> StatsA -> (Maybe OneWayDeltaQSample, StatsA)
step RemoteClockModel
remoteTS Time
localTS Key
obsSize StatsA
initialStatsA
      in (OneWayDeltaQSample -> Maybe OneWayDeltaQSample
forall a. a -> Maybe a
Just OneWayDeltaQSample
sample, StatsA
s')

-- Calculate the transit time by transforming the remotely reported
-- emit time into local clock domain then calculating differences.
calcTransitTime :: (Word32, Time)
                -> RemoteClockModel
                -> Time
                -> SISec
calcTransitTime :: (Word32, Time) -> RemoteClockModel -> Time -> SISec
calcTransitTime (Word32
remoteRefTS, Time
localRefTS) RemoteClockModel
remoteTS' Time
localTS
  = let remoteTS :: Word32
remoteTS
          = RemoteClockModel -> Word32
unRemoteClockModel RemoteClockModel
remoteTS'
        remoteClockDiffAsTimeDiff :: Word32 -> DiffTime
remoteClockDiffAsTimeDiff
          = (DiffTime
remoteClockPrecision DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
*) (DiffTime -> DiffTime)
-> (Word32 -> DiffTime) -> Word32 -> DiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> DiffTime
forall a. Fractional a => Rational -> a
fromRational (Rational -> DiffTime)
-> (Word32 -> Rational) -> Word32 -> DiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral
        correctedEmitTime :: Time
correctedEmitTime
          | Word32
remoteTS Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word32
remoteRefTS
          = (Word32 -> DiffTime
remoteClockDiffAsTimeDiff (Word32 -> DiffTime) -> Word32 -> DiffTime
forall a b. (a -> b) -> a -> b
$ Word32
remoteTS Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
remoteRefTS)
            DiffTime -> Time -> Time
`addTime` Time
localRefTS
          | Bool
otherwise -- wrap has occurred
          = (Word32 -> DiffTime
remoteClockDiffAsTimeDiff (Word32 -> DiffTime) -> Word32 -> DiffTime
forall a b. (a -> b) -> a -> b
$ Word32
forall a. Bounded a => a
maxBound Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- (Word32
remoteRefTS Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
remoteTS))
            DiffTime -> Time -> Time
`addTime` Time
localRefTS
    in Float -> SISec
S (Float -> SISec) -> Float -> SISec
forall a b. (a -> b) -> a -> b
$! Rational -> Float
forall a. Fractional a => Rational -> a
fromRational (DiffTime -> Rational
forall a. Real a => a -> Rational
toRational (Time
localTS Time -> Time -> DiffTime
`diffTime` Time
correctedEmitTime))

recordObservation :: StatsA -> Time -> Int -> SISec -> StatsA
recordObservation :: StatsA -> Time -> Key -> SISec -> StatsA
recordObservation StatsA
s Time
obsTime Key
obsSize SISec
transitTime
  = let f :: Maybe PerSizeRecord -> Maybe PerSizeRecord
f Maybe PerSizeRecord
Nothing  = PerSizeRecord -> Maybe PerSizeRecord
forall a. a -> Maybe a
Just (PerSizeRecord -> Maybe PerSizeRecord)
-> PerSizeRecord -> Maybe PerSizeRecord
forall a b. (a -> b) -> a -> b
$! SISec -> PerSizeRecord
makePerSizeRecord SISec
transitTime
        f (Just PerSizeRecord
a) = PerSizeRecord -> Maybe PerSizeRecord
forall a. a -> Maybe a
Just (PerSizeRecord -> Maybe PerSizeRecord)
-> PerSizeRecord -> Maybe PerSizeRecord
forall a b. (a -> b) -> a -> b
$! SISec -> PerSizeRecord
makePerSizeRecord SISec
transitTime PerSizeRecord -> PerSizeRecord -> PerSizeRecord
forall a. Semigroup a => a -> a -> a
<> PerSizeRecord
a
    in StatsA
s { timeLastObs     = obsTime
         , numObservations = succ (numObservations s)
         , observables     = IM.alter f obsSize (observables s)
         }

-- This might benefit from some strictness analysis, what are the
-- likely usage patterns?, do we want a single use collapse the
-- collective set of thunks or not?
--
-- There is the issue of "bias" (in its statistical meaning) here. The
-- approach here is pragmatic, we are going to use the uncorrected
-- sample standard deviation here as it has a defined solution for a
-- single sample.
--
-- Given that the consumer of this statistic also has access to the
-- population size, they could reconstruct the underlying measures and
-- take it from there.
--
-- We return `NaN` for the appropriate statistics when the population
-- is empty.
constructSample :: StatsA -> OneWayDeltaQSample
constructSample :: StatsA -> OneWayDeltaQSample
constructSample StatsA
sa = OneWaySample
  { duration :: Double
duration       = Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Rational -> Double)
-> (DiffTime -> Rational) -> DiffTime -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiffTime -> Rational
forall a. Real a => a -> Rational
toRational (DiffTime -> Double) -> DiffTime -> Double
forall a b. (a -> b) -> a -> b
$
                     DiffTime
-> ((Word32, Time) -> DiffTime) -> Maybe (Word32, Time) -> DiffTime
forall b a. b -> (a -> b) -> Maybe a -> b
maybe DiffTime
0 (\(Word32
_,Time
a) -> StatsA -> Time
timeLastObs StatsA
sa Time -> Time -> DiffTime
`diffTime` Time
a)
                             (StatsA -> Maybe (Word32, Time)
referenceTimePoint StatsA
sa)
  , sumPackets :: Key
sumPackets     = Key
population
  , sumTotalSDU :: Key
sumTotalSDU    = Key
totalSDUOctets
  , estDeltaQS :: Double
estDeltaQS     = Double -> Double
normCheck Double
dQSEst
  , estDeltaQVMean :: Double
estDeltaQVMean = Double -> Double
normCheck (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double
vSum Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
pop
  , estDeltaQVVar :: Double
estDeltaQVVar  = Double -> Double
normCheck (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ (Double
vSum2 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
vSum Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
vSum Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
pop) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
pop
  , estR :: Double
estR           = Double -> Double
normCheck Double
rEst
  , sizeDist :: String
sizeDist       = [(Key, Key, Float)] -> String
forall a. Show a => a -> String
show [ (Key
a,PerSizeRecord -> Key
count PerSizeRecord
b, let S Float
mtt = PerSizeRecord -> SISec
minTransitTime PerSizeRecord
b in Float
mtt)
                          | (Key
a, PerSizeRecord
b) <- IntMap PerSizeRecord -> [(Key, PerSizeRecord)]
forall a. IntMap a -> [(Key, a)]
IM.toAscList (StatsA -> IntMap PerSizeRecord
observables StatsA
sa)
                          , PerSizeRecord -> Key
count PerSizeRecord
b Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
> Key
0]
  }
  where
    -- the sample population size
    population :: Key
population = StatsA -> Key
numObservations StatsA
sa
    pop :: Double
pop        = Key -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Key
population
    -- Handle the empty population condition
    normCheck :: Double -> Double
normCheck Double
x
      | IntMap PerSizeRecord -> Key
forall a. IntMap a -> Key
IM.size (StatsA -> IntMap PerSizeRecord
observables StatsA
sa) Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
> Key
1 = Double
x
      | Bool
otherwise                    = Double
nan
    -- gather the data for the G,S estimations
    (Key
totalSDUOctets, [(Key, SISec)]
minSRev)
      = (Key
 -> PerSizeRecord -> (Key, [(Key, SISec)]) -> (Key, [(Key, SISec)]))
-> (Key, [(Key, SISec)])
-> IntMap PerSizeRecord
-> (Key, [(Key, SISec)])
forall a b. (Key -> a -> b -> b) -> b -> IntMap a -> b
IM.foldrWithKey Key
-> PerSizeRecord -> (Key, [(Key, SISec)]) -> (Key, [(Key, SISec)])
accum (Key
0, []) (IntMap PerSizeRecord -> (Key, [(Key, SISec)]))
-> IntMap PerSizeRecord -> (Key, [(Key, SISec)])
forall a b. (a -> b) -> a -> b
$ StatsA -> IntMap PerSizeRecord
observables StatsA
sa
    accum :: Key
-> PerSizeRecord -> (Key, [(Key, SISec)]) -> (Key, [(Key, SISec)])
accum Key
nOctets PerSizeRecord
psr (Key
sumSize, [(Key, SISec)]
minS)
      = ( Key
sumSize Key -> Key -> Key
forall a. Num a => a -> a -> a
+ (PerSizeRecord -> Key
count PerSizeRecord
psr) Key -> Key -> Key
forall a. Num a => a -> a -> a
* Key
nOctets
        , (Key
nOctets, PerSizeRecord -> SISec
minTransitTime PerSizeRecord
psr) (Key, SISec) -> [(Key, SISec)] -> [(Key, SISec)]
forall a. a -> [a] -> [a]
: [(Key, SISec)]
minS)
    -- fit a line to get the G,S estimation
    (Double
dQGEst, Double
dQSEst, Double
rEst) = [(Key, SISec)] -> (Double, Double, Double)
estimateGS [(Key, SISec)]
minSRev
    -- normalise all the observations
    normalisedObservations :: IntMap PerSizeRecord
normalisedObservations
      = let norm :: a -> SISec
norm a
n = Float -> SISec
S (Float -> SISec) -> (Double -> Float) -> Double -> SISec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Float
forall a. Fractional a => Rational -> a
fromRational (Rational -> Float) -> (Double -> Rational) -> Double -> Float
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Rational
forall a. Real a => a -> Rational
toRational
                     (Double -> SISec) -> Double -> SISec
forall a b. (a -> b) -> a -> b
$ Double
dQGEst Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (a -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
dQSEst
        in (Key -> PerSizeRecord -> PerSizeRecord)
-> IntMap PerSizeRecord -> IntMap PerSizeRecord
forall a b. (Key -> a -> b) -> IntMap a -> IntMap b
IM.mapWithKey (\Key
k -> SISec -> PerSizeRecord -> PerSizeRecord
normalisePSR (Key -> SISec
forall {a}. Integral a => a -> SISec
norm Key
k)) (StatsA -> IntMap PerSizeRecord
observables StatsA
sa)
    -- calculate the total population V stats
    (Double
vSum, Double
vSum2)
      = let S  Float
v  = SISec
vSum'
            S2 Float
v2 = SISec2
vSum2'
        in (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Rational -> Double) -> (Float -> Rational) -> Float -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Rational
forall a. Real a => a -> Rational
toRational (Float -> Double) -> Float -> Double
forall a b. (a -> b) -> a -> b
$ Float
v, Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Rational -> Double) -> (Float -> Rational) -> Float -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Rational
forall a. Real a => a -> Rational
toRational (Float -> Double) -> Float -> Double
forall a b. (a -> b) -> a -> b
$ Float
v2)
    (SISec
vSum', SISec2
vSum2')
      = (PerSizeRecord -> (SISec, SISec2) -> (SISec, SISec2))
-> (SISec, SISec2) -> IntMap PerSizeRecord -> (SISec, SISec2)
forall a b. (a -> b -> b) -> b -> IntMap a -> b
IM.foldr PerSizeRecord -> (SISec, SISec2) -> (SISec, SISec2)
vCalc (SISec
0,SISec2
0) IntMap PerSizeRecord
normalisedObservations
    vCalc :: PerSizeRecord -> (SISec, SISec2) -> (SISec, SISec2)
vCalc PerSizeRecord
psr (SISec
x, SISec2
x2)
      = (SISec
x SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
+ PerSizeRecord -> SISec
sumTransitTime PerSizeRecord
psr, SISec2
x2 SISec2 -> SISec2 -> SISec2
forall a. Num a => a -> a -> a
+ PerSizeRecord -> SISec2
sumTransitTimeSq PerSizeRecord
psr)

-- | One way measurement for interval. Note that the fields are lazy
--   here so that only calculation necessary to satisfy strictness of
--   use occurs.
data OneWayDeltaQSample = OneWaySample
  { OneWayDeltaQSample -> Double
duration       :: Double -- SI Seconds of activity captured
  , OneWayDeltaQSample -> Key
sumPackets     :: Int
  , OneWayDeltaQSample -> Key
sumTotalSDU    :: Int
  , OneWayDeltaQSample -> Double
estDeltaQS     :: Double -- octets per second
  , OneWayDeltaQSample -> Double
estDeltaQVMean :: Double -- SI Seconds
  , OneWayDeltaQSample -> Double
estDeltaQVVar  :: Double
  , OneWayDeltaQSample -> Double
estR           :: Double -- R estimate
  , OneWayDeltaQSample -> String
sizeDist       :: String -- temporary to show size distribution
  }

-- | Statistics accumulator. Strict evaluation used to keep the memory
--   footprint strictly bounded.
data StatsA = StatsA
  { -- per sample
    StatsA -> Maybe (Word32, Time)
referenceTimePoint :: !(Maybe (Word32, Time))
  , StatsA -> Time
nextSampleAt       :: !Time
    -- per observation
  , StatsA -> Key
numObservations    :: !Int
  , StatsA -> Time
timeLastObs        :: !Time
  , StatsA -> IntMap PerSizeRecord
observables        :: !(IntMap PerSizeRecord)
  }

-- This _may_ not be the best representation, but it does appear to be
-- an adequate one. There are known issues with numerical stability for
-- this representation approach in floating point arithmetic where the
-- values being measured are "large" and the variability in the sampled
-- population is "small" (i.e the inherent rounding effect of floating
-- point arithmetic has an effect).
--
-- This is very unlikely to cause an issue here as:
--
--   a) the modulo model of the RemoteClockModel (and hence the
--   establishment of a clock reference offset for a given sample)
--   means that we are only ever recording differences - thus any
--   absolute clock differences get factored out.
--
--   b) the transit delay for a measurement will be small, (probably
--   not even credible) worst case ~10^3 / 10^4 seconds, the finite
--   mantissa of IEEE754
--   (https://en.wikipedia.org/wiki/IEEE_754#Representation_and_encoding_in_memory)
--   even for 32 bit representation (24bits / 7.22 decimal digits)
--   represents an bound on the inherent measured population
--   variability.
--
--   c) longer term clock drift is covered here by the re-establishing
--   of the clock reference offsets every sampling period. Given a
--   reasonable sampling period (say 10 seconds) clock drift ( <
--   100ppm) can't amount to a significant error over such a period.
--
-- To conclude, reasonable model of delay is < 1second, the precision
-- of delay measurement is 10^-6 - this all fits nicely within a
-- IEEE754 32bit representation with its 7.22 decimal digit
-- mantissa. Haskell `Float`s are adequate for this purpose.

data PerSizeRecord = PSR
  { PerSizeRecord -> SISec
minTransitTime   :: !SISec
  , PerSizeRecord -> Key
count            :: !Int
  , PerSizeRecord -> SISec
sumTransitTime   :: !SISec
  , PerSizeRecord -> SISec2
sumTransitTimeSq :: !SISec2
  }

instance Semigroup PerSizeRecord where
  PerSizeRecord
a <> :: PerSizeRecord -> PerSizeRecord -> PerSizeRecord
<> PerSizeRecord
b = PSR { minTransitTime :: SISec
minTransitTime   = (PerSizeRecord -> SISec
minTransitTime PerSizeRecord
a) SISec -> SISec -> SISec
forall a. Ord a => a -> a -> a
`min` (PerSizeRecord -> SISec
minTransitTime PerSizeRecord
b)
               , count :: Key
count            = PerSizeRecord -> Key
count PerSizeRecord
a Key -> Key -> Key
forall a. Num a => a -> a -> a
+ PerSizeRecord -> Key
count PerSizeRecord
b
               , sumTransitTime :: SISec
sumTransitTime   = PerSizeRecord -> SISec
sumTransitTime PerSizeRecord
a SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
+ PerSizeRecord -> SISec
sumTransitTime PerSizeRecord
b
               , sumTransitTimeSq :: SISec2
sumTransitTimeSq = PerSizeRecord -> SISec2
sumTransitTimeSq PerSizeRecord
a SISec2 -> SISec2 -> SISec2
forall a. Num a => a -> a -> a
+ PerSizeRecord -> SISec2
sumTransitTimeSq PerSizeRecord
b
               }

-- | Normalise given the calculated G,S for the size
normalisePSR :: SISec -> PerSizeRecord -> PerSizeRecord
normalisePSR :: SISec -> PerSizeRecord -> PerSizeRecord
normalisePSR SISec
norm PerSizeRecord
psr
  = let adj :: SISec
adj  = (Key -> SISec
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PerSizeRecord -> Key
count PerSizeRecord
psr) SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
* SISec
norm)
        stt' :: SISec
stt' = (PerSizeRecord -> SISec
sumTransitTime PerSizeRecord
psr) SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
- SISec
adj
        ttt :: SISec -> SISec -> SISec2
ttt (S Float
a) (S Float
b)
             = Float -> SISec2
S2 (Float -> SISec2) -> Float -> SISec2
forall a b. (a -> b) -> a -> b
$ Float
a Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
b
    in PerSizeRecord
psr { minTransitTime   = minTransitTime   psr - norm
           , sumTransitTime   = stt'
           , sumTransitTimeSq = sumTransitTimeSq psr
                                - norm `ttt` (2 * stt' + norm)
           }


-- | Initial StatsA
initialStatsA :: StatsA
initialStatsA :: StatsA
initialStatsA = StatsA
  { referenceTimePoint :: Maybe (Word32, Time)
referenceTimePoint = Maybe (Word32, Time)
forall a. Maybe a
Nothing
  , nextSampleAt :: Time
nextSampleAt       = Time
noTime
  , numObservations :: Key
numObservations    = Key
0
  , timeLastObs :: Time
timeLastObs        = Time
noTime
  , observables :: IntMap PerSizeRecord
observables        = IntMap PerSizeRecord
forall a. IntMap a
IM.empty
  }
  where
    noTime :: Time
noTime = DiffTime -> Time
Time DiffTime
0

makePerSizeRecord :: SISec -> PerSizeRecord
makePerSizeRecord :: SISec -> PerSizeRecord
makePerSizeRecord SISec
tt = PSR
  { minTransitTime :: SISec
minTransitTime   = SISec
tt
  , count :: Key
count            = Key
1
  , sumTransitTime :: SISec
sumTransitTime   = SISec
tt
  , sumTransitTimeSq :: SISec2
sumTransitTimeSq = SISec -> SISec2
squareSISec SISec
tt
  }

-- May want to make this a configuration variable

-- NOTE this interval must be less than the wrap around time of the
-- `RemoteClockModel`. The remote clock model has a precision of
-- `remoteClockPrecision`.
sampleInterval :: DiffTime
sampleInterval :: DiffTime
sampleInterval = DiffTime -> DiffTime
check DiffTime
10
  where
    check :: DiffTime -> DiffTime
check DiffTime
n
     | DiffTime
n DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
> DiffTime
0 Bool -> Bool -> Bool
&& DiffTime
n DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< DiffTime
wrapInterval
       = DiffTime
n
     | Bool
otherwise
       = String -> DiffTime
forall a. HasCallStack => String -> a
error String
"Infeasible sampleInterval"
    wrapInterval :: DiffTime
wrapInterval
      = DiffTime
remoteClockPrecision DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
* (Word32 -> DiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32 -> DiffTime) -> Word32 -> DiffTime
forall a b. (a -> b) -> a -> b
$ RemoteClockModel -> Word32
unRemoteClockModel RemoteClockModel
forall a. Bounded a => a
maxBound)

nan :: Double
nan :: Double
nan = Double
0Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0