https://github.com/rufflewind/sci-ratio
Parsing and pretty-printing of exact rational numbers with arbitrarily large exponents.
https://github.com/rufflewind/sci-ratio
Last synced: 6 months ago
JSON representation
Parsing and pretty-printing of exact rational numbers with arbitrarily large exponents.
- Host: GitHub
- URL: https://github.com/rufflewind/sci-ratio
- Owner: Rufflewind
- License: mit
- Created: 2014-08-07T23:17:33.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2021-06-07T06:05:27.000Z (about 5 years ago)
- Last Synced: 2024-07-14T09:21:46.718Z (almost 2 years ago)
- Language: Haskell
- Homepage:
- Size: 70.3 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
sci-ratio [![Build status][ci]][ca]
=================================
**Quick links:** [installation](#installation), [documentation][doc],
[versioning policy][pvp].
This package provides utilities for parsing and pretty-printing exact rational
numbers with arbitrarily large exponents. Numbers are represented using the
`SciRatio a b` type, which can be thought of as a rational number combined
with a power of ten:
```
r .^ e == r * 10 ^^ e
```
The primary advantage of this data type is that it's capable of storing
extremely large exponents without issues, although *performing arithmetic* on
them is not guaranteed to be efficient in some cases (e.g. adding numbers with
wildly differing exponents).
The parsers accept a wide variety of formats:
```hs
> readSciRational "-0.0e+3" -- result: Just ((0 % 1) .^ 0)
> readSciRational "0.25e+2" -- result: Just ((25 % 1) .^ 0)
> readSciRational "-1.0e-1" -- result: Just (((-1) % 1) .^ (-1))
> readSciRational "5.0e+20/6.e0" -- result: Just ((25 % 3) .^ 19)
> readSciRational "0xfeedface" -- result: Just ((4277009102 % 1) .^ 0)
> readSciRational "1e99999999" -- result: Just ((1 % 1) .^ 99999999)
```
Numbers can be pretty-printed in a compact format:
```hs
> showSciRational (-0.0e+3) -- result: "0"
> showSciRational (0.25e+2) -- result: "25"
> showSciRational (-1.0e-1) -- result: "-.1"
> showSciRational (5.0e+20 / 6) -- result: "2.5e20/3"
> showSciRational (0xfeedface) -- result: "4277009102"
> showSciRational (1 .^ 99999999) -- result: "1e99999999"
```
Installation
------------
The package is available on [Hackage][doc]:
```sh
cabal install sci-ratio
```
[ca]: https://github.com/Rufflewind/sci-ratio/actions/workflows/build.yml
[ci]: https://github.com/Rufflewind/sci-ratio/actions/workflows/build.yml/badge.svg
[doc]: https://hackage.haskell.org/package/sci-ratio
[pvp]: https://gist.github.com/Rufflewind/03f4e03f7cfa52b8f07d