Class: BigDecimal
- Inherits:
-
Object
- Object
- BigDecimal
- Includes:
- Nio::Formattable
- Defined in:
- lib/nio/fmt.rb,
lib/nio/rtnlzr.rb,
lib/nio/rtnlzr.rb
Class Method Summary collapse
Instance Method Summary collapse
- #nio_r(tol = nil) ⇒ Object
- #nio_write_neutral(fmt) ⇒ Object
-
#nio_xr ⇒ Object
Conversion to Rational preserving the exact value of the number.
Methods included from Nio::Formattable
append_features, #nio_round, #nio_write
Class Method Details
.nio_read_neutral(neutral) ⇒ Object
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 |
# File 'lib/nio/fmt.rb', line 1599 def self.nio_read_neutral(neutral) x = nil if neutral.special? case neutral.special when :nan x = BigDecimal('NaN') # BigDecimal("0")/0 when :inf x = BigDecimal(neutral.sign=='-' ? '-1.0' : '+1.0')/0 end elsif neutral.rep_pos<neutral.digits.length x,y = neutral.to_RepDec.getQ x = BigDecimal(x.to_s)/y else if neutral.base==10 #x = BigDecimal(neutral.digits) #x *= BigDecimal("1E#{(neutral.dec_pos-neutral.digits.length)}") #x = -x if neutral.sign=='-' str = neutral.sign str += neutral.digits str += "E#{(neutral.dec_pos-neutral.digits.length)}" x = BigDecimal(str) else x = BigDecimal(neutral.digits.to_i(neutral.base).to_s) x *= BigDecimal(neutral.base.to_s)**(neutral.dec_pos-neutral.digits.length) x = -x if neutral.sign=='-' end end return x end |
Instance Method Details
#nio_r(tol = nil) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/nio/rtnlzr.rb', line 119 def nio_r(tol = nil) tol ||= Flt.Tolerance([precs[0],Float::DIG].max,:sig_decimals) case tol when Integer Rational(*Nio::Rtnlzr.max_denominator(self,tol,BigDecimal)) else Rational(*Nio::Rtnlzr.new(tol).rationalize(self)) end end |
#nio_write_neutral(fmt) ⇒ Object
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 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 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 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 |
# File 'lib/nio/fmt.rb', line 1632 def nio_write_neutral(fmt) neutral = Nio::NeutralNum.new x = self if x.nan? neutral.set_special(:nan) elsif x.infinite? neutral.set_special(:inf, x<0 ? '-' : '+') else converted = false if fmt.get_ndig==:exact && fmt.get_approx==:simplify prc = [x.precs[0],20].max neutral = x.nio_r(Flt.Tolerance(prc, :sig_decimals)).nio_write_neutral(fmt) converted = true if neutral.digits.length<prc elsif fmt.get_approx==:exact && fmt.get_base!=10 neutral = x.nio_xr.nio_write_neutral(fmt) converted = true end if !converted if fmt.get_base==10 # Don't use x.to_s because of bugs in BigDecimal in Ruby 1.9 revisions 20359-20797 # x.to_s('F') is not affected by that problem, but produces innecesary long strings sgn,ds,b,e = x.split txt = "#{sgn<0 ? '-' : ''}0.#{ds}E#{e}" sign = '+' if txt[0,1]=='-' sign = '-' txt = txt[1...txt.length] end exp = 0 x_char = fmt.get_exp_char(fmt.get_base) exp_i = txt.index(x_char) exp_i = txt.index(x_char.downcase) if exp_i===nil if exp_i!=nil exp = txt[exp_i+1...txt.length].to_i txt = txt[0...exp_i] end dec_pos = txt.index '.' if dec_pos==nil dec_pos = txt.length else txt[dec_pos]='' end dec_pos += exp neutral.set sign, txt, dec_pos, nil, fmt.get_base_digits(10), true, fmt.get_round converted = true end end if !converted x = Flt::DecNum(x.to_s) min_exp = num_class.context.etiny n = x.number_of_digits s,f,e = x.split b = num_class.radix if s < 0 sign = '-' else sign = '+' end prc = x.number_of_digits f = num_class.int_mult_radix_power(f, prc-n) e -= (prc-n) inexact = true rounding = case fmt.get_round when :even :half_even when :zero :half_down when :inf :half_up when :truncate :down when :directed_up :up when :floor :floor when :ceil :ceil else nil end # TODO: use Num#format instead # Note: it is assumed that fmt will be used for for input too, otherwise # rounding should be Float.context.rounding (input rounding for Float) rather than fmt.get_round (output) formatter = Flt::Support::Formatter.new(num_class.radix, num_class.context.etiny, fmt.get_base) formatter.format(x, f, e, rounding, prc, fmt.get_all_digits?) inexact = formatter.round_up if formatter.round_up.is_a?(Symbol) dec_pos,digits = formatter.digits txt = '' digits.each{|d| txt << fmt.get_base_digits.digit_char(d)} neutral.set sign, txt, dec_pos, nil, fmt.get_base_digits, inexact, fmt.get_round end end return neutral end |
#nio_xr ⇒ Object
Conversion to Rational preserving the exact value of the number.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/nio/rtnlzr.rb', line 64 def nio_xr s,f,b,e = split p = f.to_i p = -p if s<0 e = f.size-e if e<0 p *= b**(-e) e = 0 end q = b**(e) return Rational(p,q) end |