Data.Int53
- Package
- purescript-int-53
- Repository
- rgrempel/purescript-int-53
Purescript's built-in Int type is restricted to 32-bit integers. However, the
Javascript runtime is capable of working with 53-bit integers. So, this module
provides an Int53 type, for cases where you want all 53 bits
(from -9,007,199,254,740,991 to 9,007,199,254,740,991).
For even larger numbers, consider purescript-bigints, or purescript-hugenums. The advantage of purescript-int-53 over those is that purescript-int-53 is likely to be faster, since it provides a "thinner" wrapper over the underlying Javascript operations.
Making an Int53
To create an Int53 from a Number, use fromNumber or, more often,
ceil, floor, round or truncate.
To create an Int53 from an Int, use fromInt.
If you're starting from a String, then there is fromString.
Using an Int53
Once you have an Int53, you can do things like add, subtract, multiply,
etc., in the usual way, since the ordinary classes for arithmetic are
implemented. It should feel pretty much like using an ordinary integer.
There are also functions for even, odd, pow,
and abs.
Converting an Int53 to something else
When you need a Number again, you can use toNumber -- and when you need
an Int, there is toInt.
To get back to a String, there is a Show instance, so you can use show.
However, that includes the Int53 tag in the resulting string, so you might
sometimes want toString instead.
The Int53Value class
There is also a class for Int53Value, which might sometimes be useful if you
have a function where you'd like to accept either an Int or an Int53.
Often you won't need this.
#Int53 Source
newtype Int53A 53-bit signed integer.
It is implemented a newtype over a Number (that is, a Javascript number),
with matters arranged so that it will always be an integer.
In case you've forgotten what all the instances mean, you've got:
Semiring: add, zero, mul, one, (+), (*)
Ring: sub, (-), negate
EuclideanRing: div, mod, (/), degree
... plus the usual Eq, Ord and Show.
There's also top and bottom from Bounded, which indicate the maximum
and minimum values available for an Int53.
Instances
#Int53Value Source
class Int53Value a whereA class which allows a function to accept eitner an Int or an Int53,
work with Int53 internally, and then return whatever type was provided.
For instance, you can do something like:
doSomethingWithIntOrInt53 :: ∀ a. (Int53Value a) => a -> a
doSomethingWithIntOrInt53 =
fromInt53 <<< doSometingWithInt53 <<< toInt53
doSomethingWithInt53 :: Int53 -> Int53
doSomethingWithInt53 = ...
This is basically for cases where only some intermediate steps need the
Int53 range -- that is, where an input in the Int range will produce an
output in the Int range, but one needs an Int53 in the middle.
So, you won't need this very often ... it's just a convenience in some cases.
Members
Instances
#fromNumber Source
fromNumber :: Number -> Maybe Int53Creates an Int53 from a Number value. The number must already be an
integer and fall within the valid range of values for the Int53 type.
Otherwise, Nothing is returned.
fromNumber Global.nan == Nothing
fromNumber 2.5 == Nothing
fromNumber 1.0e65 == Nothing
fromNumber (-1.0e65) == Nothing
fromNumber 27.0 == Just (fromInt 27)
#fromString Source
fromString :: String -> Maybe Int53Reads an Int53 from a String value. The number must parse as an integer
and fall within the valid range of values for the Int53 type, otherwise
Nothing is returned.
fromString "not a number" == Nothing
fromString "2.5" == Nothing
fromString "1.0e65" == Nothing
fromString "-1.0e65" == Nothing
fromString "27.0" == Just (fromInt 27)
fromString "27" == Just (fromInt 27)
#ceil Source
ceil :: Number -> Int53Convert a Number to an Int53, by taking the closest integer equal to or
greater than the argument. Values outside the Int53 range are clamped.
ceil 27.1 == fromInt 28
ceil 27.9 == fromInt 28
ceil (-27.1) == fromInt (-27)
ceil (-27.9) == fromInt (-27)
ceil (1.0e65) == top
ceil (-1.0e65) == bottom
#floor Source
floor :: Number -> Int53Convert a Number to an Int53, by taking the closest integer equal to or
less than the argument. Values outside the Int53 range are clamped.
floor 27.1 == fromInt 27
floor 27.9 == fromInt 27
floor (-27.1) == fromInt (-28)
floor (-27.9) == fromInt (-28)
floor (1.0e65) == top
floor (-1.0e65) == bottom
#round Source
round :: Number -> Int53Convert a Number to an Int53, by taking the nearest integer to the
argument. Values outside the Int53 range are clamped.
round 27.1 == fromInt 27
round 27.9 == fromInt 28
round (-27.1) == fromInt (-27)
round (-27.9) == fromInt (-28)
round (1.0e65) == top
round (-1.0e65) == bottom
#truncate Source
truncate :: Number -> Int53Convert a Number to an Int53, by rounding towards zero.
Values outside the Int53 range are clamped.
truncate 27.1 == fromInt 27
truncate 27.9 == fromInt 27
truncate (-27.1) == fromInt (-27)
truncate (-27.9) == fromInt (-27)
truncate (1.0e65) == top
truncate (-1.0e65) == bottom
#pow Source
pow :: Int53 -> Int53 -> Int53Raises the first argument to the power of the second argument (the exponent).
If the exponent is less than 0, then pow returns 0.
pow (fromInt 2) (fromInt 3) == (fromInt 8)
pow (fromInt 2) (fromInt 0) == (fromInt 1)
pow (fromInt 0) (fromInt 0) == (fromInt 1)
pow (fromInt 2) (fromInt (-2)) == (fromInt 0)
- Modules
- Data.
Int53
Addition is saturating:
NOTE: Due to this, Int53 doesn't actually satisfy the associativity law for addition:
Multiplication is also saturating: