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.
6 4634
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.
JosAH 11,448
Recognized Expert MVP
Please read the guidelines.
On top of that: I've seen that exact same assignment description before here ;-)
kind regards,
Jos
Ganon11 3,652
Recognized Expert Specialist
On top of that: I've seen that exact same assignment description before here ;-)
kind regards,
Jos
Several times.
JosAH 11,448
Recognized Expert MVP
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
Ganon11 3,652
Recognized Expert Specialist
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.
JosAH 11,448
Recognized Expert MVP
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
Content-Type: text/x-rst
Created: 16-Dec-2004
Python-Version: 2.5
|
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>
|
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...
|
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
|
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 objects
that represent a rational number".
Yet there are eight bytes in the info? shown below.
89 00 00 00 0A 00 00 00
| |
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
mond:/pool/PROG/ruby # irb
irb(main):001:0>
irb(main):002:0* require "mathn"
|
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...
|
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 this subject is very limited. I have tried a few permutations of what I think SHOULD be right, but the compiler is giving me very cryptic errors (like...
|
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.
but for (2 2 4)&(2 1 4) it gives wrong outputs.
now i am unable to find which function is to be corrected and which one not.
...
|
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...
|
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. ...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |