-
Notifications
You must be signed in to change notification settings - Fork 52
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
num_rational::Ratio: Add the ability to format as a decimal #10
Comments
It'd be nice to have #1 so that you could print in full precision, but having float conversions would certainly be a good start, and probably good enough for most use cases. |
It'd be nice if we could somehow format the repeating part of the ratio, e.g. 1/22 becomes 0.0_45. Of course, this would not be very trivial to implement and the floating-point conversion is probably best for most cases. |
A related concern is infinite representations. E.g., how do you print 1/3? I think the best would be to have at least some mechanism of getting a precise string representation if possible (i.e., a method that returns |
Every rational number's fractional part is either finite or repeating. You can print |
Note however that the repeating part of |
True. We could limit it by printing sth like |
Any progress on that? I'd like to implement Gauss–Legendre algorithm to calculate Pi numbers, and I need to print the number as a decimal. I'd like to have something like |
I'm not aware that anyone has worked on this. |
I have not worked on this. |
I started to work on some small solution for personal needs recently. It's nothing fancy though - just printing decimal representation to a specified precision(eg. 42 places after separator). |
I just finished refactorization of my code. But now I'm having some problems while trying to integrate it into the I have the feeling that adding a lot of traits dependencies is not a good practice. But currently my code is using multiplication, division and modulo of two I`ll try to make one of those work(or at least compile) in the next few days. EDIT: I've managed to compile it and make it pass some new tests. Pull request is now on. |
…approximation to the fmt if it's given a Formatter with precision.
…he separate method 'as_decimal' returning String.
Did you end up putting up a PR? I had an idea - maybe this should be a method that returns an option? E.g., |
The PR is hanging with its next version - Using Changing it to use other base shouldn't be a big change, but then we should probably also change the name of the method. Any ideas? EDIT[2018-08-21]: |
Hi folks. I implemented a
Here are some examples: assert_eq!(÷_to_string(807, 31, 4).unwrap(), "26.0322");
assert!(match divide_to_string(1, 0, 1).err().unwrap() {
DivisionError::DivisionByZero => true,
_ => false
}); BigUint example: let num = "820123456789012345678901234567890123456789";
let den = "420420420420240240420240420240420420420";
let result_1024 = "1950.7222222204153880534352079149419688493160244330759069204925259490806291881834407646199555744655166719234903156227406500953778757619920899563294795746797267246703798051247915744867884912121330007705286911209791422213223230426822190127666529437572025264829201539629594175021000386486667365851813562015885600393107707737453721144405603009059457352854387023006301508907770762997683254372534811586343569328131455924964081595076622200116354403760742833073621862325616259444084808295834752749082040590201012701034118255745516514972270054835928011374231619434684843847682844123336846713100440285930668493675706096464476187809105398269815519780303174023949885603320678109566772031394249633001137109155600514761384776616827086908396960584246277151426685095767130738996420461009015758184659365258827537226581854494195009714400547427050697946126012192691851549545189173091941689299722345297086508711845865506663262915436874050594522308464492248968916100678819937355384703664061966410613989688966690413319849627099468906113991173042101218";
let asrt1 = divide_to_string(
BigUint::from_str_radix(num, 10).ok().unwrap(),
BigUint::from_str_radix(den, 10).ok().unwrap(),
1024
).ok();
assert_eq!(asrt1.unwrap(), result_1024); Here is the module itself: https://github.com/dnsl48/fraction/blob/0.4.x/src/division.rs If you are interested, I could prepare a PR. |
Fair enough. @Hazardius that's your PR, do you feel like reworking it completely? Your call. |
@dnsl48 I'm unable to work on this before this weekend, when I was planning to fix things pointed out by cuviper . |
@dnsl48, @Hazardius, to be clear, I was not making a judgement of which of you had a better approach. I haven't compared your work in any depth. I'm just hoping that as two interested parties, you can work together to come to the best implementation. |
@cuviper I wouldn't mind judging my code as poor. I think about it that way. ;) |
I've just found an issue in my code that could make overflows due to some corner cases, e.g. |
Sorry @Hazardius, my implementation is too different so I made a separate PR. It's also Hacktoberfest going on, so I feel that a separate PR would be a fair thing in this situation. |
I have not read the entire thread, but why not using something like
where you can change the fraction amount by replacing the format string? |
@rudolfschmidt because I may want to have 100 or more significant digits in answer, which is not possible with f64, decimal or any other standard type. |
I agree that the result is just an approximation, maybe enough for visual purposes in most cases. I am open to better solutions that I can apply to my own code as well. In my Java days, I used to convert into double that should be an f64 in Rust, divided and printed the output. |
Let's me show some example. Assume we are writing SuperPI and use You can always cast to |
If you need it lossless, that functionality is implemented in the fraction module. type D = fraction::Decimal;
let result = D::from(0.5) / D::from(0.3);
assert_eq!(format!("{:.4}", result), "1.6666"); It's open sourced. Anyone willing can check out the code and make a PR for this repo, if you'd like. |
I'm interested in implementing this feature and creating a PR, however, I have a different idea on the API. I prefer to integrate the formatting into My proposal to abuse
To use If this idea could get some likes, I'm happy to try to implement it and make a PR. |
A reasonable modification to my proposal above is to print
|
IMHO, since it is acceptable in some places to put parentheses around the repeatand, I would personally prefer that over using underscores. I feel like abusing Unicode or other methods are probably less than ideal, but this at least will help indicate the repeatand just as well. |
I didn't know that using parentheses is a common way, in that case we can definitely use parentheses to mark the cycle
|
The repeating part should be inside the parentheses, so |
Multiple repeats are not necessary, I put four 3's here just to comply with the specified number of digits. It might be better to just have |
Honestly, handling requests for excessive precision, it may be best to only parenthesise the first group. So, for example, if someone requests 8 digits of precision for 1/7, then we could display Another potential thing is whether we should still show the repeating part for non-repeating decimals in certain scenarios, e.g. should |
Yeah I think |
Rather than abusing the format traits, would it not be better to use methods that return wrappers that control how a |
I agree that we shouldn't abuse the format traits. I like the idea of a customizable wrapper. Some folks will want plain decimal precision with rounding, and others might want precisely-represented repetition -- the latter could even be gotten by further methods to extract parts. (e.g. |
I prefer to use How about directly use default format with precision to display the digits, as the precision parameter is unused in default format
To retrieve the non-repeating and repeating part, we can provide methods directly for |
From @joshlf on December 21, 2017 20:10
Ratio
'sDisplay
implementation formats as a fraction (e.g.,17/42
). It would be useful if there were a way to format instead as a decimal (e.g.,0.4047619048
).Copied from original issue: rust-num/num#358
The text was updated successfully, but these errors were encountered: