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
3
votes
1 answer

Comparing Rational Numbers

I have made the following rational numbers C++ class with all the general arithmetic functions (+, -, *, /, == and !=). template struct rationalNumber { static_assert(!std::numeric_limits::is_signed, "ERROR: Type T must be…
Jonas
  • 6,915
  • 8
  • 35
  • 53
3
votes
2 answers

Custom Struct-returning function stuck on input?

I wanted to make a function with structs to simplify rational numbers , one member is the numerator(int) and the other is the denominator(int) but the program stucks at input!! I am aware that scanf() is a bit risky but I thought its just a couple…
solid.py
  • 2,782
  • 5
  • 23
  • 30
3
votes
1 answer

Importing Ratio module using ghci

I am learning Haskell and trying to use exact Rational numbers. I have the the following simple Haskell code: import Ratio x :: Rational x = 5 % 2 When I load this in WinHugs, everything is fine. However, when I load it in ghci, I get the…
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
2
votes
2 answers

Matlab rat function in C language

Do you know how to make a rational approximation of a decimal number in C (similar to rat Matlab function)? Update If we want a P/Q approximation of a double number number, a quick fix could be: int…
Luis Andrés García
  • 5,852
  • 10
  • 43
  • 57
2
votes
2 answers

Rational numbers in Verilog

I need to use rational numbers in my Verilog code. I looked for any resource but I couldn't find anything about this issue. How can I define rational numbers in Verilog.
suphero
  • 497
  • 2
  • 8
  • 18
2
votes
1 answer

speeding up double to rational number conversion

I wrote a relatively simple code to convert doubles into rational numbers. The code works, and it is guaranteed to find the smallest rational number for a given double; however, it is slower than molasses in January. I spent a day trying various…
JSz
  • 21
  • 3
2
votes
3 answers

Rational numbers in C# and XML

I'm working with an XML file that subscribes to an industry standard. The standards document for the schema defines one of the fields as a rational number and its data is represented as two integers, typically with the second value being a 1 (e.g.…
BobC
  • 412
  • 1
  • 7
  • 20
2
votes
0 answers

Enforcing rational-based conversions between custom-unit-based quantities in Boost.Units

I have a custom unit system defined, which derives from boost::units::si::time. Child units are defined using boost::units::make_scaled_unit, hence the conversion factors are specified using boost::units::scale and boost::units::static_rational,…
2
votes
2 answers

How to serialize boost::rational

I cannot serialize boost::rational. I searched for a boost/serialize/rational.h header but it does not exist. /usr/include/boost/serialization/access.hpp:118:9: error: ‘class boost::rational’ has no member named ‘serialize’ Is there a…
jlandercy
  • 7,183
  • 1
  • 39
  • 57
2
votes
3 answers

rational - original numbers in ruby

How do I get the original numbers? For example when I type: r = Rational(2, 10) # (1/5) 2 and 10 will be changed to 1 and 5: r.numerator # 1 r.denominator # 5 How do I get 2 & 10 from instance of Rational class(r)? I monkey-patched Rational…
Darek Nędza
  • 1,420
  • 1
  • 12
  • 19
2
votes
1 answer

MATLAB vpa() doesn't compute variable-point number for expression with exponent?

Trying to use vpa() to compute a variable point number for a rational expression in an exponent: syms x; ans1 = x^(12345/67890) ans2 = vpa(x^(12345/67890),3) ans2_5 = vpa((12345/67890),3) ans3 = vpa(x*(12345/67890),3) The above shows the issue.…
Trevor
  • 181
  • 1
  • 7
2
votes
5 answers

Convert Floating-point number into a Rational number in Java?

Possible Duplicate: How can I turn a floating point number into the closest fraction represented by a byte numerator and denominator? I would like to take an arbitrary float or double in Java and convert it to a rational number - ie. a number of…
sanity
  • 35,347
  • 40
  • 135
  • 226
1
vote
2 answers

Creating a rational class and operator overloading in python

I'm defining rational class, so for example a = Rational(1,2) #1/2, and b = Rational(2,3) #2/3, and I want to have c = a + b so that c = Rational(7,6) #7/6. My code so far is: class Rational(object): def __init__(self, v1, v2): …
1
vote
5 answers

C++ program with a self-designed class for rational numbers pairs not working as expected

I have a problem with the code below. It is not performing any function now... neither add nor subtract nor divison nor multiplication. Any help will be appreciated... I need explanation... so that I can understand it and get over with it…
tiger
  • 19
  • 1
  • 1
  • 6
1
vote
2 answers

rational arithmetic from given input

Im currently working on https://open.kattis.com/problems/rationalarithmetic for own practice. I get 4 digits and a operation. The input is: x1 y1 op x2 y2 and the fraction is x1/y1 and x2/y2. If I get the input: 1 3 + 1 2 then its 1/3 + 1/2 and the…
user3664730
  • 45
  • 1
  • 8