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
10
votes
2 answers

Specify the number of decimal digits on the explicit generator of a sequence in Raku

I've written a simple code: sub euler-ex ($x) of Seq { 1, { $x**++$ / [×] 1 .. ++$ } ... Inf } say " 5: " ~ euler-ex(5)[^20] ~ " = " ~ [+](euler-ex(5)[^20]); The output: 5: 1 5 12.5 20.833333 26.041667 26.041667 21.701389 15.500992 9.68812…
Lars Malmsteen
  • 738
  • 4
  • 23
9
votes
2 answers

Converting an arbitrary-precision rational number (OCaml, zarith) to an approximate floating number

I'm using the Zarith library to do arbitrary-precision rational arithmetic. Suppose I have a rational number q of type Q.t that is the ratio of two big integers (Q is Zarith's arbitrary-precision rational number module). Sometimes, I would like to…
8
votes
1 answer

Exact value of a floating-point number as a rational

I'm looking for a method to convert the exact value of a floating-point number to a rational quotient of two integers, i.e. a / b, where b is not larger than a specified maximum denominator b_max. If satisfying the condition b <= b_max is…
8
votes
5 answers

How to convert a Rational into a "pretty" String?

I want to display some Rational values in their decimal expansion. That is, instead of displaying 3 % 4, I would rather display 0.75. I'd like this function to be of type Int -> Rational -> String. The first Int is to specify the maximum number…
Mike Izbicki
  • 6,286
  • 1
  • 23
  • 53
5
votes
4 answers

How to use Enumerations in DXL Scripts?

I'd like to test the value of an enumeration attribute of a DOORs object. How can this be done? And where can I find a DXL documentation describing basic features like this? if (o."Progress" == 0) // This does NOT work { // do something }
h0b0
  • 1,802
  • 1
  • 25
  • 44
5
votes
2 answers

Solving rational number Linear Programming problem in Python

I have an LP with integer constraints that I want to solve in exact arithmetic, using Python. In fact, I only need a feasible point. Edit: "Exact arithmetic" here means rational numbers, of unbounded enumerator and denominator. Previous…
Hennich
  • 682
  • 3
  • 18
5
votes
1 answer

boost rational_cast with rounding?

How can I do a rational_cast with rounding? Currently I'm doing a hack like this: boost::rational pts = ..., time_base = ...; int64_t rounded = std::llround(boost::rational_cast(pts / time_base)); But I'd like to be…
ronag
  • 49,529
  • 25
  • 126
  • 221
5
votes
2 answers

Ruby code to remove insignificant decimal places?

I would like to get an elegant code, which removes the insignificant closing zeroes, eg: 29.970 => 29.97 29.97 => 29.97 25.00 => 25 25.0 => 25 I tried: argument.to_r.to_f.to_s But doesn't work in every cases, eg on 25.00 gives 25.0
Konstantin
  • 2,983
  • 3
  • 33
  • 55
4
votes
2 answers

How are rational numbers implemented in Julia

Are the numerator and denominator stored as integers? Specifically, how are implemented the basic operations: sum, subtraction, multiplication, division?
4
votes
2 answers

Power function handling negative base with non integer exponents

Math.pow(), pow(), whatever it's called, a lot of languages (and calculators) have some built in function to calculate x=a^b for floats (or doubles). There's the special case of a being negative and b not being an integer. Some will return NaN,…
user4159038
4
votes
3 answers

How to format a Rational number as a decimal?

Given an arbitrary large (or small) Rational number that has a finite decimal representation, e.g.: r = Rational(1, 2**15) #=> (1/32768) How can I get its full decimal value as a string? The expected output for the above number…
Stefan
  • 109,145
  • 14
  • 143
  • 218
4
votes
3 answers

parse input to rational in Julia

I want to read the user input and store it as a Rational, whatever the type: integer, float ot rational. For instance: 5 --> store it as 5//1 2.3 --> store it as 23//10 4//7 --> store it as 4//7 At the moment I wrote the following: a =…
Pigna
  • 2,792
  • 5
  • 29
  • 51
4
votes
2 answers

Why doesn't numpy determinant return a Fraction when given a Fraction matrix?

I want to perform operations on rational matrices. I use the modules numpy and fractions. Here is my code: import numpy as np from fractions import Fraction m=np.matrix([[Fraction(1, 6), Fraction(8, 7)], [Fraction(1, 2), Fraction(3,…
Tom Cornebize
  • 1,362
  • 15
  • 33
3
votes
1 answer

Is "less than" for rational numbers decideable in Coq?

In the Coq standard library, the "less than" relation is decidable for the natural numbers (lt_dec) and for the integers (Z_lt_dec). When I search however (Search ({ _ _ _ } + {~ _ _ _ })) I can't find a Q_le_dec or Qle_dec for the rationals, only…
LogicChains
  • 4,332
  • 2
  • 18
  • 27
3
votes
1 answer

rationalize number with limit denominator

I know that Maxima CAS can rationalize floating point number ( convert to ratio): (%i215) rat(0.1667); (%o215) 1667/10000 Is it possible to rationalize floating point number with limit denominator like in python ? I would like to set limit…
Adam
  • 1,254
  • 12
  • 25
1
2 3 4 5 6 7 8