<commit_before>{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-|
    Module      :  Numeric.AERN.RealArithmetic.Basis.Double.Conversion
    Description :  conversions between Double and standard numeric types
    Copyright   :  (c) <NAME>
    License     :  BSD3

    Maintainer  :  <EMAIL>
    Stability   :  experimental
    Portability :  portable
    
    Conversions between Double and standard numeric types.

    This is a private module reexported publicly via its parent.
-}

module Numeric.AERN.RealArithmetic.Basis.Double.Conversion where

import Numeric.AERN.RealArithmetic.NumericOrderRounding

instance Convertible Integer Double where
    type ConvertEffortIndicator Integer Double = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ n
       | ndn >= n = dn
       | otherwise = dnUp
       where
       dn = fromInteger n
       ndn = floor dn
       (m, e) = decodeFloat dn
       dnUp = encodeFloat (m + 1) e 
    convertDnEff _ n 
       | ndn <= n = dn
       | otherwise = dnDn
       where
       dn = fromInteger n
       ndn = ceiling dn
       (m, e) = decodeFloat dn
       dnDn = encodeFloat (m - 1) e 
    
instance Convertible Double Integer where
    type ConvertEffortIndicator Double Integer = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ d = ceiling d
    convertDnEff _ d = floor d

instance Convertible Double Double where
    type ConvertEffortIndicator Double Double = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ d = d
    convertDnEff _ d = d

instance Convertible Rational Double where
    type ConvertEffortIndicator Rational Double = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ r
       | rdr >= r = dr
       | otherwise = drUp
       where
       rdr = toRational dr
       dr = fromRational r
       (m, e) = decodeFloat dr
       drUp = encodeFloat (m + 1) e 
    convertDnEff _ r 
       | rdr <= r = dr
       | otherwise = drDn
       where
       rdr = toRational dr
       dr = fromRational r
       (m, e) = decodeFloat dr
       drDn = encodeFloat (m - 1) e 

instance Convertible Double Rational where
    type ConvertEffortIndicator Double Rational = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ d = toRational d
    convertDnEff _ d = toRational d
<commit_msg>spacing<commit_after>{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-|
    Module      :  Numeric.AERN.RealArithmetic.Basis.Double.Conversion
    Description :  conversions between Double and standard numeric types
    Copyright   :  (c) <NAME>
    License     :  BSD3

    Maintainer  :  <EMAIL>
    Stability   :  experimental
    Portability :  portable

    Conversions between Double and standard numeric types.

    This is a private module reexported publicly via its parent.
-}

module Numeric.AERN.RealArithmetic.Basis.Double.Conversion where

import Numeric.AERN.RealArithmetic.NumericOrderRounding

instance Convertible Integer Double where
    type ConvertEffortIndicator Integer Double = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ n
       | ndn >= n = dn
       | otherwise = dnUp
       where
       dn = fromInteger n
       ndn = floor dn
       (m, e) = decodeFloat dn
       dnUp = encodeFloat (m + 1) e
    convertDnEff _ n
       | ndn <= n = dn
       | otherwise = dnDn
       where
       dn = fromInteger n
       ndn = ceiling dn
       (m, e) = decodeFloat dn
       dnDn = encodeFloat (m - 1) e

instance Convertible Double Integer where
    type ConvertEffortIndicator Double Integer = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ d = ceiling d
    convertDnEff _ d = floor d

instance Convertible Double Double where
    type ConvertEffortIndicator Double Double = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ d = d
    convertDnEff _ d = d

instance Convertible Rational Double where
    type ConvertEffortIndicator Rational Double = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ r
       | rdr >= r = dr
       | otherwise = drUp
       where
       rdr = toRational dr
       dr = fromRational r
       (m, e) = decodeFloat dr
       drUp = encodeFloat (m + 1) e
    convertDnEff _ r
       | rdr <= r = dr
       | otherwise = drDn
       where
       rdr = toRational dr
       dr = fromRational r
       (m, e) = decodeFloat dr
       drDn = encodeFloat (m - 1) e

instance Convertible Double Rational where
    type ConvertEffortIndicator Double Rational = ()
    convertDefaultEffort _ _ = ()
    convertUpEff _ d = toRational d
    convertDnEff _ d = toRational d

{'fraction_non_alphanumeric': 0.06640793445450625, 'fraction_numerical': 0.0021561017680034496, 'mean_word_length': 2.364031907179115, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3022788', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1443, 'n_words': 428}