Questions tagged [rational-number]

A rational is a number that can be expressed as the ratio of two integers.

In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, p and q, with the denominator q not equal to zero. Since q may be equal to 1, every integer is a rational number. The set of all rational numbers is usually denoted by a boldface Q (or blackboard bold (\mathbb{Q} in LaTeX), Unicode ℚ); it was thus denoted in 1895 by Peano after quoziente, Italian for "quotient".

110 questions
3
votes
1 answer

How to prove that the halving function over positive rationals always has an existential?

open import Data.Nat using (ℕ;suc;zero) open import Data.Rational open import Data.Product open import Relation.Nullary open import Data.Bool using (Bool;false;true) halve : ℕ → ℚ halve zero = 1ℚ halve (suc p) = ½ * halve p ∃-halve : ∀ {a b} → 0ℚ…
Marko Grdinić
  • 3,798
  • 3
  • 18
  • 21
3
votes
0 answers

Round a Rational64 down to next multiple of a fraction without overflow?

I'm representing numbers as ratios of signed 64-bit integers using the num-rational crate's Rational64 type. I'm trying to round a number down to the next multiple of another number, and I'm getting integer overflow issues when I do it in either of…
joshlf
  • 21,822
  • 11
  • 69
  • 96
3
votes
1 answer

boost rational cast to double

Using the following bit of code compiled against boost 1.62: #include #include int main() { auto val = boost::rational(499999, 2); std::cout << val << std::endl; std::cout <<…
user4436000
  • 413
  • 4
  • 7
3
votes
2 answers

Finding the continued fraction of 2^(1/3) to very high precision

Here I'll use the notation It is possible to find the continued fraction of a number by computing it then applying the definition, but that requires at least O(n) bits of memory to find a0, a1 ... an, in practice it is a much worse. Using double…
3
votes
2 answers

Pattern-matching on Rationals in Haskell

The following function is pretty straightforward: test :: Int -> Int test x = case x of 0 -> 0 1 -> 1 _ -> 2 and indeed, test 0 == 0, test 1 == 1, and test 77 == 2. The following function is almost as straightforward: import…
3
votes
2 answers

problem with Double and Rational Number

I am writing a function in which I need to read a string contains floating point number and turn it back to Rational. But When I do toRational (read input :: Double), it will not turn for eg: 0.9 into 9 % 10 as expected, but instead 81..... %…
altair211
  • 97
  • 1
  • 8
2
votes
2 answers

rationalize() for BigFloats has an upper limit?

I have the following code: function recursion(i::BigFloat) r = BigFloat(0) if i >= 1.0 i = i - 1.0 r = 1.0/(2.0+recursion(i)) end return r end function main() solution = 0 i = BigFloat(1) while i < 1000 …
MFerguson
  • 1,739
  • 9
  • 17
  • 30
2
votes
1 answer

Rational approximation of double using int numerator and denominator in C++

A real world third party API takes a parameter of type fraction which is a struct of an int numerator and denominator. The value that I need to pass is known to me as a decimal string that is converted to a double. The range of possible values are,…
2
votes
1 answer

Trivial Rationals problems without variables in SBV Solver in Haskell

I am working with SBV Solver in Haskell, specifically I am using the Rational type for the variables. I want to solve linear problems for example: solution1 = do [x] <- sRationals ["x"] constrain $ 5.%1 .<= x The code works without…
2
votes
1 answer

Why are rational numbers from Num printed as ?

I continue with my exploration on the Num library of Ocaml, with the reason that one whole library about logics was written using it. Today, I would like to make the negative of a rational number. Obtain -1/2, from 1/2. To do so, I think that, given…
Theo Deep
  • 666
  • 4
  • 15
2
votes
1 answer

Convert a float to a rational number that is guaranteed to convert back to the original float

I am looking for an algorithm to convert a float to a rational number, such that the rational number is guaranteed to evaluate back to the original float, and the denominator is minimized. A naive algorithm can just return the actual value of the…
xiaq
  • 764
  • 1
  • 6
  • 13
2
votes
1 answer

Square root calculation using continued fractions to n bits of precision

This is an unsolved problem from my past arbitrary-precision rational numbers C++ assignment. For calculation, I used this expression from Wikipedia (a being the initial guess, r being its remainder): I ended up, just by guessing from experiments,…
2
votes
4 answers

Why can this constructor declared with two parameters be called with just one?

I watched my lecturer's video from my university and he says about the Rational class that its constructor goes like this: Rational (int top=0 , int bottom=1) : t(top) , b(bottom) {normalize();} Until now everything is OK, BUT !! he also said that…
Master C
  • 1,536
  • 4
  • 14
  • 19
2
votes
1 answer

Appropriate scale for converting via BigDecimal to floating point

I've written an arbitrary precision rational number class that needs to provide a way to convert to floating-point. This can be done straightforwardly via BigDecimal: return new BigDecimal(num).divide(new BigDecimal(den), 17,…
rwallace
  • 31,405
  • 40
  • 123
  • 242
2
votes
1 answer

How can I feed a rational or irrational number to argparse?

How can I feed rational numbers like 3/2, through terminal, into my python script using argparse? I have the same problem when I want to input pi or any other irrational numbers like sqrt(2) using argparse. Regards, Hadi
hadi zahir
  • 21
  • 1