velnias75 / rational Goto Github PK
View Code? Open in Web Editor NEWA C++ rational (fraction) template class
License: GNU General Public License v3.0
A C++ rational (fraction) template class
License: GNU General Public License v3.0
By Melchoir via Reddit
A Gaussian integer is a complex number where the real and imaginary parts are both integers. In other words, a thing of the form a+bi where a and b are integers. I think it can even be implemented as std::complex. Some salient points are:
Gaussian integers and Gaussian rationals are both interesting types. It would be nice if one could define a Gaussian integer type and, with a little extra effort, get the rational type by applying your template to it. In the terminology of pure mathematics, your template implements a field of fractions, which is meaningful for any integral domain, including the ordinary integers.
I understand the point about designing the template for completeness. My point is that as a potential user of the library, I would be worried that, for example, the implementation of Rational::operator+= secretly performs some normalization that depends on the existence of T::operator<. This information is impossible to get just from reading the code, and I would dread having to test it out myself, only to pull my hair out deciphering the compiler's error messages. Even if it compiles, I would be worried that it makes some assumption about the behavior of the < operator that is violated by my storage type, and the compiler wouldn't even be able to detect that. This might very well be the case for the Gaussian integers, which I would count as an "exotic" storage type.
Eh... I would try adding support for Gaussian integers myself, but it feels like it might be a lot of work. :)
From Reddit:
Very cool!
For documentation: It would be good to describe more explicitly the range of storage types that are expected to work. Can I use polynomials and get rational functions? Can I use Gaussian integers and get Gaussian rationals? Ideally, T would not need to have comparison methods as long as I never reference Rational's comparison methods, but maybe they're needed internally for other purposes.
If the above are meant to be supported, it would be good to see a couple of test cases, too. Hopefully, the exotic storage types wouldn't have to be fully fleshed out if they're just for testing.
Also for test cases: Make sure you've handled the most negative value M of a signed binary integer type, since -M = M, which might cause trouble in lots of places.
Back to docs, it would be good to describe what it means to support GMP. What does one gain by using a Rational of GMP integers, rather than a native GMP rational?
On the implementation side, I don't think you need to declare your own [copy, move] [constructor, assignment operator]s or destructor. It looks like your implementations aren't doing anything special.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.