Monday 17 November 2014

Why a decimal fraction is not expressing exactly what a rational number is in base 2?



I am currently using rational numbers to express currency and math operations with currency, while dealing with rational numbers has provided a great convenience in over coming the limitations of scaling integers into unit64 I have a few concerns around decimal fractions vs rational numbers and the quotient they express:




Given the following rational number:



$5764607523034235/576460752303423488$



this supposedly represents 0.01 'exactly', according to the 'quotToFloat' method found here: http://golang.org/src/pkg/math/big/rat.go



but given the following decimal fraction:



$1/100$




this supposedly does not represent 0.01 'exactly'.



My concern lies in holding 2 different rational numbers that represent the same qoutient but not being able to find equality between them without converting them and comparing.



Why is it that $1/100$ does not express 0.01 exactly? Am I butting up against base2 limitations?


Answer



Yes, you are up against base 2 limitations. It is the same as in base 10, you cannot represent $\frac 13=0.\overline 3$ exactly because it has a prime factor that does not divide $10$. $0.1_{10}=0.00011\overline{0011}_2$. It is a repeating "decimal" because of the factor $5$ in the denominator. Why are you using quotToFloat? It looks like your package has exact rationals, so you can just subtract them and check whether the difference is zero for equality.


No comments:

Post a Comment

real analysis - How to find $lim_{hrightarrow 0}frac{sin(ha)}{h}$

How to find $\lim_{h\rightarrow 0}\frac{\sin(ha)}{h}$ without lhopital rule? I know when I use lhopital I easy get $$ \lim_{h\rightarrow 0}...