Skip to content

potential bug with abs #9

@symbiont-alexey-babkin

Description

@symbiont-alexey-babkin

Hi, I'm trying to create a custom bounded Decimal with Precision of 38 and Scale of 10

this is how I define my newtype for the bounded Integer that underlies that Decimal

type Scale = 10

type DefaultRounding = D.RoundHalfFromZero

newtype IntegerPrec38 = IntegerPrec38 {unIntegerPrec38 :: Integer}
  deriving (Eq, Show, Ord)
  deriving newtype (Num, Integral, Real, Enum, NFData, D.Round DefaultRounding, D.Round D.RoundHalfToZero, D.Round D.RoundHalfEven)
  deriving newtype Seri.Flat
  deriving stock Data

instance Bounded IntegerPrec38 where
  minBound = -10 ^ (38 :: Int)
  maxBound = 10 ^ (38 :: Int) - 1
  
type UnderlyingIntegral = IntegerPrec38

type D = D.Decimal DefaultRounding Scale UnderlyingIntegral

I have the subtract 1 like this from maxBound to mimic the hard bounds of Int64 for example, its minBound, negated is always larger by 1 than maxBound (due to 2s compliment encoding of signed numbers)

The bounding seems to mostly work with the bounded operations like addition, multiplication ...

I however had this failed test:

      it "overflow due to having more negative values than positive" $ do
       first displayException (abs minBound) `shouldBe` Left "arithmetic overflow"

which should pass given the bounds I specified above.

I'm catching this corner case in my user code, but I believe this should be caught at the library level and hence I think it's a bug.

Please let me know if I make any erroneous assumptions on my part or if you have a suggestion how to better use the lib for my case.

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions