Class: Quotient
Constant Summary
collapse
- @@delta =
0
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from SGML
#sgml_id, #tog_sgml_id
Methods inherited from Numeric
#irrational?, #rational?, #sgml_id, #to_N, #to_N0, #to_Q, #to_R
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
1570
1571
1572
|
# File 'lib/m500.rb', line 1570
def a
@a
end
|
#abs1 ⇒ Object
Returns the value of attribute abs1.
1570
1571
1572
|
# File 'lib/m500.rb', line 1570
def abs1
@abs1
end
|
#b ⇒ Object
Returns the value of attribute b.
1570
1571
1572
|
# File 'lib/m500.rb', line 1570
def b
@b
end
|
#deltaIter ⇒ Object
Returns the value of attribute deltaIter.
1570
1571
1572
|
# File 'lib/m500.rb', line 1570
def deltaIter
@deltaIter
end
|
#denominator ⇒ Object
Returns the value of attribute denominator.
1570
1571
1572
|
# File 'lib/m500.rb', line 1570
def denominator
@denominator
end
|
#numerator ⇒ Object
Returns the value of attribute numerator.
1570
1571
1572
|
# File 'lib/m500.rb', line 1570
def numerator
@numerator
end
|
Class Method Details
.new!(a, b = 1) ⇒ Object
1535
1536
1537
1538
1539
1540
1541
|
# File 'lib/m500.rb', line 1535
def Quotient.new!(a, b = 1)
if a.kind_of?(Quotient) && b == 1
a
else
new(a, b)
end
end
|
Instance Method Details
#%(other) ⇒ Object
1690
1691
1692
1693
|
# File 'lib/m500.rb', line 1690
def % (other)
value = (self / other).to_i
return self - other * value
end
|
#*(a) ⇒ Object
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
|
# File 'lib/m500.rb', line 1624
def * (a)
if a == 0 or ( @numerator == 0 and @denominator == 1)
return Quotient(0,1)
elsif a.kind_of?(Quotient)
abs = @abs1 * a.abs1
num = @numerator * a.numerator
den = @denominator * a.denominator
Quotient(num*abs, den)
elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
naught
elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
infinity
elsif (a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum))
if a ==1 then
self
else
self * Quotient(a, 1)
end
elsif a.kind_of?(Float)
self * a.to_Q
else
x, y = a.coerce(self)
x * y
end
end
|
#**(other) ⇒ Object
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
|
# File 'lib/m500.rb', line 1667
def ** (other)
if other.kind_of?(Quotient)
Float(self) ** other
elsif other.kind_of?(Integer)
if other > 0
num = @numerator ** other
den = @denominator ** other
elsif other < 0
num = @denominator ** -other
den = @numerator ** -other
elsif other == 0
num = 1
den = 1
end
tmp = Quotient.new!(num, den)
Quotient(tmp.numerator,tmp.denominator)
elsif other.kind_of?(Float)
Float(self) ** other
else
x, y = other.coerce(self)
x ** y
end
end
|
#+(a) ⇒ Object
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
|
# File 'lib/m500.rb', line 1576
def + (a)
if a == 0
return self
elsif @numerator == 0 and @denominator == 1
return a.to_Q
elsif a.kind_of?(Fraction)
self + a.to_Q
elsif a.kind_of?(Quotient)
num = (@numerator*@abs1) * (a.denominator)
num_a = (a.numerator*a.abs1) * @denominator
Quotient(num + num_a, @denominator * a.denominator)
elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
self
elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
infinity
elsif (a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum))
self + Quotient(a, 1)
elsif a.kind_of?(Float)
Float(self) + a
else
x, y = a.coerce(self)
x + y
end
end
|
#-(a) ⇒ Object
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
|
# File 'lib/m500.rb', line 1600
def - (a)
if a == 0
return self
elsif @numerator == 0 and @denominator == 1
return a.to_Q
else
if a.kind_of?(Quotient)
num = (@numerator*@abs1) * (a.denominator)
num_a = (a.numerator*a.abs1) * @denominator
Quotient(num - num_a, @denominator * a.denominator)
elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) or a == 0
self
elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
infinity*(-1)
elsif (a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum))
self - Quotient.new!(a, 1)
elsif a.kind_of?(Float)
Float(self) - a
else
x, y = a.coerce(self)
x - y
end
end
end
|
#/(a) ⇒ Object
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
|
# File 'lib/m500.rb', line 1649
def / (a)
if (@numerator == 0 and @denominator == 1)
return Quotient(0,1)
elsif a.kind_of?(Quotient)
abs = @abs1 * a.abs1
num = @numerator * a.denominator
den = @denominator * a.numerator
Quotient(num*abs, den)
elsif a.kind_of?(Integer)
raise ZeroDivisionError, "division by zero" if a == 0
self / Quotient.new!(a, 1)
elsif a.kind_of?(Float)
Float(self) / a
else
x, y = a.coerce(self)
x / y
end
end
|
#<=>(other) ⇒ Object
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
|
# File 'lib/m500.rb', line 1723
def <=> (other)
if @setdelta then
@@delta = Quotient(((other - self).abs)/@deltaIter) if (other.abs1 == 1 and self.abs1 == 1) or (other.abs1 == -1 and self.abs1 == -1)
@@delta = Quotient((Quotient(other.numerator,other.denominator) + Quotient(self.numerator,self.denominator))/@deltaIter) if (other.abs1 == 1 and self.abs1 == -1) or (other.abs1 == -1 and self.abs1 == 1)
end
@setdelta = false
if other.kind_of?(Quotient)
num = (@numerator*@abs1) * other.denominator
num_o = (other.numerator*other.abs1) * @denominator
v = num- num_o
if v > 0
return 1
elsif v < 0
return -1
else
return 0
end
elsif other.kind_of?(Integer)
return self <=> Quotient.new!(other, 1)
elsif other.kind_of?(Float)
return Float(self) <=> other
elsif defined? other.coerce
x, y = other.coerce(self)
return x <=> y
else
return nil
end
end
|
#==(other) ⇒ Object
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
|
# File 'lib/m500.rb', line 1701
def == (other)
if other.kind_of?(Quotient)
@numerator == other.numerator and @denominator == other.denominator
elsif other.kind_of?(Integer)
self == Quotient.new!(other, 1)
elsif other.kind_of?(Float)
Float(self) == other
else
other.to_i == self.to_i
end
end
|
#===(other) ⇒ Object
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
|
# File 'lib/m500.rb', line 1712
def === (other)
if other.kind_of?(Quotient)
@a == other.a and @b == other.b
return 1
elsif other.kind_of?(Quotient)
@numerator == other.numerator and @denominator == other.denominator
return -1
else
return 0
end
end
|
#abs ⇒ Object
1698
1699
1700
|
# File 'lib/m500.rb', line 1698
def abs
@abs1 = -1 ? Quotient.new!(@numerator, @denominator) : self
end
|
#coerce(other) ⇒ Object
1754
1755
1756
1757
1758
1759
1760
1761
1762
|
# File 'lib/m500.rb', line 1754
def coerce(other)
if Natural === other or Counting === other or Zahlen === other
[Quotient(other,1),self]
elsif Fixnum === other
[other, self.to_f]
else
[Float(other),self.to_f]
end
end
|
#diavmod(other) ⇒ Object
1694
1695
1696
1697
|
# File 'lib/m500.rb', line 1694
def diavmod(other)
value = (self / other).to_i
return value, self - other * value
end
|
#hash ⇒ Object
1830
1831
1832
|
# File 'lib/m500.rb', line 1830
def hash
@numerator.hash ^ @denominator.hash ^ @abs.hash
end
|
#inspect ⇒ Object
1827
1828
1829
|
# File 'lib/m500.rb', line 1827
def inspect
@abs1 == -1 ? sprintf("Quotient(-%s, %s)", @numerator.inspect, @denominator.inspect) : sprintf("Quotient(%s, %s)", @numerator.inspect, @denominator.inspect)
end
|
#is_0? ⇒ Boolean
1763
1764
1765
|
# File 'lib/m500.rb', line 1763
def is_0?
@numerator === 0 and @denominator === 1 ? true : false
end
|
#q2Dec(remainder) ⇒ Object
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
|
# File 'lib/m500.rb', line 1787
def q2Dec(remainder)
maxcount =1000
kmcount = 0
ret = nil
until (@remainders.member?(remainder) and @remainders.index(remainder)!= 0) or (kmcount > maxcount + 100) do
break if remainder == 0
@remainders << remainder
@reg2 << remainder.div(@denominator.to_i)
remainder = remainder.remainder(@denominator.to_i)*10
kmcount += 1
end
@reg2.shift
if remainder == 0
if @abs1 ==1 then
ret = "Decimal('#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}')"
elsif @abs1 == -1
ret = "Decimal('-#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}')"
end
else
@reg2.insert(@remainders.find_index(remainder)-1,"[") unless (kmcount > maxcount + 100)
if @abs1 ==1 then
ret = "Decimal('#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}]')" unless (kmcount > maxcount + 100)
ret = "Decimal('#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}[0]')" if (kmcount > maxcount + 100)
elsif @abs1 == -1
ret = "Decimal('-#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}]')" unless (kmcount > maxcount + 100)
ret = "Decimal('#{@numerator.div(@denominator)}#{Decimal::decimalSeparator}#{@reg2.join}[0]')" if (kmcount > maxcount + 100)
end
end
eval(ret)
end
|
#setdelta ⇒ Object
1573
1574
1575
|
# File 'lib/m500.rb', line 1573
def setdelta
@setdelta = true
end
|
#succ ⇒ Object
1751
1752
1753
|
# File 'lib/m500.rb', line 1751
def succ
self + @@delta
end
|
#to_Dec ⇒ Object
1775
1776
1777
|
# File 'lib/m500.rb', line 1775
def to_Dec
@to_Dec ||= q2Dec(@numerator.remainder(@denominator))
end
|
#to_f ⇒ Object
1769
1770
1771
|
# File 'lib/m500.rb', line 1769
def to_f
@numerator.to_f/@denominator.to_f
end
|
#to_i ⇒ Object
1766
1767
1768
|
# File 'lib/m500.rb', line 1766
def to_i
Integer(@numerator.div(@denominator))
end
|
#to_K ⇒ Object
1781
1782
1783
|
# File 'lib/m500.rb', line 1781
def to_K
Kettenbruch(self.to_Frac)
end
|
#to_s ⇒ Object
1820
1821
1822
1823
1824
1825
1826
|
# File 'lib/m500.rb', line 1820
def to_s
if @b == 1
(@a*@abs1).to_s
else
"#{@numerator*@abs1}/#{@denominator}"
end
end
|
#to_s! ⇒ Object
1817
1818
1819
|
# File 'lib/m500.rb', line 1817
def to_s!
"Quotient(#{(@a*@abs1).to_s},#{@b.to_s})"
end
|
#to_sgml ⇒ Object
1532
1533
1534
|
# File 'lib/m500.rb', line 1532
def to_sgml
"<mfrac #{sgml_id}class='quotient'><mn>#{@numerator*@abs1}</mn> <mn>#{@denominator}</mn></mfrac>"
end
|
#to_Sig ⇒ Object
1778
1779
1780
|
# File 'lib/m500.rb', line 1778
def to_Sig
Sigma(self)
end
|
#to_Z ⇒ Object
1784
1785
1786
|
# File 'lib/m500.rb', line 1784
def to_Z
self.denominator == 1 ? Zahlen(self.numerator) : emptySet
end
|