473,783 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rational arithmetic

1 New Member
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 2861
JosAH
11,448 Recognized Expert MVP
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
2148
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 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org>
2
2803
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 script to convert numbers to arbitrary bases. It aims to take any of whole numbers (python ints, longs, or Decimals), rational numbers (n / m n, m whole) and floating points (the best I can do for reals), and convert them to any base between 2 and...
6
2745
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 rational number class from the Boost libararies. Is there anything out there like that or for that matter in the framework that I am missing. Cheers
1
1542
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
3485
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 numerator and denumerator as integers. Structures like this can be used; struct Rational{int a, int b}; to represent a number a/b. Declaration to include Typedef struck Rational Rational; It should include only operation of addition and...
6
4648
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 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...
18
1917
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 fractions with integers, while it should be more fairly compared to floats. Arguments against: - When I use the result of / as a sequence index, it's usually an error which should not be hidden by making the program working for some data, since it...
135
4324
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
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.