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, #to_Z
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
1500
1501
1502
|
# File 'lib/m500.rb', line 1500
def a
@a
end
|
#abs1 ⇒ Object
Returns the value of attribute abs1.
1500
1501
1502
|
# File 'lib/m500.rb', line 1500
def abs1
@abs1
end
|
#b ⇒ Object
Returns the value of attribute b.
1500
1501
1502
|
# File 'lib/m500.rb', line 1500
def b
@b
end
|
#deltaIter ⇒ Object
Returns the value of attribute deltaIter.
1500
1501
1502
|
# File 'lib/m500.rb', line 1500
def deltaIter
@deltaIter
end
|
#denominator ⇒ Object
Returns the value of attribute denominator.
1500
1501
1502
|
# File 'lib/m500.rb', line 1500
def denominator
@denominator
end
|
#numerator ⇒ Object
Returns the value of attribute numerator.
1500
1501
1502
|
# File 'lib/m500.rb', line 1500
def numerator
@numerator
end
|
Class Method Details
.new!(a, b = 1) ⇒ Object
1465
1466
1467
1468
1469
1470
1471
|
# File 'lib/m500.rb', line 1465
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
1620
1621
1622
1623
|
# File 'lib/m500.rb', line 1620
def % (other)
value = (self / other).to_i
return self - other * value
end
|
#*(a) ⇒ Object
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
|
# File 'lib/m500.rb', line 1554
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
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
|
# File 'lib/m500.rb', line 1597
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
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
|
# File 'lib/m500.rb', line 1506
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
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
|
# File 'lib/m500.rb', line 1530
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
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
|
# File 'lib/m500.rb', line 1579
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
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
|
# File 'lib/m500.rb', line 1653
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
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
|
# File 'lib/m500.rb', line 1631
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
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
|
# File 'lib/m500.rb', line 1642
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
1628
1629
1630
|
# File 'lib/m500.rb', line 1628
def abs
@abs1 = -1 ? Quotient.new!(@numerator, @denominator) : self
end
|
#coerce(other) ⇒ Object
1684
1685
1686
1687
1688
1689
1690
1691
1692
|
# File 'lib/m500.rb', line 1684
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
1624
1625
1626
1627
|
# File 'lib/m500.rb', line 1624
def diavmod(other)
value = (self / other).to_i
return value, self - other * value
end
|
#hash ⇒ Object
1758
1759
1760
|
# File 'lib/m500.rb', line 1758
def hash
@numerator.hash ^ @denominator.hash ^ @abs.hash
end
|
#inspect ⇒ Object
1755
1756
1757
|
# File 'lib/m500.rb', line 1755
def inspect
@abs1 == -1 ? sprintf("Quotient(-%s, %s)", @numerator.inspect, @denominator.inspect) : sprintf("Quotient(%s, %s)", @numerator.inspect, @denominator.inspect)
end
|
#is_0? ⇒ Boolean
1693
1694
1695
|
# File 'lib/m500.rb', line 1693
def is_0?
@numerator === 0 and @denominator === 1 ? true : false
end
|
#q2Dec(remainder) ⇒ Object
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
|
# File 'lib/m500.rb', line 1714
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
1503
1504
1505
|
# File 'lib/m500.rb', line 1503
def setdelta
@setdelta = true
end
|
#succ ⇒ Object
1681
1682
1683
|
# File 'lib/m500.rb', line 1681
def succ
self + @@delta
end
|
#to_Dec ⇒ Object
1705
1706
1707
|
# File 'lib/m500.rb', line 1705
def to_Dec
@to_Dec ||= q2Dec(@numerator.remainder(@denominator))
end
|
#to_f ⇒ Object
1699
1700
1701
|
# File 'lib/m500.rb', line 1699
def to_f
@numerator.to_f/@denominator.to_f
end
|
#to_Frac ⇒ Object
1702
1703
1704
|
# File 'lib/m500.rb', line 1702
def to_Frac
self >= 0 ? Fraction(Zahlen(@numerator),Natural(@denominator),1) : Fraction(Zahlen(@numerator),Natural(@denominator),-1)
end
|
#to_i ⇒ Object
1696
1697
1698
|
# File 'lib/m500.rb', line 1696
def to_i
Integer(@numerator.div(@denominator))
end
|
#to_K ⇒ Object
1711
1712
1713
|
# File 'lib/m500.rb', line 1711
def to_K
Kettenbruch(self.to_Frac)
end
|
#to_s ⇒ Object
1748
1749
1750
1751
1752
1753
1754
|
# File 'lib/m500.rb', line 1748
def to_s
if @b == 1
(@a*@abs1).to_s
else
"#{@numerator*@abs1}/#{@denominator}"
end
end
|
#to_s! ⇒ Object
1745
1746
1747
|
# File 'lib/m500.rb', line 1745
def to_s!
"Quotient(#{(@a*@abs1).to_s},#{@b.to_s})"
end
|
#to_sgml ⇒ Object
1462
1463
1464
|
# File 'lib/m500.rb', line 1462
def to_sgml
"<mfrac #{sgml_id}class='quotient'><mn>#{@numerator*@abs1}</mn> <mn>#{@denominator}</mn></mfrac>"
end
|
#to_Sig ⇒ Object
1708
1709
1710
|
# File 'lib/m500.rb', line 1708
def to_Sig
Sigma(self)
end
|