Class: GMP::F
- Inherits:
-
Numeric
- Object
- Numeric
- GMP::F
- Defined in:
- ext/gmpf.c,
ext/gmp.c,
ext/gmpf.c
Overview
GMP Multiple Precision floating point numbers.
Instances of this class can store variables of the type mpf_t. This class also contains many methods that act as the functions for mpf_t variables, as well as a few methods that attempt to make this library more Ruby-ish.
Class Method Summary collapse
- .default_prec ⇒ Object
- .default_prec=(arg) ⇒ Object
-
.GMP::R.new(arg) ⇒ Object
Creates a new GMP::R float, with arg as its value, converting where necessary.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Returns the product of
float
andother
. - #** ⇒ Object
-
#+(other) ⇒ Object
Returns the sum of
float
andother
. -
#-(float2) ⇒ Object
Subtracts
float2
fromfloat1
. - #-@ ⇒ Object
-
#/(float2) ⇒ Object
Divides
float1
byfloat2
. - #< ⇒ Object
- #<= ⇒ Object
-
#<=>(arg) ⇒ Object
Float Comparison.
-
#==(arg) ⇒ Object
what does really “equal” mean ? it’s not obvious.
- #> ⇒ Object
- #>= ⇒ Object
-
#abs ⇒ Object
call-seq: float.abs.
-
#abs! ⇒ Object
call-seq: float.abs!.
- #acos ⇒ Object
- #acosh ⇒ Object
- #asin ⇒ Object
- #asinh ⇒ Object
- #atan ⇒ Object
- #atanh ⇒ Object
- #ceil ⇒ Object
- #ceil! ⇒ Object
-
#coerce(arg) ⇒ Object
new method - testing.
- #cos ⇒ Object
- #cosh ⇒ Object
- #exp ⇒ Object
- #expm1 ⇒ Object
- #finite? ⇒ Boolean
-
#floor ⇒ Object
unsorted.
- #floor! ⇒ Object
- #infinite? ⇒ Boolean
- #initialize(*args) ⇒ Object constructor
- #log ⇒ Object
- #log10 ⇒ Object
- #log1p ⇒ Object
- #log2 ⇒ Object
- #nan? ⇒ Boolean
-
#neg! ⇒ Object
call-seq: float.neg!.
- #number? ⇒ Boolean
- #prec ⇒ Object
- #sgn ⇒ Object
- #sin ⇒ Object
- #sinh ⇒ Object
- #sqrt ⇒ Object
- #tan ⇒ Object
- #tanh ⇒ Object
-
#to_d ⇒ Object
(also: #to_f)
******************************************************************** Converting Floats * ********************************************************************.
-
#to_s ⇒ Object
call-seq: float.to_s.
- #trunc ⇒ Object
- #trunc! ⇒ Object
Constructor Details
#initialize(*args) ⇒ Object
74 75 76 77 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 108 109 110 111 112 113 114 115 116 |
# File 'ext/gmpf.c', line 74
VALUE r_gmpf_initialize(int argc, VALUE *argv, VALUE self)
{
MP_FLOAT *self_val, *arg_val_f;
unsigned long prec = 0;
VALUE arg;
mpf_get_struct (self, self_val);
if (argc==0) {
mpf_init(self_val);
mpf_set_si(self_val, 0);
return Qnil;
}
arg = argv[0];
if (argc == 2) {
if (FIXNUM_P(argv[1])) {
if (FIX2INT(argv[1]) >= 0)
prec = FIX2INT(argv[1]);
else
rb_raise(rb_eRangeError, "prec must be non-negative");
} else {
rb_raise(rb_eTypeError, "prec must be FixNum");
}
} else if (GMPF_P(arg)) {
mpf_get_struct (arg, arg_val_f);
prec = mpf_get_prec (arg_val_f);
}
if (prec == 0)
mpf_init (self_val);
else
mpf_init2 (self_val, prec);
if (GMPF_P(arg)) {
mpf_get_struct (arg, arg_val_f);
mpf_set(self_val, arg_val_f);
} else {
mpf_set_value(self_val, arg);
}
return Qnil;
}
|
Class Method Details
.default_prec ⇒ Object
103 104 105 106 107 |
# File 'ext/gmp.c', line 103
static VALUE r_gmpfsg_get_default_prec(VALUE klass)
{
(void)klass;
return INT2NUM(mpf_get_default_prec());
}
|
.default_prec=(arg) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'ext/gmp.c', line 109
static VALUE r_gmpfsg_set_default_prec(VALUE klass, VALUE arg)
{
(void)klass;
if (FIXNUM_P(arg)) {
if (FIX2INT(arg) <= 0) {
rb_raise(rb_eRangeError, "prec must be positive");
}
mpf_set_default_prec (FIX2INT(arg));
} else {
rb_raise(rb_eTypeError, "prec must be FixNum");
}
return Qnil;
}
|
.GMP::R.new(arg) ⇒ Object
Creates a new GMP::R float, with arg as its value, converting where necessary.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'ext/gmpf.c', line 58
VALUE r_gmpfsg_new(int argc, VALUE *argv, VALUE klass)
{
MP_FLOAT *res_val;
VALUE res;
(void)klass;
if (argc > 2)
rb_raise(rb_eArgError, "wrong # of arguments(%d for 0, 1 or 2)", argc);
mpf_make_struct (res, res_val);
rb_obj_call_init(res, argc, argv);
return res;
}
|
Instance Method Details
#*(other) ⇒ Object
Returns the product of float
and other
. other
can be
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
-
Float
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'ext/gmpf.c', line 313
VALUE r_gmpf_mul(VALUE self, VALUE arg)
{
MP_FLOAT *self_val, *res_val, *arg_val_f;
MP_RAT *arg_val_q;
MP_INT *arg_val_z;
VALUE res;
unsigned long prec;
mpf_get_struct_prec (self, self_val, prec);
if (GMPF_P(arg)) {
mpf_get_struct(arg, arg_val_f);
prec_max(prec, arg_val_f);
mpf_make_struct_init(res, res_val, prec);
mpf_mul(res_val, self_val, arg_val_f);
} else if (GMPQ_P(arg)) {
mpq_get_struct(arg, arg_val_q);
mpf_make_struct_init(res, res_val, prec);
mpf_set_q(res_val, arg_val_q);
mpf_mul(res_val, self_val, res_val);
} else if (GMPZ_P(arg)) {
mpz_get_struct(arg, arg_val_z);
mpf_make_struct_init(res, res_val, prec);
mpf_set_z(res_val, arg_val_z);
mpf_mul(res_val, self_val, res_val);
} else if (FLOAT_P(arg)) {
mpf_make_struct_init(res, res_val, prec);
mpf_set_d(res_val, NUM2DBL(arg));
mpf_mul(res_val, self_val, res_val);
} else if (FIXNUM_P(arg)) { // _ui with sign control instead ?
mpf_make_struct_init(res, res_val, prec);
mpf_set_si(res_val, FIX2INT(arg));
mpf_mul(res_val, self_val, res_val);
} else if (BIGNUM_P(arg)) {
mpz_temp_from_bignum(arg_val_z, arg);
mpf_make_struct_init(res, res_val, prec);
mpf_set_z(res_val, arg_val_z);
mpf_mul(res_val, res_val, self_val);
mpz_temp_free(arg_val_z);
} else {
typeerror(ZQFXBD);
}
return res;
}
|
#** ⇒ Object
#+(other) ⇒ Object
Returns the sum of float
and other
. other
can be
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
-
Float
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'ext/gmpf.c', line 197
VALUE r_gmpf_add(VALUE self, VALUE arg)
{
MP_FLOAT *self_val, *res_val, *arg_val_f;
MP_RAT *arg_val_q;
MP_INT *arg_val_z;
VALUE res;
unsigned long prec;
mpf_get_struct_prec (self, self_val, prec);
if (GMPF_P(arg)) {
mpf_get_struct (arg, arg_val_f);
prec_max(prec, arg_val_f);
mpf_make_struct_init(res, res_val, prec);
mpf_add(res_val, self_val, arg_val_f);
} else if (GMPQ_P(arg)) {
mpq_get_struct (arg, arg_val_q);
mpf_make_struct_init(res, res_val, prec);
mpf_set_q (res_val, arg_val_q);
mpf_add (res_val, res_val, self_val);
} else if (GMPZ_P(arg)) {
mpz_get_struct (arg, arg_val_z);
mpf_make_struct_init(res, res_val, prec);
mpf_set_z (res_val, arg_val_z);
mpf_add (res_val, res_val, self_val);
} else if (FLOAT_P(arg)) {
mpf_make_struct_init(res, res_val, prec);
mpf_set_d (res_val, NUM2DBL(arg));
mpf_add (res_val, res_val, self_val);
} else if (FIXNUM_P(arg)) { // _ui with sign control instead ?
mpf_make_struct_init(res, res_val, prec);
mpf_set_si (res_val, FIX2INT(arg));
mpf_add (res_val, res_val, self_val);
} else if (BIGNUM_P(arg)) {
mpz_temp_from_bignum(arg_val_z, arg);
mpf_make_struct_init(res, res_val, prec);
mpf_set_z (res_val, arg_val_z);
mpf_add (res_val, res_val, self_val);
mpz_temp_free(arg_val_z);
} else {
typeerror(ZQFXBD);
}
return res;
}
|
#-(float2) ⇒ Object
Subtracts float2
from float1
. float2
can be
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
-
Float
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'ext/gmpf.c', line 255
VALUE r_gmpf_sub(VALUE self, VALUE arg)
{
MP_FLOAT *self_val, *res_val, *arg_val_f;
MP_RAT *arg_val_q;
MP_INT *arg_val_z;
VALUE res;
unsigned long prec;
mpf_get_struct_prec (self, self_val, prec);
if (GMPF_P(arg)) {
mpf_get_struct(arg, arg_val_f);
prec_max(prec, arg_val_f);
mpf_make_struct_init(res, res_val, prec);
mpf_sub(res_val, self_val, arg_val_f);
} else if (GMPQ_P(arg)) {
mpq_get_struct(arg, arg_val_q);
mpf_make_struct_init(res, res_val, prec);
mpf_set_q(res_val, arg_val_q);
mpf_sub(res_val, self_val, res_val);
} else if (GMPZ_P(arg)) {
mpz_get_struct(arg, arg_val_z);
mpf_make_struct_init(res, res_val, prec);
mpf_set_z(res_val, arg_val_z);
mpf_sub(res_val, self_val, res_val);
} else if (FLOAT_P(arg)) {
mpf_make_struct_init(res, res_val, prec);
mpf_set_d(res_val, NUM2DBL(arg));
mpf_sub(res_val, self_val, res_val);
} else if (FIXNUM_P(arg)) { // _ui with sign control instead ?
mpf_make_struct_init(res, res_val, prec);
mpf_set_si(res_val, FIX2INT(arg));
mpf_sub(res_val, self_val, res_val);
} else if (BIGNUM_P(arg)) {
mpz_temp_from_bignum(arg_val_z, arg);
mpf_make_struct_init(res, res_val, prec);
mpf_set_z(res_val, arg_val_z);
mpf_sub(res_val, self_val, res_val);
mpz_temp_free(arg_val_z);
} else {
typeerror(ZQFXBD);
}
return res;
}
|
#-@ ⇒ Object
#/(float2) ⇒ Object
Divides float1
by float2
. float2
can be
-
GMP::Z
-
Fixnum
-
GMP::Q
-
GMP::F
-
Bignum
-
Float
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'ext/gmpf.c', line 371
VALUE r_gmpf_div(VALUE self, VALUE arg)
{
MP_FLOAT *self_val, *res_val, *arg_val_f;
MP_RAT *arg_val_q;
MP_INT *arg_val_z;
VALUE res;
unsigned long prec;
mpf_get_struct_prec (self, self_val, prec);
if (GMPF_P(arg)) {
mpf_get_struct (arg, arg_val_f);
prec_max(prec, arg_val_f);
mpf_make_struct_init(res, res_val, prec);
mpf_div(res_val, self_val, arg_val_f);
} else if (GMPQ_P(arg)) {
mpq_get_struct (arg, arg_val_q);
mpf_make_struct_init(res, res_val, prec);
mpf_set_q (res_val, arg_val_q);
mpf_div (res_val, self_val, res_val);
} else if (GMPZ_P(arg)) {
mpz_get_struct (arg, arg_val_z);
mpf_make_struct_init(res, res_val, prec);
mpf_set_z (res_val, arg_val_z);
mpf_div (res_val, self_val, res_val);
} else if (FLOAT_P(arg)) {
mpf_make_struct_init(res, res_val, prec);
mpf_set_d (res_val, NUM2DBL(arg));
mpf_div (res_val, self_val, res_val);
} else if (FIXNUM_P(arg)) { // _ui with sign control instead ?
mpf_make_struct_init(res, res_val, prec);
mpf_set_si (res_val, FIX2INT(arg));
mpf_div (res_val, self_val, res_val);
} else if (BIGNUM_P(arg)) {
mpz_temp_from_bignum(arg_val_z, arg);
mpf_make_struct_init(res, res_val, prec);
mpf_set_z (res_val, arg_val_z);
mpf_div (res_val, self_val, res_val);
mpz_temp_free(arg_val_z);
} else {
typeerror(ZQFXBD);
}
return res;
}
|
#< ⇒ Object
#<= ⇒ Object
#<=>(arg) ⇒ Object
Float Comparison
487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'ext/gmpf.c', line 487
VALUE r_gmpf_cmp(VALUE self, VALUE arg)
{
MP_FLOAT *self_val;
int res;
mpf_get_struct(self,self_val);
res = mpf_cmp_value(self_val, arg);
if (res > 0)
return INT2FIX(1);
else if (res == 0)
return INT2FIX(0);
else
return INT2FIX(-1);
}
|
#==(arg) ⇒ Object
what does really “equal” mean ? it’s not obvious
480 481 482 483 484 485 |
# File 'ext/gmpf.c', line 480
VALUE r_gmpf_eq(VALUE self, VALUE arg)
{
MP_FLOAT *self_val;
mpf_get_struct (self,self_val);
return (mpf_cmp_value(self_val, arg) == 0) ? Qtrue : Qfalse;
}
|
#> ⇒ Object
#>= ⇒ Object
#abs ⇒ Object
call-seq:
float.abs
From the GMP Manual:
Returns the absolute value of float
.
#abs! ⇒ Object
call-seq:
float.abs!
Sets float
to the absolute value of float
.
#acos ⇒ Object
#acosh ⇒ Object
#asin ⇒ Object
#asinh ⇒ Object
#atan ⇒ Object
#atanh ⇒ Object
#ceil ⇒ Object
#ceil! ⇒ Object
#coerce(arg) ⇒ Object
new method - testing
98 99 100 101 |
# File 'ext/gmp.c', line 98
static VALUE r_gmpf_coerce(VALUE self, VALUE arg)
{
return rb_assoc_new(r_gmpfsg_new(1, &arg, cGMP_F), self);
}
|
#cos ⇒ Object
#cosh ⇒ Object
#exp ⇒ Object
#expm1 ⇒ Object
#finite? ⇒ Boolean
#floor ⇒ Object
unsorted
#floor! ⇒ Object
#infinite? ⇒ Boolean
#log ⇒ Object
#log10 ⇒ Object
#log1p ⇒ Object
#log2 ⇒ Object
#nan? ⇒ Boolean
#neg! ⇒ Object
call-seq:
float.neg!
Sets float
to -float
.
#number? ⇒ Boolean
#prec ⇒ Object
530 531 532 533 534 535 |
# File 'ext/gmpf.c', line 530
VALUE r_gmpf_get_prec(VALUE self)
{
MP_FLOAT *self_val;
mpf_get_struct(self, self_val);
return INT2NUM(mpf_get_prec(self_val));
}
|
#sgn ⇒ Object
#sin ⇒ Object
#sinh ⇒ Object
#sqrt ⇒ Object
#tan ⇒ Object
#tanh ⇒ Object
#to_d ⇒ Object Also known as: to_f
********************************************************************
Converting Floats *
********************************************************************
135 136 137 138 139 140 141 |
# File 'ext/gmpf.c', line 135
VALUE r_gmpf_to_d(VALUE self)
{
MP_FLOAT *self_val;
mpf_get_struct(self, self_val);
return rb_float_new(mpf_get_d(self_val));
}
|
#to_s ⇒ Object
call-seq:
float.to_s
Returns the decimal representation of float
, as a string.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'ext/gmpf.c', line 151
VALUE r_gmpf_to_s(VALUE self)
{
MP_FLOAT *self_val;
char *str, *str2;
VALUE res;
mp_exp_t exponent;
mpf_get_struct(self, self_val);
str = mpf_get_str(NULL, &exponent, 10, 0, self_val);
if ((strcmp(str, "NaN") == 0) ||
(strcmp(str, "Inf") == 0) ||
(strcmp(str, "-Inf") == 0))
{
res = rb_str_new2(str);
}
else
{
if (str[0] == '-')
__gmp_asprintf(&str2, "-0.%se%+ld", str+1, exponent);
else
__gmp_asprintf(&str2, "0.%se%+ld", str, exponent);
res = rb_str_new2(str2);
free(str2);
}
free(str);
return res;
}
|