-
Notifications
You must be signed in to change notification settings - Fork 262
Added function to show Rational at a decimal precision #2945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,9 +8,33 @@ | |||||||||
|
|
||||||||||
| module Data.Rational.Show where | ||||||||||
|
|
||||||||||
| open import Data.Integer.Base using (ℤ; +_; ∣_∣) | ||||||||||
| import Data.Integer.Show as ℤ using (show) | ||||||||||
| open import Data.Rational.Base using (ℚ; ↥_; ↧_) | ||||||||||
| open import Data.Nat.Base using (ℕ; zero; suc) | ||||||||||
| open import Data.Nat.Show using (toDigitChar) | ||||||||||
| open import Data.Product.Base using (_×_; _,_) | ||||||||||
| open import Data.Rational.Base | ||||||||||
| using (ℚ; ↥_; ↧_; _/_; _*_; truncate; fracPart) | ||||||||||
| open import Data.String.Base using (String; _++_) | ||||||||||
| open import Data.String using (fromVec) | ||||||||||
| open import Data.Vec.Base using (Vec; []; _∷_; map) | ||||||||||
|
|
||||||||||
| show : ℚ → String | ||||||||||
| show p = ℤ.show (↥ p) ++ "/" ++ ℤ.show (↧ p) | ||||||||||
|
|
||||||||||
| -- The integer part of q, | ||||||||||
| -- followed by the first n digits of its decimal representation | ||||||||||
| atPrecision : (n : ℕ) → ℚ → ℤ × Vec ℕ n | ||||||||||
| atPrecision n q = (truncate q , decimalPart n ((+ 10 / 1) * (fracPart q))) | ||||||||||
| where | ||||||||||
| decimalPart : (n : ℕ) → ℚ → Vec ℕ n | ||||||||||
| decimalPart zero q = [] | ||||||||||
| -- fracPart q is a (non-negative) proper fraction, | ||||||||||
| -- and 0 ≤ num < den implies 0 ≤ 10 num/den < 10, | ||||||||||
| -- so everything but position 0 is a proper digit | ||||||||||
| decimalPart (suc n) q | ||||||||||
| = ∣ truncate q ∣ ∷ decimalPart n ((+ 10 / 1) * (fracPart q)) | ||||||||||
|
|
||||||||||
| showAtPrecision : ℕ → ℚ → String | ||||||||||
| showAtPrecision n q with atPrecision n q | ||||||||||
| ... | (int , dec) = ℤ.show int ++ "." ++ (fromVec (map toDigitChar dec)) | ||||||||||
|
Comment on lines
+39
to
+40
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things:
For the first:
Suggested change
For the second, I guess I'm not so prescriptive ;-) |
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,15 @@ | |
| module Data.Rational.Unnormalised.Show where | ||
|
|
||
| import Data.Integer.Show as ℤ using (show) | ||
| open import Data.Rational.Unnormalised.Base using (ℚᵘ; ↥_; ↧_) | ||
| open import Data.Nat.Base using (ℕ) | ||
| open import Data.Rational.Base using (fromℚᵘ) | ||
| import Data.Rational.Show as ℚ using (showAtPrecision) | ||
| open import Data.Rational.Unnormalised.Base | ||
| using (ℚᵘ; ↥_; ↧_) | ||
| open import Data.String.Base using (String; _++_) | ||
|
|
||
| show : ℚᵘ → String | ||
| show p = ℤ.show (↥ p) ++ "/" ++ ℤ.show (↧ p) | ||
|
|
||
| showAtPrecision : ℕ → ℚᵘ → String | ||
| showAtPrecision n q = ℚ.showAtPrecision n (fromℚᵘ q) | ||
|
Comment on lines
+22
to
+23
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for doing this. I realise it violates the 'usual' heuristics of the dependency graph ( |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine on a first pass, ... and premature optimisation is the root of all evil ;-)