Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
58 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"postinstall": "bower install", | ||
"build": "pulp build", | ||
"test": "pulp test", | ||
"build": "spago build", | ||
"spago-build": "spago build", | ||
"test": "spago -x ./test.dhall test", | ||
"spago-test": "spago -x ./test.dhall test" | ||
}, | ||
"devDependencies": { | ||
"big-integer": "^1.6.51", | ||
"bower": "^1.8.14", | ||
"pulp": "^16.0.1", | ||
"purescript": "^0.15.2", | ||
"purs-tidy": "^0.9.2", | ||
"purescript": "^0.15.8", | ||
"spago": "^0.20.9" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
let upstream = | ||
https://github.com/purescript/package-sets/releases/download/psc-0.15.2-20220706/packages.dhall | ||
sha256:7a24ebdbacb2bfa27b2fc6ce3da96f048093d64e54369965a2a7b5d9892b6031 | ||
https://github.com/purescript/package-sets/releases/download/psc-0.15.8-20230503/packages.dhall | ||
sha256:b0ca02f2efb206465b499ee847f174212be0c8c2e031044b7bd192d5294f22cb | ||
|
||
in upstream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,60 @@ | ||
module Data.Rational | ||
( Rational | ||
, (%) | ||
, toNumber | ||
, fromInt | ||
, module Data.Ratio | ||
, fromBigInt | ||
, numerator | ||
, denominator | ||
, class ToRational | ||
, toRational | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Int as Int | ||
import Data.Ratio (Ratio, denominator, numerator, (%)) | ||
import Data.Ratio as Ratio | ||
import JS.BigInt (BigInt) | ||
import JS.BigInt as BigInt | ||
|
||
type Rational = Ratio Int | ||
newtype Rational = Rational (Ratio.Ratio BigInt) | ||
|
||
derive newtype instance Eq Rational | ||
derive newtype instance Ord Rational | ||
derive newtype instance Show Rational | ||
derive newtype instance Semiring Rational | ||
derive newtype instance Ring Rational | ||
derive newtype instance CommutativeRing Rational | ||
derive newtype instance EuclideanRing Rational | ||
derive newtype instance DivisionRing Rational | ||
|
||
toNumber :: Rational -> Number | ||
toNumber x = Int.toNumber (numerator x) / Int.toNumber (denominator x) | ||
toNumber (Rational x) = BigInt.toNumber (Ratio.numerator x) / BigInt.toNumber (Ratio.denominator x) | ||
|
||
fromInt :: Int -> Rational | ||
fromInt i = i % 1 | ||
fromInt i = Rational $ Ratio.reduce (BigInt.fromInt i) (BigInt.fromInt 1) | ||
|
||
fromBigInt :: BigInt -> Rational | ||
fromBigInt i = Rational $ Ratio.reduce i (BigInt.fromInt 1) | ||
|
||
numerator :: Rational -> BigInt | ||
numerator (Rational x) = Ratio.numerator x | ||
|
||
denominator :: Rational -> BigInt | ||
denominator (Rational x) = Ratio.denominator x | ||
|
||
class | ||
ToRational a | ||
where | ||
toRational :: a -> a -> Rational | ||
|
||
instance ToRational Int | ||
where | ||
toRational a b = Rational $ Ratio.reduce (BigInt.fromInt a) (BigInt.fromInt b) | ||
|
||
instance ToRational BigInt | ||
where | ||
toRational a b = Rational $ Ratio.reduce a b | ||
|
||
infixl 7 toRational as % |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters