473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

rational arithmetic

In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other real numbers. This is the floating-point representation. However as we may all know, floating-point numbers are quite inaccurate. This means that ½ might actually be represented as 0.49998, which may not be good enough for some applications.
In this assignment we shall explore a way of representing rational numbers using structures. For each rational number we shall represent/store the numerator and denominator as integers. We shall use a structure like this:
struct Rational {int a, int b;};

to represent a number . In C we declare a new structure to represent our rational number using:
typedef struct Rational Rational;

This declares the new type Rational to be an alias for struct Rational. We can now define operations on data of this kind. In particular we need to implement rational number arithmetic. We shall limit ourselves to the operations of addition and multiplication. (Note that subtraction is really addition in disguise and is no more complicated.)
Your first task will be to implement the functions:
Rational R_init(int a, int b);
Rational R_add(Rational x, Rational y);
Rational R_mult(Rational x, Rational y);
int R_show(Rational x);

The functions should do the following: R_init should return a rational representation for where a and b are the integers. R_add should add two rational numbers returning their sum, R_mult should return the product of the two rational arguments it is passed. R_show should print out the rational number (in the form a/b, not as real decimal).
This part should be pretty straightforward. You should not need to reduce the rational numbers to their lowest form. That is, if you add and , it is OK to report 2/6 as the answer, rather than 1/3.

The next part of the assignment requires you to compute a close rational approximation to e, which is often approximated to 2.17… Note that e is actually 1 + 1/1+1/2+1/6+1/24+1/120 + … Compute this sum as far as it will go without giving you integer overflow problems. In short, write a function:
void e(void);
This function should compute and print out e using the rational arithmetic functions you wrote above.

please guide me
May 15 '07 #1
1 2835
JosAH
11,448 Expert 8TB
I have a deja vu.

kind regards,

Jos
May 15 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Mike Meyer | last post by:
This version includes the input from various and sundry people. Thanks to everyone who contributed. <mike PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
6
by: Mike Friel | last post by:
I am writing an assembly that will be used in a suite of financial applcations. Naturally I must use integer arithmetic to ensure the accuracy of my calculations and in the past have used the...
1
by: muggy440 | last post by:
Hello, I would like some sample programs about the implementation of the function struct and typedef as applied to solving rational problems.
1
by: lordhavemercy | last post by:
Rational Arithmetic I need a possible representstion of Rational numbers in C using Structures. It has to be a foating-point representatiobn. Each rational number has to represent/store the...
6
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
18
by: Lie | last post by:
I'm very surprised actually, to see that Python rejected the use of fractional/rational numbers. However, when I read the PEP, I know exactly why the proposal was rejected: People compared...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.