any simple class for rational numbers?

  • Thread starter Leslaw Bieniasz
  • Start date
L

Leslaw Bieniasz

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: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
H

Hans Mull

Leslaw said:
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: (e-mail address removed) |
*-------------------------------------------------------------------*
| 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
 
J

Juha Nieminen

Leslaw said:
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
 
H

Hans Mull

Juha said:
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...
 
C

Christopher Dearlove

Juha Nieminen said:
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.
 
J

Juha Nieminen

Christopher said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top