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

representing rational numbers using java objects

3
thank you very much for attending to me,if there is any one,who is an expert in programming please how can i write the programme that can compute the approximated value of e
May 5 '07 #1
12 4732
Ganon11
3,652 Expert 2GB
There are many ways to approximate the value of e. Which method were you looking to implement?
May 5 '07 #2
blazo
3
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.


help me please answer me!!!!
May 5 '07 #3
Ganon11
3,652 Expert 2GB
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

Then when you are ready post a new question in this thread.

MODERATOR
May 5 '07 #4
JosAH
11,448 Expert 8TB
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.

help me please answer me!!!!
No need to panic. Play with that series a bit: 1 + 1/1 + 1/2 + 1/6 + 1/24 + 1/120
is the expansion for n == 5. You can rewrite that term as:

5!/5! + 5.4.3.2/5! + 5.4.3/5! + 5.4/5! +5/5! + 1/5!. In general (for no particular value
of n >= 0) you get:

n!/n! + n.(n-1).(n-2) ... 2/n! + n.(n-1).(n-2) ...3/n! + ... n.(n-1)/n! + n/n! +1/n!

The division num/div ~ e > 1 so if the numerator should be < INT_MAX. The
largest denominator is reached for n == 12 because 12! < INT_MAX and
13! > INT_MAX.12!*e < INT_MAX so you can sum those terms up to n == 12.

kind regards,

Jos
May 6 '07 #5
JosAH
11,448 Expert 8TB
ps. I forgot to mention that a naive addition: a/b + c/d == (a*d + c*b) / (b*d)
will cause integer overflow. A check for (b == d) solves this little problem.

kind regards,

Jos
May 6 '07 #6
milcah
1
a program that can add and multiply rational numbers .use that program to calculate e
May 12 '07 #7
JosAH
11,448 Expert 8TB
a program that can add and multiply rational numbers .use that program to calculate e
Yes indeed, that was the exact subject we were discussing here, thank you.

kind regards,

Jos
May 12 '07 #8
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.


help me please answer me!!!!
where is the answer?
May 14 '07 #9
JosAH
11,448 Expert 8TB
where is the answer?
Hopefully it's between you ears because a boilerplate code answer doesn't do
much good most of the time. It's a "give a man a fish and he'll live for another
day; teach him how to fish etc. etc." kind of thing.

kind regards,

Jos
May 14 '07 #10
DeMan
1,806 1GB
More specifically, give a man a program and he'll return with the same question tomorrow, teach a man to program and he might teach you something tomorrow.
May 16 '07 #11
the moment someone comes screaming for help about adding, you help them, dont just say go to the river so you can fish, show them the river and may be buy them a hook to fish, be helpful guys
May 17 '07 #12
r035198x
13,262 8TB
the moment someone comes screaming for help about adding, you help them, dont just say go to the river so you can fish, show them the river and may be buy them a hook to fish, be helpful guys
It's a bit of measure for measure actually. Code for code. We don't want to do more work than the OP is willing to put in themselves.
May 17 '07 #13

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...
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...
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
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.