Class: Float

Inherits:
Object
  • Object
show all
Defined in:
lib/sollya.rb

Instance Method Summary collapse

Instance Method Details

#to_hex(pad: false) ⇒ String

Convert the float64 value to a C99 hexadecimal string

Parameters:

  • pad (Boolean) (defaults to: false)

    if set to true and the number is positive, a space will be placed bebore the '0x'

Returns:



67
68
69
70
71
72
# File 'lib/sollya.rb', line 67

def to_hex(pad: false)
  m, e = Math.frexp(abs)
  e -= 1
  s = m > 0 ? (m * 2**53).to_i.to_s(16) : '0'*14
  "#{negative? ? '-' : (pad ? ' ' : '')}0x#{s[0]}.#{s[1..]}p#{e.negative? ? '-' : '+'}#{e.abs}"
end

#to_hexf(pad: false, round: :nearest) ⇒ String

Convert the float64 value to a float32 following the round direction, then convert the float32 to a hexadecimal string in C99 style

Parameters:

  • pad (Boolean) (defaults to: false)

    if set to true and the number is positive, a space will be placed bebore the '0x'

  • round (Symbol) (defaults to: :nearest)

    float64 to float32 rounding direction, either :nearest, :up, :down, or :zero

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sollya.rb', line 78

def to_hexf(pad: false, round: :nearest)
  m, e = Math.frexp(abs)
  e -= 1
  m = (m * 2**53).to_i
  rest = m & 0x000000001fffffff
  m =   (m & 0x001fffffe0000000) >> 29
  addulp = case round
  when :nearest, :rndn
    r = (rest & 0x10000000) != 0
    s = (rest & 0x0fffffff) != 0
    r and (s or (m & 1) == 1)
  when :up,      :rndu
    not(negative? or rest.zero?)
  when :down,    :rndd
    negative? and not(rest.zero?)
  when :zero,    :rndz
    false
  else
    raise TypeError, "round musb be :nearest, :up, :down, or :zero, not #{round}"
  end
  if addulp then
    m += 1
    unless (m & 0x1000000).zero? then
      m >>= 1
      e += 1
    end
  end
  s = m > 0 ? (m << 1).to_s(16) : '0'*6
  "#{negative? ? '-' : (pad ? ' ' : '')}0x#{s[0]}.#{s[1..]}p#{e.negative? ? '-' : '+'}#{e.abs}f"
end

#to_mpfrMPFR

Return a MPFR object with a precision of 53 bits whose value is self.

Returns:



473
474
475
476
477
478
479
480
# File 'ext/mpfr_rb.c', line 473

static VALUE mpfrrb_Float_to_mpfr(VALUE self)
{
  VALUE obj = mpfrrb_alloc(c_MPFR);
  mpfr_ptr mp = mpfrrb_rb2ref(obj);
  mpfr_init2(mp, 53);
  mpfr_set_d(mp, NUM2DBL(self), MPFR_RNDN);
  return obj;
}

#to_sollyaObject



251
252
253
254
# File 'ext/sollya_rb.c', line 251

static VALUE sollyarb_float_to_sollya(VALUE self)
{
  return sollyarb_ref2rb(sollya_lib_constant_from_double(NUM2DBL(self)), c_SolFunction);
}