473,405 Members | 2,334 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,405 software developers and data experts.

representing rational numbers using java objects

1
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.
May 10 '07 #1
6 4626
r035198x
13,262 8TB
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 read the guidelines.
May 10 '07 #2
JosAH
11,448 Expert 8TB
Please read the guidelines.
On top of that: I've seen that exact same assignment description before here ;-)

kind regards,

Jos
May 10 '07 #3
Ganon11
3,652 Expert 2GB
On top of that: I've seen that exact same assignment description before here ;-)

kind regards,

Jos
Several times.
May 10 '07 #4
JosAH
11,448 Expert 8TB
Several times.
You've been around here much longer than I have ;-) I do like the assignment
though: it's well defined and a clean implementation is almost written by just
reading the assignment text carefully.

kind regards,

Jos
May 10 '07 #5
Ganon11
3,652 Expert 2GB
I don't like it because it has to do with Taylor series (the infinite series to calculate the value of e). This is one of the topics I had to learn on my own for the big AP Calculus exam I took yesterday, and was by far the hardest topic. I don't wanna talk about series again until Calc II in college.
May 10 '07 #6
JosAH
11,448 Expert 8TB
I don't like it because it has to do with Taylor series (the infinite series to calculate the value of e). This is one of the topics I had to learn on my own for the big AP Calculus exam I took yesterday, and was by far the hardest topic. I don't wanna talk about series again until Calc II in college.
Series are fun :-P I answered the series part last week in this very same forum
and I even managed to upset a few folks in the Cafe/Lounge section with those
series ;-) but I'm too lazy to answer it again (and again and again) just because
people are too lazy too google or read.

kind regards,

Jos
May 10 '07 #7

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

Similar topics

21
by: Mike Meyer | last post by:
PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org> Status: Draft Type: Staqndards...
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...
4
by: Martin Stainsby | last post by:
I'm not sure whether I am approaching this correctly? I'm trying to get the focal length from exif info stored in an image. The Visual studio help says it is stored in "An array of two Byte...
3
by: Schüle Daniel | last post by:
Hello NG, recently I was using Scheme and Ruby and was really nicely surprised to find there support for the computing with rational numbers for example this how it works in Ruby ...
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...
3
BenTheMan
by: BenTheMan | last post by:
Hello all. I will probably be frequenting these discussions in the future. I am a graduate student in physics learning C++ on the fly. This is probably an easy quesiton, but my background in...
5
by: anumliaqat | last post by:
hello!! i hav a problem.my program is giving correct outputs for some inputs but wrong results for others. for example if u give (4 2 2)&(2 1 2) to it,it shows all results correct....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.