|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi, I need a simple class implementing rational numbers as fractions of integers, together with arithmetic operations on such fractions, checks for overflow, etc. Any suggestions? L.B. !!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!! *-------------------------------------------------------------------* | Dr. Leslaw Bieniasz, | | Institute of Physical Chemistry of the Polish Academy of Sciences,| | Department of Complex Systems and Chemical | | Processing of Information | | ul. Niezapominajek 8, 30-239 Cracow, Poland. | | tel. (personal) +48 (12) 6395212 | | tel./fax. (secretariat) +48 (12) 4251923 | | E-mail: nbbienia@cyf-kr.edu.pl | *-------------------------------------------------------------------* | Interested in Computational Electrochemistry? | | Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia | *-------------------------------------------------------------------* |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Leslaw Bieniasz schrieb:
> > Hi, > > I need a simple class implementing rational numbers > as fractions of integers, together with arithmetic > operations on such fractions, checks for overflow, etc. > Any suggestions? > > L.B. > > > !!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!! > *-------------------------------------------------------------------* > | Dr. Leslaw Bieniasz, | > | Institute of Physical Chemistry of the Polish Academy of Sciences,| > | Department of Complex Systems and Chemical | > | Processing of Information | > | ul. Niezapominajek 8, 30-239 Cracow, Poland. | > | tel. (personal) +48 (12) 6395212 | > | tel./fax. (secretariat) +48 (12) 4251923 | > | E-mail: nbbienia@cyf-kr.edu.pl | > *-------------------------------------------------------------------* > | Interested in Computational Electrochemistry? | > | Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia | > *-------------------------------------------------------------------* I don't know if you think this is simple, but I worked with GMP some time ago. mpq_t is a rational number class. Kind regards |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Leslaw Bieniasz wrote:
> I need a simple class implementing rational numbers > as fractions of integers, together with arithmetic > operations on such fractions, checks for overflow, etc. > Any suggestions? I think that in the time it took you to write this and the time it will take for someone to write an useful answer, you could have written such a simple class yourself... :P |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Juha Nieminen schrieb:
> Leslaw Bieniasz wrote: >> I need a simple class implementing rational numbers >> as fractions of integers, together with arithmetic >> operations on such fractions, checks for overflow, etc. >> Any suggestions? > > I think that in the time it took you to write this and the time it > will take for someone to write an useful answer, you could have written > such a simple class yourself... :P Maybe he has some special requirements, like a very efficient libary or arbitrary precision support... |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Feb 8, 1:10pm, Leslaw Bieniasz <nbbie...@cyf-kr.edu.pl> wrote:
> I need a simple class implementing rational numbers > as fractions of integers, together with arithmetic > operations on such fractions, checks for overflow, etc. > Any suggestions? > I haven't used it myself, but boost provides one that seems to fit your requirements: http://www.boost.org/libs/rational/index.html Saul |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"Juha Nieminen" <nospam@thanks.invalid> wrote in message
news:47ac59c0$0$23817$4f793bc4@news.tdc.fi... > Leslaw Bieniasz wrote: >> I need a simple class implementing rational numbers >> as fractions of integers, together with arithmetic >> operations on such fractions, checks for overflow, etc. >> Any suggestions? > > I think that in the time it took you to write this and the time it > will take for someone to write an useful answer, you could have written > such a simple class yourself... :P From past conversation with the author of the Boost Rational class (which I have however never used) I know that there's more to it than that. Just to take one detail, you probably want to store it in simplest form (e.g. 3/4 rather than 6/8) which reduces the chances of overflow when using finite length integers. In fact you want to do some such manipulation during, say, addition so that you don't overflow when you don't have to, even if a naive implementation might. (It's possible for two numbers to be safe, their sum to be safe, but the obvious implementation to overflow during the calculation. I don't know if t's always possible to avoid that.) And of course you want to do that efficiently (no more often than necessary). I don't know all the details. I understand that, as ever, Knuth is the starting point. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Christopher Dearlove wrote:
> "Juha Nieminen" <nospam@thanks.invalid> wrote in message > news:47ac59c0$0$23817$4f793bc4@news.tdc.fi... >> Leslaw Bieniasz wrote: >>> I need a simple class implementing rational numbers >>> as fractions of integers, together with arithmetic >>> operations on such fractions, checks for overflow, etc. >>> Any suggestions? >> I think that in the time it took you to write this and the time it >> will take for someone to write an useful answer, you could have written >> such a simple class yourself... :P > > From past conversation with the author of the Boost Rational class (which > I have however never used) I know that there's more to it than that. Just > to take one detail, you probably want to store it in simplest form (e.g. 3/4 > rather than 6/8) which reduces the chances of overflow when using finite > length integers. In fact you want to do some such manipulation during, > say, addition so that you don't overflow when you don't have to, even if a > naive implementation might. (It's possible for two numbers to be safe, their > sum to be safe, but the obvious implementation to overflow during the > calculation. I don't know if t's always possible to avoid that.) And of > course you want to do that efficiently (no more often than necessary). I > don't know all the details. I understand that, as ever, Knuth is the > starting > point. It was just a joke. |
|
![]() |
| Outils de la discussion | |
|
|