<commit_before>{-# LANGUAGE CPP                        #-}
{-# LANGUAGE NoImplicitPrelude          #-}
{-# LANGUAGE PackageImports             #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

{-
  Copyright 2014 Google Inc. All rights reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-}

module Internal.Num (
    Number,
    (+),
    (-),
    (*),
    (/),
    (^),
    (>),
    (>=),
    (<),
    (<=),
    max,
    min,
    negate,
    abs,
    signum,
    truncate,
    round,
    ceiling,
    floor,
    quotient,
    remainder,
    reciprocal,
    pi,
    exp,
    sqrt,
    log,
    logBase,
    sin,
    tan,
    cos,
    asin,
    atan,
    atan2,
    acos,
    properFraction,
    even,
    odd,
    gcd,
    lcm,
    sum,
    product,
    maximum,
    minimum,
    isInteger,
    fromInteger,
    fromRational,
    fromInt,
    toInt,
    fromDouble,
    toDouble
    ) where

import qualified "base" Prelude as P
import "base" Prelude (Bool(..), (.), (==), (&&), map, not, otherwise)
import Numeric

{-|The type for numbers.

  Numbers can be positive or negative, whole or fractional.  For example, 5,
  3.2, and -10 are all values of the type Number.
-}
newtype Number = Number P.Double
    deriving (P.RealFrac, P.Real, P.Floating, P.RealFloat)

fromDouble :: P.Double -> Number
fromDouble x | P.isNaN x      = P.error "result is undefined"
             | P.isInfinite x = P.error "result is too large"
             | otherwise      = Number x

toDouble :: Number -> P.Double
toDouble (Number x) = x

fromInteger :: P.Integer -> Number
fromInteger = fromDouble . P.fromInteger

fromRational :: P.Rational -> Number
fromRational = fromDouble . P.fromRational

fromInt :: P.Int -> Number
fromInt = fromDouble . P.fromIntegral

toInt :: Number -> P.Int
toInt n | isInteger n = P.truncate (toDouble n)
        | otherwise   = P.error "a whole number is required"

instance P.Show Number where
    showsPrec p x | isInteger x = P.showsPrec p (P.truncate (toDouble x))
                  | otherwise   = showFFloat P.Nothing (toDouble x)

instance P.Eq Number where
    Number a == Number b = a == b

instance P.Num Number where
    fromInteger = fromInteger
    (+) = (+)
    (-) = (-)
    (*) = (*)
    negate = negate
    abs = abs
    signum = signum

instance P.Fractional Number where
    fromRational x = Number (P.fromRational x)
    (/) = (/)

instance P.Enum Number where
    succ = fromDouble . P.succ . toDouble
    pred = fromDouble . P.pred . toDouble
    toEnum = fromDouble . P.toEnum
    fromEnum = P.fromEnum . toDouble<commit_msg>Better formatting for numbers - format to 4 digits always, and then drop trailing zeroes after the fraction.<commit_after>{-# LANGUAGE CPP                        #-}
{-# LANGUAGE NoImplicitPrelude          #-}
{-# LANGUAGE PackageImports             #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

{-
  Copyright 2014 Google Inc. All rights reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-}

module Internal.Num (
    Number,
    (+),
    (-),
    (*),
    (/),
    (^),
    (>),
    (>=),
    (<),
    (<=),
    max,
    min,
    negate,
    abs,
    signum,
    truncate,
    round,
    ceiling,
    floor,
    quotient,
    remainder,
    reciprocal,
    pi,
    exp,
    sqrt,
    log,
    logBase,
    sin,
    tan,
    cos,
    asin,
    atan,
    atan2,
    acos,
    properFraction,
    even,
    odd,
    gcd,
    lcm,
    sum,
    product,
    maximum,
    minimum,
    isInteger,
    fromInteger,
    fromRational,
    fromInt,
    toInt,
    fromDouble,
    toDouble
    ) where

import qualified "base" Prelude as P
import "base" Prelude (Bool(..), (.), (==), (&&), map, not, otherwise)
import Numeric

{-|The type for numbers.

  Numbers can be positive or negative, whole or fractional.  For example, 5,
  3.2, and -10 are all values of the type Number.
-}
newtype Number = Number P.Double
    deriving (P.RealFrac, P.Real, P.Floating, P.RealFloat)

fromDouble :: P.Double -> Number
fromDouble x | P.isNaN x      = P.error "result is undefined"
             | P.isInfinite x = P.error "result is too large"
             | otherwise      = Number x

toDouble :: Number -> P.Double
toDouble (Number x) = x

fromInteger :: P.Integer -> Number
fromInteger = fromDouble . P.fromInteger

fromRational :: P.Rational -> Number
fromRational = fromDouble . P.fromRational

fromInt :: P.Int -> Number
fromInt = fromDouble . P.fromIntegral

toInt :: Number -> P.Int
toInt n | isInteger n = P.truncate (toDouble n)
        | otherwise   = P.error "a whole number is required"

instance P.Show Number where
    show (Number x) = stripZeros (showFFloatAlt (P.Just 4) x "")
      where stripZeros = P.reverse
                       . P.dropWhile (== '.')
                       . P.dropWhile (== '0')
                       . P.reverse

instance P.Eq Number where
    Number a == Number b = a == b

instance P.Num Number where
    fromInteger = fromInteger
    (+) = (+)
    (-) = (-)
    (*) = (*)
    negate = negate
    abs = abs
    signum = signum

instance P.Fractional Number where
    fromRational x = Number (P.fromRational x)
    (/) = (/)

instance P.Enum Number where
    succ = fromDouble . P.succ . toDouble
    pred = fromDouble . P.pred . toDouble
    toEnum = fromDouble . P.toEnum
    fromEnum = P.fromEnum . toDouble
{'fraction_non_alphanumeric': 0.10774357346874008, 'fraction_numerical': 0.004919073310060298, 'mean_word_length': 2.2691908713692945, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1765918', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1994, 'n_words': 653}