rational-numbers

Convert decimal to fraction (rational number) in Objective C?

被刻印的时光 ゝ 提交于 2020-01-05 10:27:29
问题 As part of a calculator app, I am trying to implement uses with sigma notation. However, the result it prints out is always a decimal, and the rest isn't important. I simply want to change the decimal to a fraction. I already have the reduce function, the problem I'm having is getting from a decimal like this: '0.96875' to it's fractional value, '31/32' Thanks! PS: I've looked into just about everything, and for the life of me, I can't figure this out. All I need at this point is how to take

How to serialize boost::rational

核能气质少年 提交于 2019-12-20 05:26:29
问题 I cannot serialize boost::rational<int> . 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<int>’ has no member named ‘serialize’ Is there a way to achieve it? 回答1: Just serialize the numerator and denominator. Here's the legwork, in semi-generic form (supports archives with named nodes, like XML serialization, too): Live On Coliru #include <boost/archive/xml_iarchive.hpp> #include <boost

simplifying fractions in Java

筅森魡賤 提交于 2019-12-18 04:30:15
问题 My task is to develop a rational class. If 500 and 1000 are my inputs, then (½) must be my output. I have written a program on my own to find it. Is there another best way to find the solution, or my program is already the best one? public class Rational { public static void main(String[] args){ int n1 = Integer.parseInt(args[0]); int n2 = Integer.parseInt(args[1]); int temp1 = n1; int temp2 = n2; while (n1 != n2){ if(n1 > n2) n1 = n1 - n2; else n2 = n2 - n1; } int n3 = temp1 / n1 ; int n4 =

How to parse a decimal fraction into Rational in Haskell?

我们两清 提交于 2019-12-10 02:42:02
问题 I've been participating in a programming contest and one of the problems' input data included a fractional number in a decimal format: 0.75 is one example. Parsing that into Double is trivial (I can use read for that), but the loss of precision is painful. One needs to be very careful with Double comparisons (I wasn't), which seems redundant since one has Rational data type in Haskell. When trying to use that, I've discovered that to read a Rational one has to provide a string in the

Coq QArith division by zero is zero, why?

独自空忆成欢 提交于 2019-12-09 09:04:31
问题 I noticed that in Coq's definition of rationals the inverse of zero is defined to zero. (Usually, division by zero is not well-defined/legal/allowed.) Require Import QArith. Lemma inv_zero_is_zero: (/ 0) == 0. Proof. unfold Qeq. reflexivity. Qed. Why is it so? Could it cause problems in calculations with rationals, or is it safe? 回答1: The short answer is: yes, it is absolutely safe. When we say that division by zero is not well-defined, what we actually mean is that zero doesn't have a

How to serialize boost::rational

二次信任 提交于 2019-12-02 07:17:43
I cannot serialize boost::rational<int> . 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<int>’ has no member named ‘serialize’ Is there a way to achieve it? Just serialize the numerator and denominator. Here's the legwork, in semi-generic form (supports archives with named nodes, like XML serialization, too): Live On Coliru #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/serialization/serialization.hpp> #include <boost/rational.hpp>

Pure Python rational numbers module for 2.5

梦想与她 提交于 2019-12-01 18:11:46
Has anybody seen such a thing? Small self-sufficient modules are preferred. A. Coady The fractions module from 2.6 can be ripped out if necessary. Grab fractions.py, numbers.py, and abc.py; all pure python modules. You can get the single files from here (2.6 branch, 2.7 does not work): http://hg.python.org/cpython/branches SymPy is a symbolic maths library written entirely in Python and has full support for rational numbers. From the tutorial : >>> from sympy import * >>> a = Rational(1,2) >>> a 1/2 >>> a*2 1 >>> Rational(2)**50/Rational(10)**50 1/88817841970012523233890533447265625 There is

Pure Python rational numbers module for 2.5

随声附和 提交于 2019-12-01 16:19:16
问题 Has anybody seen such a thing? Small self-sufficient modules are preferred. 回答1: The fractions module from 2.6 can be ripped out if necessary. Grab fractions.py, numbers.py, and abc.py; all pure python modules. You can get the single files from here (2.6 branch, 2.7 does not work): http://hg.python.org/cpython/branches 回答2: SymPy is a symbolic maths library written entirely in Python and has full support for rational numbers. From the tutorial: >>> from sympy import * >>> a = Rational(1,2) >>

rational - original numbers in ruby

半腔热情 提交于 2019-12-01 08:54:42
问题 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 class and created new method( Rational_o ): def Rational_o *args x, y = args r = Rational *args r.x = x r.y = y r end class Rational attr_accessor :x, :y end It works, but is there build-in method or variable(s) where original x & y are stored? 回答1: No,

Algorithm for detecting repeating decimals?

倖福魔咒の 提交于 2019-11-30 07:14:53
问题 Is there an algorithm for figuring out the following things? If the result of a division is a repeating decimal (in binary). If it repeats, at what digit (represented as a power of 2) does the repetition start? What digits repeat? Some examples: 1/2 = 1/10 = 0.1 // 1 = false, 2 = N/A, 3 = N/A, 4 = N/A 1/3 = 1/11 = 0.010101... // 1 = true, 2 = -2, 3 = 10 2/3 = 10/11 = 0.101010... // 1 = true, 2 = -1, 3 = 10 4/3 = 100/11 = 1.010101... // 1 = true, 2 = 0, 3 = 10 1/5 = 1/101 = 0.001100110011... /