Questions tagged [fractions]

Fractions, or "fractional numbers" (lit. "broken numbers"), or "rational numbers", are numbers expressed as the ratio of two integers, such as 1/2 or 23/17.

In computing and programming, fractional (or "rational") numbers are an alternative to floating point numbers: Since numerator and denominator are stored as integers, the representation is exact, as opposed to most floating point implementations, which cannot represent most rational numbers exactly. Arbitrary-precision ("bignum") libraries usually contain support for rational numbers.

800 questions
212
votes
24 answers

range() for floats

Is there a range() equivalent for floats in Python? >>> range(0.5,5,1.5) [0, 1, 2, 3, 4] >>> range(0.5,5,0.5) Traceback (most recent call last): File "", line 1, in range(0.5,5,0.5) ValueError: range() step argument must…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
119
votes
6 answers

How to convert a decimal number into fraction?

I was wondering how to convert a decimal into a fraction in its lowest form in Python. For example: 0.25 -> 1/4 0.5 -> 1/2 1.25 -> 5/4 3 -> 3/1
user2370460
  • 7,470
  • 10
  • 31
  • 46
104
votes
26 answers

Best way to represent a fraction in Java?

I'm trying to work with fractions in Java. I want to implement arithmetic functions. For this, I will first require a way to normalize the functions. I know I can't add 1/6 and 1/2 until I have a common denominator. I will have to add 1/6 and…
eleven81
  • 6,301
  • 11
  • 37
  • 48
73
votes
11 answers

How to write fraction value using html?

I want to write fraction value such as the picture below: How do I write fraction value using html without using image? NOTE: I don't want this 1 1/2 pattern but strictly just as the pic above
user774411
  • 1,749
  • 6
  • 28
  • 47
54
votes
8 answers

Is there a JavaScript function that reduces a fraction

say we have fraction 2/4, it can be reduced to 1/2. Is there a JavaScript function that can do the reducing?
dave
  • 14,991
  • 26
  • 76
  • 110
54
votes
4 answers

Does Python have a function to reduce fractions?

For example, when I calculate 98/42 I want to get 7/3, not 2.3333333, is there a function for that using Python or Numpy?
LWZ
  • 11,670
  • 22
  • 61
  • 79
51
votes
3 answers

Print number as reduced fraction in R

On my old beat-up TI-83 I can get a reduced fraction representation of a rational real number with the following syntax. .14>Frac 7/50 Is there a similar syntax, function, or CRAN package that will allow me to do this in R?
Daniel Standage
  • 8,136
  • 19
  • 69
  • 116
40
votes
13 answers

Is there a reliable way in JavaScript to obtain the number of decimal places of an arbitrary number?

It's important to note that I'm not looking for a rounding function. I am looking for a function that returns the number of decimal places in an arbitrary number's simplified decimal representation. That is, we have the…
Milosz
  • 2,924
  • 3
  • 22
  • 24
32
votes
19 answers

Convert Fraction String to Decimal?

I'm trying to create a javascript function that can take a fraction input string such as '3/2' and convert it to decimal—either as a string '1.5' or number 1.5 function ratio(fraction) { var fraction = (fraction !== undefined) ? fraction :…
ryanve
  • 50,076
  • 30
  • 102
  • 137
31
votes
5 answers

Split decimal variable into integral and fraction parts

I am trying to extract the integral and fractional parts from a decimal value (both parts should be integers): decimal decimalValue = 12.34m; int integral = (int) decimal.Truncate(decimalValue); int fraction = (int) ((decimalValue -…
invarbrass
  • 2,023
  • 4
  • 20
  • 23
26
votes
1 answer

Is there any way to access denominator of a fraction in Python 2

Is there any way to access the denominator in a fraction in Python 2? For example, I have a variable: x = fractions.Fraction(4,1) And I want to access its denominator. How do I do that?
nsane
  • 1,715
  • 4
  • 21
  • 31
25
votes
11 answers

How to display "Classic" built-up fractions with an horizontal line in CSS / JavaScript?

I have a fraction and I want to display it neatly and nicely. For example 4/5 would be 4 — 5 I have looked at this and while this solution is decent the problem lies in having the 4 and the 5 in the same line with a straight line separating…
cjds
  • 8,268
  • 10
  • 49
  • 84
23
votes
15 answers

PHP convert decimal into fraction and back?

I want the user to be able to type in a fraction like: 1/2 2 1/4 3 And convert it into its corresponding decimal, to be saved in MySQL, that way I can order by it and do other comparisons to it. But I need to be able to convert the decimal back…
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
22
votes
4 answers

regex to match irreducible fractions

How can I match irreducible fractions with regex? For example, 23/25, 3/4, 5/2, 100/101, etc. First of all, I have no idea about the gcd-algorithm realization in regex. Update for all of you who's answering like "You are using the wrong tool": Yeah,…
20
votes
2 answers

What's the difference between the classes Floating and Fractional in Haskell?

What is the difference is between the Floating and Fractional classes in Haskell?
joseabp91
  • 305
  • 2
  • 8
1
2 3
53 54