rational-number

Exact value of a floating-point number as a rational

帅比萌擦擦* 提交于 2020-12-01 11:39:11
问题 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 impossible, then the result falls back to the best approximation which still satisfies the condition. Hold on. There are a lot of questions/answers here about the best rational approximation of a truncated real number which is represented as a floating

Exact value of a floating-point number as a rational

扶醉桌前 提交于 2020-12-01 11:38:21
问题 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 impossible, then the result falls back to the best approximation which still satisfies the condition. Hold on. There are a lot of questions/answers here about the best rational approximation of a truncated real number which is represented as a floating

Exact value of a floating-point number as a rational

£可爱£侵袭症+ 提交于 2020-12-01 11:35:08
问题 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 impossible, then the result falls back to the best approximation which still satisfies the condition. Hold on. There are a lot of questions/answers here about the best rational approximation of a truncated real number which is represented as a floating

Appropriate scale for converting via BigDecimal to floating point

久未见 提交于 2020-01-15 06:01:35
问题 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, RoundingMode.HALF_EVEN).doubleValue(); but this requires a value for the scale parameter when dividing the decimal numbers. I picked 17 as the initial guess because that is approximately the precision of a double precision floating point number, but I don't know whether

How to convert a Rational into a “pretty” String?

為{幸葍}努か 提交于 2020-01-12 17:40:31
问题 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 of decimal places, since Rational expansions may be non-terminating. Hoogle and the haddocks for Data.Ratio didn't help me. Where can I find this function? 回答1: Here is an arbitrary precision solution that doesn't use floats: import Data.Ratio

How to convert a Rational into a “pretty” String?

自闭症网瘾萝莉.ら 提交于 2020-01-12 17:40:11
问题 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 of decimal places, since Rational expansions may be non-terminating. Hoogle and the haddocks for Data.Ratio didn't help me. Where can I find this function? 回答1: Here is an arbitrary precision solution that doesn't use floats: import Data.Ratio

How to convert a Rational into a “pretty” String?

爱⌒轻易说出口 提交于 2020-01-12 17:40:02
问题 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 of decimal places, since Rational expansions may be non-terminating. Hoogle and the haddocks for Data.Ratio didn't help me. Where can I find this function? 回答1: Here is an arbitrary precision solution that doesn't use floats: import Data.Ratio

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

▼魔方 西西 提交于 2020-01-03 18:52:31
问题 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, 2)]]) print(np.linalg.det(m)) # Gives -0.321428571429 print(m[0,0]*m[1,1] - m[0,1]*m[1,0]) # Gives -9/28 Since computing the determinant only require rational operations with the Gauss' method, the determinant of a rational matrix is rational. So my questions are

problem with Double and Rational Number

巧了我就是萌 提交于 2019-12-23 21:44:31
问题 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..... % 9007... Thx 回答1: This is correct behavior. The number 0.9 is not representable as a Double , not in Haskell, C, or Java. This is because Double and Float use base 2: they can only represent a certain subset of the dyadic fractions exactly. To get the

How to format a Rational number as a decimal?

混江龙づ霸主 提交于 2019-12-23 17:16:26
问题 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 is: "0.000030517578125" to_f apparently doesn't work: r.to_f #=> 3.0517578125e-05 And sprintf requires me to specify the number of digits: sprintf('%.30f', r) #=> "0.000030517578125000000000000000" 回答1: Most ten-year-olds know how to do that: use long division! 1 Code