Questions tagged [rational-numbers]

In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, with the denominator q not equal to zero.

Some programming languages provide a built-in (primitive) rational data type to represent rational numbers and to do arithmetic on them (e.g. the ratio type of Common Lisp and analogous types provided by most languages for algebraic computation, such as Mathematica and Maple).

Many languages that do not have a built-in rational type still provide it as a library-defined type.

86 questions
0
votes
1 answer

Multiplying Fractions and Integers in Python

I'm trying to build a rational numbers class that will perform various arithmetic functions based on input values without using the fractions module. When I'm using two different fractions the code works fine, but as soon as I try to use an integer…
turkadactyl
  • 3
  • 1
  • 3
0
votes
3 answers

Why won't my method print out?

I've made a main method in one class and a lot of other small methods in another class. When I call on them in my main method using their location and making sure that they would outprint if I called on them, they still don't outprint. Only the…
0
votes
0 answers

Round fixed-point rational to narrower denominator

I've got a (let's say nonnegative) rational number expressed as the ratio of two integers a/b, where 0 <= a < 2^m and 0 < b < 2^n. I'd like to round that to a smaller representation with only p bits in the denominator; that is, find the largest…
Sneftel
  • 40,271
  • 12
  • 71
  • 104
0
votes
1 answer

Javascript rationals - preserving denominator of 1

I need to work with rationals in Javascript that have a denominator of 1. So, I have some input value, say 1024, and I need to store it as 1024/1. Of course 1024 / 1 just gives me 1024. So how can I obtain the raw rational version?
ChrisM
  • 2,128
  • 1
  • 23
  • 41
0
votes
2 answers

Pushing rational numbers

I have to make class NumberSet, which is a container for different kinds of numbers that can include only numbers that are not already in it. class NumberSet include Enumerable def initialize @arr=[] end def each (&block) @arr.each…
user103220
  • 35
  • 1
  • 9
0
votes
1 answer

C++: Why won't erase() remove the first character in my string?

I'm writing a rational class, where the following must be acceptable forms of input: 3/4, 1.4545, (7/8, (7/8), 7/8), or even ((((((5/4))), where my code should rewrite it as 5/4 for the constructor to accept. At this point I am passing a string…
0
votes
1 answer

Operator overloading with integers Python

I am writing a program to divide rational number but I want it to be able to handle fractions. I want to divide 1 by 1/3 but my program is encountering an error when dealing with the integer. I have tried to convert the integer to a rational several…
0
votes
1 answer

Floating point errors

I am having trouble with floating points. A the double . 56 in Java, for example, might actually be stored as .56000...1. I am trying to convert a decimal to a fraction. I tried to do this using continued fractions Continuous Fractions but my…
Zhv Z
  • 53
  • 5
0
votes
3 answers

How to create a List of fractions?

I need a way of putting a fraction into an ArrayList. I am having problems however because I'm not sure how I could do this in a way that I could later compare them so they can be sorted. Note: I will be using 4 or more fractions and I'm not too…
0
votes
2 answers

The most accurate way to calculate numerator and denominator of a double

I have implemented class NaturalNum for representing a natural number of "infinite" size (up to 4GB). I have also implemented class RationalNum for representing a rational number with infinite accuracy. It stores the numerator and the denominator of…
barak manos
  • 29,648
  • 10
  • 62
  • 114
0
votes
3 answers

Why am I getting rounding errors when using Ruby Rational?

I'm calculating programatically the frequencies of given musical notes. Quick intro: The musical note A4 has a frequency of 440Hz Notes one octave higher are double the frequency of the same note in a lower octave (so A3 is 220Hz, A2 is 110Hz) The…
bodacious
  • 6,608
  • 9
  • 45
  • 74
0
votes
1 answer

Boost.Units frequency to period

I am trying to use the Boost.Units library in my conversion of a frequency (for instance, 20kHz) into the duration of its period (50000 nanoseconds). I don't want to use floating point. The trouble I am having is that simply taking the reciprocal…
user1714423
  • 191
  • 5
0
votes
1 answer

Octave random Rational Numbers gernrator

I just start to study about Octave and I have a question about getting Rational Numbers. I just check http://www.gnu.org/software/octave/doc/interpreter/Random-Number-Generation.html#Random-Number-Generation this page to learn the way to get…
Dc Redwing
  • 1,681
  • 11
  • 32
  • 42
0
votes
1 answer

Why have Apache commons-math based it's Fraction on int type?

Why have Apache commons-math3 based it's Fraction on int type??? Are there any reasons to use int instead of long? Do we have some performance gains here? Aren't longs process at the same speed as ints on modern CPUs? I think we got only unneeded…
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
0
votes
1 answer

Representing a fraction in binary?

I'm looking at binary data representations for fractions, and I was wondering what kind of a data structure the fraction would have if you were to store both the numerator and denominator that store a fraction. Is it just two separate numbers that…
Adam
  • 355
  • 1
  • 6
  • 12